So, I'm using this as a base so I don't have to write self everywhere:
As seen 11 minutes into Object Oriented Programming in PICO-8
But because of that, I noticed a few problems:
-
this completely removes any way to get parent by getmetatable? because what I'm getting is just an empty table, which is just it following the _ENV variables all the way down to nothing?
So I would have to make a workaround then? - every time I make a new instance, it triggers init() .. that is cool and all, I tried to change my entity class to include a way to auto add them to the instance list:
init=function(self) add(ins_list,self) end |
but when I then create a new class
powerup=entity:new({})
then it gets added right away
so I tried to make a way to disable it, with this edit to class's new:
new=function(_𝘦𝘯𝘷,tbl,fix) tbl=setmetatable(tbl or {},{__index=_𝘦𝘯𝘷}) if(fix == nil) then tbl:init() end return tbl end, |
so I could create classes without triggering init by using:
powerup=entity:new({},true)
without success, so is there any way to make it ignore it if the fix argument is set?



-
I realize now that what getmetatable returns is the {__index=_𝘦𝘯𝘷} part, not sure why it appeared as nil first, maybe I did something wrong, but:
if getmetadata(e).__index == parentclass then
worked just fine now - And little did I know that I solved my own problem while typing out the code I thought I used, apparently it wasn't the same, and the code works perfectly fine as it is, as I can use the 3rd argument to disable the init.
Isolated test:
So .. now I will continue with my grand project :D take care ya'all



Oh, I wanted my powerup to set a random direction on startup, but also trigger the parent init even .. so in other words, I wanted to make a equivilent for super()
So I ended up with:
powerup=entity:new({ init=function(_𝘦𝘯𝘷) getmetatable(getmetatable(_𝘦𝘯𝘷).__index).__index.init(self) dir=rnd(1) end },0) |
I have no idea why it needed 2 getmetatable.
..but ofc, it is less costly to just do another add(global.list,_𝘦𝘯𝘷)
[Please log in to post a comment]