I'm having trouble coming up with logic for removing every enemy object from the scene.

Hello,

    In my project, I’m creating enemies with the function enemy.new. Something like this;

enemyspawntimer = timer.performWithDelay(600, function() Enemy.new() end, 0)

  And those enemies gets destroyed by different stuff during gameplay. But I also want to have a function called removeEnemies() which removes all the enemies. This is the part I’m having problem with. I was thinking about creating a table allEnemies = {} , and adding each enemy as a child as the enemies created, and than loop over it to remove each of them. This kind of doesn’t work because when destroying an enemy with a random event, I can’t remove it from the allEnemies table because I don’t know where it is in that table. So my removeEnemies() function tries to remove enemies that already got removed. (This doesn’t give an error, but, since I’m using #allEnemies, the loop count is always going up even tho there are only a few enemies remaining on-screen)
 

I hope I could express my situation, all help is appreciated! :slight_smile:

try table.indexof( enemyYouWantToDestroy ) to get position in table

You have to be quite careful with tables because you can end up deleting an entry in mid processing.

What I would advise is having a tag entry in your tables - something like __delete and setting this - your code for processing the table can test it and ignore it if needed. 

Then, at the end of your enterFrame event, iterate through the loop removing entries, or if your entries are a dictionary collection rather than an array, just set them to nil.

http://stackoverflow.com/questions/12394841/safely-remove-items-from-an-array-table-while-iterating

This will allow you to do both. Your enemies can be deleted by setting the tag, and you can delete them all by just iterating over them and setting all the tags.

It is generally good practice to only delete stuff in one place - say for example at a later date you added an observer to it, you could remove it in one place but not the other.

Oh wow didn’t know that! That should solve my problems…

Edit: Nope, I don’t think there is ‘indexOf()’ in Corona…

Awesome, thanks it is working.

    for i = #enemies, 1, -1 do                  if (enemies[i].shouldDestroy == true) then             enemies[i]:destroy()             table.remove(enemies, i)         end              end  

But I can’t get it working if I loop from ‘i = 1, #enemies for a reason I can’t understand.

And since I’m removing enemies off-screen, enterframe might be overkill, so I put it in a timer.

Thanks again!

that’s cause when you remove item 1 item 2 is now item 1. if you want to loop forward you only have to remove item 1 each time

try table.indexof( enemyYouWantToDestroy ) to get position in table

You have to be quite careful with tables because you can end up deleting an entry in mid processing.

What I would advise is having a tag entry in your tables - something like __delete and setting this - your code for processing the table can test it and ignore it if needed. 

Then, at the end of your enterFrame event, iterate through the loop removing entries, or if your entries are a dictionary collection rather than an array, just set them to nil.

http://stackoverflow.com/questions/12394841/safely-remove-items-from-an-array-table-while-iterating

This will allow you to do both. Your enemies can be deleted by setting the tag, and you can delete them all by just iterating over them and setting all the tags.

It is generally good practice to only delete stuff in one place - say for example at a later date you added an observer to it, you could remove it in one place but not the other.

Oh wow didn’t know that! That should solve my problems…

Edit: Nope, I don’t think there is ‘indexOf()’ in Corona…

Awesome, thanks it is working.

    for i = #enemies, 1, -1 do                  if (enemies[i].shouldDestroy == true) then             enemies[i]:destroy()             table.remove(enemies, i)         end              end  

But I can’t get it working if I loop from ‘i = 1, #enemies for a reason I can’t understand.

And since I’m removing enemies off-screen, enterframe might be overkill, so I put it in a timer.

Thanks again!

that’s cause when you remove item 1 item 2 is now item 1. if you want to loop forward you only have to remove item 1 each time