You can remove them while iterating through. What I was asking is do you have something else like a collision detection event, a runtime listener or something that could be removing items for you while you are trying to remove them.
I’m using my own timer solution library, so it’s iterating through my list of timers. I’m also stopping a timer sometimes when it’s iterated. I checked into my program, and here’s the result:
[lua]
Eternal timer running to fire shots from a turret
- Within the fire function, it first checks a flag to see whether it should stop on this shot
- If flag is true, stops the timer and resets the flag
-> Error is thrown
- Otherwise proceeds
- Otherwise it fires
End of fire function
[/lua]
So if removing the index (stopping the timer) within the iteration isn’t something you’re supposed to do, how would I achieve this?
Be good to see some code - the only thing I can guess at this stage is whether you’re facing the “iterate-backwards” approach required when deleting entries from a table.
I think most people would use an integer index and use traditional for i = 1, tablesize do / end type constructs to manage those types of loops. Key-value pairs are good when you have a natural index that’s not a number, but it sounds like for what your doing the index doesn’t matter.
You can add things to the end of a table pretty easy using the # operator (gets table length):
mytable[#mytable+1] = value
Then to remove an entry, you can use table.remove… for instance to remove the 3rd entry: table.remove(mytable, 3).
And to iterate over the table (best for removing things is to go backwards):
The only thing is deleting things from the middle of the list and using the # operator to measure the length of the array. If you have an array:
x[1] = 10
x[2] = 15
x[3] = nil
x[4] = “Barney”
x[5] = “Wilma”
Then #x returns 2. The nil stops the counting. There are two solutions. One is to use table.maxn(x) to get the actual count of records and when iterating over it, skip the nil’s or two: table.remove(x, 3) to remove the entry. The later will copy 4 to 3, 5 to 4 to collapse out the hole, so it can be a bit time consuming if you have 100,000 times and you remove #4 for instance.
Of course if you use table.maxn() and you iterate over a list of 100,000 that 90% of them have been removed, that can be inefficient too. So a combination of the two, use maxn() and then periodically purge the nil’s and get a new maxn()
You can remove them while iterating through. What I was asking is do you have something else like a collision detection event, a runtime listener or something that could be removing items for you while you are trying to remove them.
I’m using my own timer solution library, so it’s iterating through my list of timers. I’m also stopping a timer sometimes when it’s iterated. I checked into my program, and here’s the result:
[lua]
Eternal timer running to fire shots from a turret
- Within the fire function, it first checks a flag to see whether it should stop on this shot
- If flag is true, stops the timer and resets the flag
-> Error is thrown
- Otherwise proceeds
- Otherwise it fires
End of fire function
[/lua]
So if removing the index (stopping the timer) within the iteration isn’t something you’re supposed to do, how would I achieve this?
Be good to see some code - the only thing I can guess at this stage is whether you’re facing the “iterate-backwards” approach required when deleting entries from a table.