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 part is solved:

  • 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?

EDIT: NWM, I realize now that what I do get is {__index=_𝘦𝘯𝘷}, not sure why it appeared as nil first, maybe I did something wrong, but:
if getmetadata(e).__index == parentclass then
worked just fine

  • 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?




[Please log in to post a comment]