problems with oop and inheritance

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
hokuto
Prole
Posts: 18
Joined: Thu Mar 30, 2023 4:17 pm

problems with oop and inheritance

Post by hokuto »

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.
Attachments
prueba6.2.zip
(6.73 KiB) Downloaded 37 times
RNavega
Party member
Posts: 235
Joined: Sun Aug 16, 2020 1:28 pm

Re: problems with oop and inheritance

Post by RNavega »

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...

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".
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.
User avatar
_JM_
Prole
Posts: 9
Joined: Thu Mar 30, 2023 9:06 pm
Contact:

Re: problems with oop and inheritance

Post by _JM_ »

hokuto wrote: Thu Mar 30, 2023 7:43 pm When the player presses z and shoots an error occurs that points to the remove method of the shot class
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.
prueba6.2_v1.0.zip
(7.04 KiB) Downloaded 34 times
User avatar
_JM_
Prole
Posts: 9
Joined: Thu Mar 30, 2023 9:06 pm
Contact:

Re: problems with oop and inheritance

Post by _JM_ »

Hi, I made a little improvement in the code, shown below

Code: Select all

Disparo = setmetable({}, Padre)
With this change, the Disparo class is now able to use the Padre's class methods the way you intended
Attachments
prueba6.2_v2.0.zip
(6.61 KiB) Downloaded 34 times
hokuto
Prole
Posts: 18
Joined: Thu Mar 30, 2023 4:17 pm

Re: problems with oop and inheritance

Post by hokuto »

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...

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".
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.
Thank you very much for the link, I'll take a look. :awesome:
hokuto
Prole
Posts: 18
Joined: Thu Mar 30, 2023 4:17 pm

Re: problems with oop and inheritance

Post by hokuto »

_JM_ wrote: Fri Mar 31, 2023 1:13 pm Hi, I made a little improvement in the code, shown below

Code: Select all

Disparo = setmetable({}, Padre)
With this change, the Disparo class is now able to use the Padre's class methods the way you intended
Thank you very much for the help, now it works perfectly. :awesome:

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.
hokuto
Prole
Posts: 18
Joined: Thu Mar 30, 2023 4:17 pm

Re: problems with oop and inheritance

Post by hokuto »

I missed a comment and when I translated it with the translator I could understand it.

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
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).
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 14 guests