How to remove class/sprite

Started by
3 comments, last by Ravyne 13 years, 5 months ago
Hi,

On my game i have a vector with all my enemies, and i want that when it collide with some other objects, be removed from vector and destroyed.

My vector:

static std::vector<Enemy*> *enemies;


What i have tested:

void Enemy::remove(){	//GameObjects::enemies->erase(*this);	//delete this;	for (std::vector<Enemy*>::iterator it = GameObjects::enemies->begin(); it!=GameObjects::enemies->end(); ++it) {		if (((Enemy*)*it) == this)		{			GameObjects::enemies->erase(it);		}	}}


But allways that i do it, i get a fatal error.

Im using SFML library.
Advertisement
You need to control the iterator as vector->erase() changes it. Take a look at the docs for vector.erase(iter).

Try something like:
vector<..>::iterator iter = enemies.begin();while( iter != enemies.end() ){  if( *iter == enemyToDelete )  {    iter = enemies.erase(iter);  }  else  {    iter++;  }}

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Hi,

I resolve the problem with:

for (long x = 0; x < (long)GameObjects::enemies->size(); ++x)		{			Enemy *enemy = GameObjects::enemies->at(x);			if (enemy == this)			{				GameObjects::enemies->erase(GameObjects::enemies->begin() + x);			}		}



When i erase an element from vector, it is all removed from the memory?
Yes, but - "the element" is a pointer. The pointer is removed from memory, but the thing it points at is not. This is a very important thing that you should be happy about: something else could be pointing at the same thing, and it would be very bad to take it away!
Quote:Original post by Zahlman
Yes, but - "the element" is a pointer. The pointer is removed from memory, but the thing it points at is not. This is a very important thing that you should be happy about: something else could be pointing at the same thing, and it would be very bad to take it away!


Unless he intends that the vector have ownership of course.

OP: Would I be correct in assuming that Enemy is a base class, or is it just a non-inherited class? I assume that's why you're storing enemy* instead of Enemy. If its a base, make sure your destructor calls in derived classes are virtual and chain the other destructors properly.

Also, a fast way to delete items from a vector is to partition it such that all the active elements are in the front and the ones to be deleted are in the back -- this is what std::remove_if does. Once it's done its work you then delete the items from the back. This method will result in no extraneous copies of data in the vector (granted, its just a pointer so its not much -- but it can add up for this sort of thing) since delete only occurs at the back. If the contents of the vector don't have to maintain the same order (that is, it doesn't have to be "stable") then you can do even better than remove_if by swapping deleted elements with the last element (don't forget to update the logical "last element" when you do this though) -- basically, get an iterator to head and to tail, walk forward from head and when you find something to remove, swap the contents of head and tail, then step tail forward until head and tail meet.

throw table_exception("(? ???)? ? ???");

This topic is closed to new replies.

Advertisement