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?