Jump to content
  • Advertisement
Sign in to follow this  
Stellar Developer

Removing an item from stl::list?

This topic is 4613 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

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.. game_loop { render(); for (all entities) { if (pEntity->bInactive) { xWorld.erase( pEntity ); } } } -sd

Share this post


Link to post
Share on other sites
Advertisement
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
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
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
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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!