Log In  


So I was building a game that generates 30 stars at random x,y cords using the following snipet, however is returns an error

 stars={}
 for i=1,30 do
  local countup=i
  add(stars,countup)
  add(stars.countup,x)
  add(stars.countup,y)
  add(stars.countup.x,rnd(0,127))
  add(stars.countup.y,rnd(0,127))
 end

Does anyone know why?

Thanks, Bouncy



hello! add is meant to work with tables that act like sequences, meaning that their keys are not strings (like x or y) but sequential numbers (1, 2, etc)

official doc:
https://www.lexaloffle.com/dl/docs/pico-8_manual.html#Tables
https://www.lexaloffle.com/dl/docs/pico-8_manual.html#ADD
(it uses the term «array», which in my opinion is misguided because that means something else in popular low-level language, but anyway)

this is a longer write-up, very useful: https://www.lexaloffle.com/bbs/?tid=44686

so if you want to generate a sequence table containing key-value tables, you should do something like:

stars={}
for i=1,30 do
 add(stars, {x=rnd(127), y=rnd(127)})
 # maybe add flr to make sure the coords are integer, not fractional!
end

thanks! this helps alot



[Please log in to post a comment]