Game Development Community

Loop problem deleteObject (logic)

by James Laker (BurNinG) · in Torque Game Engine · 05/04/2007 (4:32 am) · 0 replies

I have the following piece of code:

void GuiCommanderHud::DeleteSpawnPoints()
{

	SimSet * commandermap = dynamic_cast<SimSet*>(Sim::findObject("guiBattleMap"));


	////Loop through all Missiongroup Objects
	for(SimSetIterator itr(commandermap); *itr; ++itr)
	{
		//Appendix A
		Con::printf("(*itr)->getClassName() = %s",(*itr)->getClassName());

		if (dStrstr((*itr)->getClassName(),"guiCommanderRadioCtrl") != NULL)
		{

			//Cast the Iteration Object to a ShapeBase
			guiCommanderRadioCtrl* spawnctrl = static_cast<guiCommanderRadioCtrl*>(*itr);

			commandermap->removeObject(spawnctrl);
			//spawnctrl->setVisible(false);
			spawnctrl->deleteObject();


		}
	}

}

Now what this code does, is it finds all the guiCommanderRadioCtrl in my guiCommanderhud (guiBattleMap) removes it from the Commandermap... and then deletes the object. Or that is the idea.

If I remove the part where it actually deletes it, it will loop through it correctly and Print out the correct amount of guiCommanderRadioCtrls (10).

But when I start deleting it only deletes a few of them. Is this because when deleting one, all the guiCommanderRadioCtrls moves one up in the list? And the loop doesn't take that in account, or am I doing something wrong?

If it moves up, what would be the correct way of removing them all Correctly?

Thanx