Game Development Community

SimGroup/Set Iteration Problem

by Robert DeMaria · in Torque Game Builder · 05/22/2006 (8:43 pm) · 3 replies

I'm currently trying to implement enemies in my game. All the enemies that exist during gameplay are contained in a SimGroup that I iterate over during the "onUpdateScene" callback. To spawn new enemies while the game is running, I detect collisions that occur between the camera and a dummy object. If the camera hits the dummy object, then my "spawnEnemy" function will be called from the "onCollision" callback. The "spawnEnemy" function will ultimately try to add the new enemy to the enemy SimGroup. When this occurs, the physics of my game will mess up. The player will usually get stuck in the floor or hover in the air.

If I don't iterate over the SimGroup each frame, or if I don't add the object to the SimGroup, then the problem will stop occuring (but the enemy AI won't be executed). The problem occurs with both SimGroups and SimSets.

Is there any way to safely add an object to a SimGroup/Set while the set is being iterated over? If not, can anyone think of a way to work around the problem?

#1
05/23/2006 (10:44 am)
One way would be using a temporar simset within the iteration and add all those items from the set to your main simset after the iteration finished.
#2
05/23/2006 (2:35 pm)
Unfortunately this doesn't work. In order to do this, I still have to add the enemy to the temporary SimSet in the "onCollision" callback and iterate over it during the "onUpdateScene" callback to add its elements to the main SimGroup. This results in the same bug.
#3
05/24/2006 (9:28 am)
Never mind, this bug actually had nothing to do with the iteration. A typo I didn't catch caused all my tests to indicate that the iteration was the source of the problem. I should have learned by now not to look for bugs when I need sleep :)