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

P#126440 2023-02-28 22:08

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
P#126448 2023-02-28 23:45 ( Edited 2023-02-28 23:45)

thanks! this helps alot

P#126451 2023-03-01 00:11

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-29 12:47:31 | 0.012s | Q:11