Hi, I'm new to lua and love2d, before using lua I've used processing and I still use it from time to time, but it's not game-oriented, that's why I decided on lua and love2d.
I have been trying to learn oop with metatables in lua but I don't understand them very well and I am having problems, I am going to show an example where a ship launches bullets.
There are 4 files, the main, the player, the bullet and the parent class that handles a list for all classes and some functions to remove objects from the list and update all objects, although in this example it only handles update the shot class which is the one that inherits from the parent class.
When the player presses z and shoots an error occurs that points to the remove method of the shot class, if self.remove() is commented out of the draw() method it works but the shot it fires has a weird effect.
I would like to know where I am making the mistakes, I know there is a library called classic that works very well but I prefer to do it without external libraries.
If there are other better ways to do this I'll be happy to learn new ways.
problems with oop and inheritance
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
problems with oop and inheritance
- Attachments
-
- prueba6.2.zip
- (6.73 KiB) Downloaded 55 times
Re: problems with oop and inheritance
Hi hokuto. I wrote what I understand of emulating classes in Lua in this post here: https://love2d.org/forums/viewtopic.php ... 86#p253586
The other posts in that thread are also useful.
When I was first trying to learn that in Lua, I didn't try to make a complex game with them, but something simpler that just printed some text (like they usually do in tutorials for using classes in languages that support them).
Did you go through this step? It's really helpful
For instance, writing test code that does something like...
PS to do that, you need to start Löve in console mode (using either lovec.exe, or "love.exe --console") so you can use Lua's print() function to output text to the console (via stdout). This isn't the same as love.graphics.print() or .printf(), as these render text on the Löve screen as 2D graphics.
The other posts in that thread are also useful.
When I was first trying to learn that in Lua, I didn't try to make a complex game with them, but something simpler that just printed some text (like they usually do in tutorials for using classes in languages that support them).
Did you go through this step? It's really helpful
For instance, writing test code that does something like...
Code: Select all
p.virtualFunction() -- Prints "Function of table: 0x00372b68".
a.virtualFunction() -- Prints "Function of table: 0x00374ff0, inherited from table: 0x00372b68".
b.virtualFunction() -- Prints "Function of table: 0x0037b518, inherited from table: 0x00372b68".
Re: problems with oop and inheritance
The error here is that you forget the self in the "parteAlta" field.
I uploaded a new version of your code with some comments that, I hope, will help you understand how oop works in lua.
Re: problems with oop and inheritance
Hi, I made a little improvement in the code, shown below
With this change, the Disparo class is now able to use the Padre's class methods the way you intended
Code: Select all
Disparo = setmetable({}, Padre)
- Attachments
-
- prueba6.2_v2.0.zip
- (6.61 KiB) Downloaded 66 times
Re: problems with oop and inheritance
Thank you very much for the link, I'll take a look.RNavega wrote: ↑Thu Mar 30, 2023 9:53 pm Hi hokuto. I wrote what I understand of emulating classes in Lua in this post here: https://love2d.org/forums/viewtopic.php ... 86#p253586
The other posts in that thread are also useful.
When I was first trying to learn that in Lua, I didn't try to make a complex game with them, but something simpler that just printed some text (like they usually do in tutorials for using classes in languages that support them).
Did you go through this step? It's really helpful
For instance, writing test code that does something like...PS to do that, you need to start Löve in console mode (using either lovec.exe, or "love.exe --console") so you can use Lua's print() function to output text to the console (via stdout). This isn't the same as love.graphics.print() or .printf(), as these render text on the Löve screen as 2D graphics.Code: Select all
p.virtualFunction() -- Prints "Function of table: 0x00372b68". a.virtualFunction() -- Prints "Function of table: 0x00374ff0, inherited from table: 0x00372b68". b.virtualFunction() -- Prints "Function of table: 0x0037b518, inherited from table: 0x00372b68".
Re: problems with oop and inheritance
Thank you very much for the help, now it works perfectly._JM_ wrote: ↑Fri Mar 31, 2023 1:13 pm Hi, I made a little improvement in the code, shown below
With this change, the Disparo class is now able to use the Padre's class methods the way you intendedCode: Select all
Disparo = setmetable({}, Padre)
I forgot to say that I only speak Spanish, I say it in case there is something that is not understood in the written text, I have used the google translator.
If you know another way to do it that is better or easier or more optimal, I would like you to explain it to me. By the way, there is a metatables tutorial in the forum to learn how to use metatables better.
Re: problems with oop and inheritance
I missed a comment and when I translated it with the translator I could understand it.
Well, we will have to introduce the object to the list and thus we would not have to call the update method of the player class in the main. This would be used -> Parent: insert (object).
Code: Select all
That said, as a exercise, see if you can change the 'Jugador' class
so that it adds the player instance to the list of objects. If
you can do that, there will be no more reason to call the
player's update an draw method in main.lua, since it will be called
by the 'Padre' class
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 11 guests