Original Post
Hi everyone In my game, I have a standard list which I iterate through each update and run each object in the lists update function. The list is part of an object manager, which does things like find targets, store and delete objects etc all from this one list. Unfortunatly I have run into a problem. When an object is destroyed or expires and needs to be removed from the list, it leaves a invalid element where the old element used to be. I have tried swaping and pop_back'ing to no avail. What I would really love to be able to do is to remove an object from the list, and then to update the list so as to no longer contain the invalid element. Heres the code I use to remove the object:
void objectmanager::removeobject(Ogre::String objectname)
{
//broken - its leaving blank feilds in the list, which the update function is hitting and dieing on.
std::list<BaseEntity*>::iterator objectbegin = listofobjects.begin();
for (;objectbegin != listofobjects.end(); objectbegin++)
{
if ((*objectbegin)->getName() == objectname)
{
listofobjects.erase(objectbegin);
std::swap(objectbegin,listofobjects.end());
listofobjects.pop_back();
}
}
}
Heres one of the functions which access's the list:
void objectmanager::updateObjects(float deltaT)
{
std::list<BaseEntity*>::iterator objectbegin = listofobjects.begin();
for (;objectbegin != listofobjects.end(); objectbegin++)
{
{
(*objectbegin)->update(deltaT);
}
}
}
when the update function rolls round to the position of the old data, it hits null values and the whole program dies. Im in dire straights... please help!