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)
:: johanp

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 2019-12-13 20:38 | 0.009s | 4194k | Q:14