Log In  


So, I'm using this as a base so I don't have to write self everywhere:

```
ins_list={}

GLOBAL=_𝘦𝘯𝘷

class=setmetatable({
new=function(_𝘦𝘯𝘷,tbl)
tbl=setmetatable(tbl or {},{__index=_𝘦𝘯𝘷})

tbl:init()

return tbl

end,
init=function()end
},{__index=_𝘦𝘯𝘷})

entity=class:new({
x=0,
y=0,
img_ind=1,
spr_ind={10,12,14,12},
draw=function(_𝘦𝘯𝘷)
img_ind += .08
if(img_ind > #spr_ind +1) img_ind=1
spr(spr_ind[flr(img_ind)],x,y)
end
})
```


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:

list={}

global=_𝘦𝘯𝘷

class=setmetatable({
 new=function(_𝘦𝘯𝘷,tbl,skip)
  tbl=setmetatable(tbl or {},{__index=_𝘦𝘯𝘷})

  if(skip==nil)tbl:init()

  return tbl
 end,
 init=function()end
},{__index=_𝘦𝘯𝘷})

entity=class:new({
 init=function(_𝘦𝘯𝘷)
  add(global.list,_𝘦𝘯𝘷)
 end
},0)

powerup=entity:new({},0)

powerup:new() // list contain only THIS powerup that didn't have another argument

So .. now I will continue with my grand project :D take care ya'all



[Please log in to post a comment]