Sign in to follow this  
Stellar Developer

Removing an item from stl::list?

Recommended Posts

Im using an stl list to store my entities. I want the entities to be able to remove themselves from the list after they're done/dead. I dont know how, but I want to the entity to call a self member RemoveSelf() in which, it will be deleted from the world entity list. my current implementation is below, but I think its overkill and requires allot of iteration when lotts of entities are at hand. can anyone help me with a better implementation? currently i accomplish this by.. [code] game_loop { render(); for (all entities) { if (pEntity->bInactive) { xWorld.erase( pEntity ); } } } [code] -sd

Share this post


Link to post
Share on other sites
stylin    758
Looks fine to me. Are you have noticeable speed issues with this section of code? Allowing objects to delete themselves from the list might be the overkill you're looking to avoid. Don't you have to iterate through the whole list anyway?

Just keep another list, and reserve it for objects you want to destroy.

Share this post


Link to post
Share on other sites
mcguile25    235
You would have to have some way for each list node to know what list it's in or have some function available for it to call when it needs to be cleaned up. If you're storing class instances for the list nodes, you should be able to have a pointer/reference to the parent list inside of each node.

Share this post


Link to post
Share on other sites
Prototype    2014
I usually flag the objects as 'dead' using a member variable, and delete them the next time the list is traversed. The advantage is that dead elements remain in the container for another frame so other objects depending on it (e.g. by having a pointer) can take proper action instead of having to handle a deleted object.

Just my €0.02

Share this post


Link to post
Share on other sites
Or you can try:

// you can use either a function of a functor
bool is_inactive(Entity *e)
{
return e->bInactive;
}
// ...
xWorld.erase(std::remove_if(xWorld.begin(), xWorld.end(), is_inactive), xWorld.end());


Prototype's solution nicely fits in this one (since remove_if will remove every tagged object).

I know, the syntax looks convoluted ;)

HTH,

Share this post


Link to post
Share on other sites
SiCrane    11839
Quote:
Original post by Emmanuel Deloget
I know, the syntax looks convoluted ;)

You don't need to use the erase-remove idiom with std::list. std::list has a remove_if() member function.

Share this post


Link to post
Share on other sites
thx for the replies.

works great :D very clean ^^

not that it really matters, but here is the code I used...
I had to account for cleanup


xEntities.xWorld.remove_if( IsEntityInactive );

bool IsEntityInactive(RenderableEntity *e)
{
bool bInactive = false;

if ( e->IsInactive() == true )
{
bInactive = true;
delete e;
}

return bInactive;
}

Share this post


Link to post
Share on other sites

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