Log In  

Hi all,
just getting my feet wet with pico-8 even if I've programmed quite extensively over the years. So far I'm having a blast!

Now to the question:

Is it safe to remove elements from a table while traversing it?

foreach(actor,update_actor)

...

function update_actor(a)
 if(a.dead) delete(actor,a)
end

Or does the above skip elements?

Other option would be to mark elements for deletion and then make a delete sweep post the update calls.

Thanks!

P#27544 2016-08-28 12:15 ( Edited 2016-08-28 16:31)

Hi - yes that is safe - I normally use 'del' rather than delete - not sure if they're synonymous, since I'm a lazy programmer and didn't read the manual properly ..

function update_actor(a)
  if(a.dead) then del(actor,a) end
end
P#27545 2016-08-28 12:22 ( Edited 2016-08-28 16:22)

Thanks! Let's hope its just shorthand. :)

P#27549 2016-08-28 12:31 ( Edited 2016-08-28 16:31)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-28 09:00:18 | 0.006s | Q:13