Log In  


Cart #aa-0 | 2024-12-07 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA

issue

i'm trying out some object oriented programming but i've run into some confusion
when i make a value in a list derived from another list

class={
 new=function(self,tbl)
  tbl=tbl or {}
  setmetatable(tbl,{
   __index=self
  })
  return tbl
 end,
}

entity=class:new({
 --dsp is read by the main upd
 --stands for despawn
 --removes the entity from the
 --list if true
 --dsp=false,
 --dspwait=false,
 x=0,
 y='smelly',
 --o is for origin
 ox=0,
 oy=0,
 sprn=1,
 --s is for size
 --the size of the spr that is
 xs=1,
 ys=1,
 --a is for animation
 asprnum=2,
 --asp is how much to divide
 --the tmr by
 --so 1 is every frame,
 --2 is evey other frame,
 --3 is every third frame,
 --etc.
 aspd=1,

 --update=function()
  --if x< camera.x+8 or x> camera.x+128 then
   --if 
    --dsp = true

  --end
 --end,

 draw=function()
  spr(sprn,x,y)
  print(y)
 end,

})

and then try to reference a value from that same table from a function (as seen in the draw function
in the entity class) it spits out nil. i have the entire cart up above as you likely have already seen, ignore that it's trying to draw a sprite at a position thats text. i set y to smelly for testing so i could easily see if it was spitting out smelly or it was spitting out nil.

wait you can paste pico-8 sprites in here
[8x8]



1

sprn is nil because you're not actually referencing a value from the table, you're referencing the global variable sprn which does not exist. You'd want to use self.sprn instead, and set the draw function like draw = function(self). Then, instead of calling thing.draw(), call it with a colon instead of a dot: thing:draw(). This is equivalent to calling thing.draw(thing), but it's neater.



[Please log in to post a comment]