Hello, I hope I'm not double posting. I searched the forum for an answer to my question bu couldn't find one so I'm making my own post.
I'm used to programming, but this is my first project with Lua and the Playdate SDK, so I'm a bit lost with the OOP and extends mechanisms. (In case you'd like to know, i'm working on a simple space invaders clone to learn to make games on playdate )
I have an entity class, which is basically a class that draws rectangle at given positions for now:
class('Entity').extends()
function Entity:init(rect, speed, alive)
self.rect = rect
self.speed = speed
self.alive = alive
end
It may not be very elegant, but it works.
What I would like to do, is to create a subclass called Placeholder of this Entity class, that will be able to perform operations to the rect field of Entity.
So I created my subclass:
class('Placeholder').extends(Entity)
function Placeholder:init(rect, speed, alive, padding)
Placeholder.super.init(self, rect, speed, alive)
self.base_width = base_width
self.base_height = base_height
self.entities = {}
self.entities_number = 0
self.horizontal_movement = 1
end
function Placeholder:add_entity()
x = self.rect.x + self.entities_number * (self.base_width + self.padding)
y = self.rect.y
rect = playdate.geometry.rect.new(x, y, self.base_width, self.base_height)
e = Entity(rect, self.speed, self.alive)
self.entities[self.entities_number] = e
self.entities_number += 1
self.width += self.base_width + self.padding
end
Halas, doing this make my game crash on startup, with the following error:
placeholder.lua:13: attempt to index a nil value (field 'rect')
stack traceback:
placeholder.lua:13: in method 'add_entity'
main.lua:52: in main chunk
I think that either accessing a field of the superclass is not possible, either I'm not doing it correctly.
I didn't saw anything that helped in the Object Oriented Programming section of the documentation, so I thought that would be trivial.
Does someone know if what I want to do is doable ?
Thanks you in advance