Removing an Object from a TLIST

BlitzMax Forums/BlitzMax Programming/Removing an Object from a TLIST

Sean Doherty(Posted 2006) [#1]
What happens if I remove an object from a TLIST inside a update of all TLIST's?

'Remove the Starship from the List.
Method RemoveStarship(pTStarship:TStarship)
	m_pTStarshipList.Remove( pTStarship )				
End Method 


For pTStarship = EachIn m_pTStarshipList
	pTStarship.Update(dTimeStep)
Next


Is it possible the same starship would get updated twice? How does it work when you remove and your iterating through the list?


Tibit(Posted 2006) [#2]
from a TLIST inside a update of all TLIST's?

A list consists of a lot of linked objects, those objects may be Tlists. Is that what you mean? If not, keep reading :)

When you call remove() blitzmax loops the list to find the object to remove and removes that object's link. The each-in loop will not loop an object not in the list.

So:
For pTStarship = EachIn m_pTStarshipList
	pTStarship.Update(dTimeStep)
        If A = 4 Then m_pTStarshipList.Remove(pTStarship)
Next

Would remove pTStarship if A = 4, would be the same if this would have been inside Update(). Remember that removing an object from a list does not remove the object itself. An object can be in more than one list and in several globals and in fields of other objects. Removing it from the list only removes the link the list has to the object.


Sean Doherty(Posted 2006) [#3]
I guess what I'm asking is:

If your cycling through a TLIST and adding entries to the front of the TLIST while removing entries from the center of the TLIST. Would it be possible to call an entry that had been removed?

Maybe I should be cloning the list before I start the update if it's possible that entries will be deleted during the update?


Sean Doherty(Posted 2006) [#4]
Never mind, I think I found the problem. When the starship gets destroyed it is removed from the sector. However, it has already warp to a different sector it cannot be removed. Since therefore it is updated the next time through the loop with some null data.

I will have to reposition the removal code.