game_loop
{
render();
for (all entities)
{
if (pEntity->bInactive)
{
xWorld.erase( pEntity );
}
}
}
-sd
Removing an item from stl::list?
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..
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.
Just keep another list, and reserve it for objects you want to destroy.
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.
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
Just my €0.02
Or you can try:
Prototype's solution nicely fits in this one (since remove_if will remove every tagged object).
I know, the syntax looks convoluted ;)
HTH,
// you can use either a function of a functorbool 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,
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.
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
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;}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement