In my game, i use one vector to store all my objects. At max, there might be around 100 objects contained within the vector, so it's not that crowded. But! I've noticed an issue while adding objects to my vector. Sometimes, when adding many objects right after eachother, my game crashes. After a little debugging, i traced the crash down to this function:
baseObject *object; vector<baseObject *>::iterator collisionIndex, endVector; /* Set the end */ endVector = this->lists.end(); /* Iterate over all objects in the list */ for (this->listsIndex = this->lists.begin(); this->listsIndex < endVector; this->listsIndex++) { /* Assign the object */ object = *this->listsIndex; /* Check for collision */ for (collisionIndex = this->lists.begin(); collisionIndex < endVector; collisionIndex++) { /* We can't collide with our selfs */ if (object == *collisionIndex) continue; /* Check for collision THIS IS WHERE IT CRAHES */ object->defaultCollision(*collisionIndex); } }
When a bullet collides with an enemy, it creates an explosion of particles. What i thought, was that since the particles got added to the same list i'm allready iterating through, something nasty would happen and crash. You may see that i've tried to fix this by setting the endVector before going through my list (I push_back() objects), and thus not accessing the new objects in this iteration. But that did not fix it.
So i am out of ideas. Help, please? :)