Removing entities sometimes doesn't work
Posted: Tue Dec 10, 2013 4:01 pm
Hi,
I am running argos3 simulation where sometimes I need to remove an entity from a family of entity types. Rarely I get this error:
which quits the application. I am not sure what I am doing wrong. Here is my code for how the entities are created:
where pellets is a vector of pointers to the CBoxEntities.
Then at some point later in the simulation, I randomly remove a pellet:
As you can see, I thought if I specifically double-check if the pellet can actually be found by trying to find it by its id between all box entities, it should be fine. Yet I still get the error. Is there a better way of doing this?
I am running argos3 simulation where sometimes I need to remove an entity from a family of entity types. Rarely I get this error:
Code: Select all
[FATAL] CSpace::RemoveEntity() : Entity "pellet509" has not been found in the indexes.
Code: Select all
std::ostringstream pelletId;
pelletId << "pellet" << pelletIdCounter;
CBoxEntity* pellet = new CBoxEntity(pelletId.str(),CVector3(robotPosition + CVector3(0.1,0.1,0)),CQuaternion(),true,CVector3(0.1,0.1,0.1),0.1);
AddEntity(*pellet);
pellets.push_back(pellet);
pelletIdCounter++;
Then at some point later in the simulation, I randomly remove a pellet:
Code: Select all
if (pellets.size() > 0) {
uint pelletId = uint(randNumGenerator->Uniform(CRange<Real>(0,pellets.size()-1)));
//-- try if it still exists in space indexes
CSpace::TMapPerType& pelletsInSpace = GetSpace().GetEntitiesByType("box");
CBoxEntity* pellet = any_cast<CBoxEntity*>(pelletsInSpace[pellets[pelletId]->GetId()]);
if (pellet != NULL) {
RemoveEntity(*pellet);
}
//-- remove from the array
pellets.erase(pellets.begin()+pelletId);
pellet = NULL;
}