Sign in to follow this  

How to remove class/sprite

This topic is 2567 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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++;
}
}

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 2567 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this