Okay another appallingly stupid question.
I’m dealing with tables of items in my game. I have army guys (units) who get killed during a level, then the next level I need to do some housework, clean out the dead wood from the tables etc. Then carry on.
When a unit dies, it is set to unit[i].isDead=TRUE and it’s physics body is removed and all its graphics set to alpha=0.
At the end of a level I have a ‘purge’ which goes through the unit table, picks out all the unit[i].isDead=TRUE guys, nils all their content and then removes the table entry. (My FOR loop counts downwards…btw ;) )
The very next bit of code in my purge function re-numbers all the guys in the table so their table key matches their ID number: (e.g.: unit[i].myNumber = i)
Then, the next level begins.
At the moment when I process the unit table, I’m flipping between: for i = 1,#unit do and **for i=table.maxn(unit) do ****…**but I’m not I’m not sure which I should be using! As far as I understand it, using #table in a for loop results in the code counting all the table entries that contain something…UNTIL it hits a nil entry? (which could mean units after that nil entry wouldn’t be processed?)
While the table.man method is more thorough and goes through every entry even though it might have nothing (a nil) in it. Is this right? I’ve looked for more info on #table but can’t find anything that explains what exactly the # means in terms of running through a table.
Grateful for any help here.
best
Alex
