table.remove NOT removing?

I have a game loop, and every 5th cycle through I have the code purge any ‘dead wood’ from my ‘enemyUnit’ table. (any enemyunits that have been killed call a ‘remove’ function that removes all the display objects and sets them to nil. So, below is the ‘clearing out the dead wood’ code in my game loop;

for i = 1,table.maxn(enemyUnit)do

     print(“checking through enemyunit table for key entry”,i)

    if (enemyUnit[i]~=nil)then

          print(i,“is alive”)

          print(“key value”,i,“has a .myNumber value of”,enemyUnit[i].myNumber)

          enemyUnit[i].myNumber=i

          print(“this should now be…”,enemyUnit[i].myNumber)

     elseif (enemyUnit[i]==nil) then

          print(“key entry”,i,“is a NIL, removing entry from table”)

          print (“BEFORE:table.maxn(enemyUnit)=”,table.maxn(enemyUnit))

          table.remove(enemyUnit,i)

          print (“AFTER:table.maxn(enemyUnit)=”,table.maxn(enemyUnit))

     end

end --of for all enemyUnits

When I review the printed lines…it shows the enemyUnit table is NOT decreasing. Can anyone see what I’m doing wrong?

Any particular reason you are trying to remove the enemies from the enemyUnit table after the fact? Why not just immediately remove them from the table once they have been killed and removed from display?

@Alex,

You would be much better off indexing by the object ID like this:

local enemyUnits = {} -- Touch handler to demo concept (not necessarily what you'd do). local function touch( self, event ) if( event.phase == "ended" ) then enemyUnits[self] = nil -- remove entry from table no fuss, no muss display.remove( self ) -- destroy the unit end return true end -- Create 10 enemies for this example for i = 1, 10 do local tmp = display.newCirlc( 50 + 15 \* i, 10, 10 ) tmp.myName = "Enemy " .. i enemyUnits[tmp] = tmp -- store object in table and index it by the object's ID tmp.touch = touch tmp:addEventListener( "touch" ) end

The above technique has the benefit of never creating holes in the table.

You can iterate over the table any time like this:

for k,v in pairs( enemyUnits ) do print(v.myName) end

Any particular reason you are trying to remove the enemies from the enemyUnit table after the fact? Why not just immediately remove them from the table once they have been killed and removed from display?

@Alex,

You would be much better off indexing by the object ID like this:

local enemyUnits = {} -- Touch handler to demo concept (not necessarily what you'd do). local function touch( self, event ) if( event.phase == "ended" ) then enemyUnits[self] = nil -- remove entry from table no fuss, no muss display.remove( self ) -- destroy the unit end return true end -- Create 10 enemies for this example for i = 1, 10 do local tmp = display.newCirlc( 50 + 15 \* i, 10, 10 ) tmp.myName = "Enemy " .. i enemyUnits[tmp] = tmp -- store object in table and index it by the object's ID tmp.touch = touch tmp:addEventListener( "touch" ) end

The above technique has the benefit of never creating holes in the table.

You can iterate over the table any time like this:

for k,v in pairs( enemyUnits ) do print(v.myName) end