Add entities programatically into a prototype
Posted: Tue May 22, 2018 2:57 pm
Hi,
I am trying to attach LED entities to a prototype entity. The prototype entity is specified in the .argos file as a box and a loop function should get all prototype entities, and attach LEDs to them so that when a prototype moves, the LED moves with it. The code I currently have a) doesn't attach the LED to the prototype properly - when the prototype moves, the LED doesn't and b) crashes when you close the ARGoS window with segmentation fault (although the simulation runs normally).
I would like to know how to properly attach the LED and what to do in the Destroy function (perhaps) in order to prevent the crash. The code is:
As you can see, the LEDs are stored in a vector of LED pointers (is this the right thing to do?) The problem I think is the line prototypeEntity.AddComponent(*light);
Thanks!
I am trying to attach LED entities to a prototype entity. The prototype entity is specified in the .argos file as a box and a loop function should get all prototype entities, and attach LEDs to them so that when a prototype moves, the LED moves with it. The code I currently have a) doesn't attach the LED to the prototype properly - when the prototype moves, the LED doesn't and b) crashes when you close the ARGoS window with segmentation fault (although the simulation runs normally).
I would like to know how to properly attach the LED and what to do in the Destroy function (perhaps) in order to prevent the crash. The code is:
Code: Select all
void MainLoopFunctions::Reset() {
int i,w;
//-- attach leds to distinguish between targets
std::string entityId;
w = 0;
CLEDMedium& ledMedium(GetSimulator().GetMedium<CLEDMedium>("leds"));
CSpace::TMapPerType& targets = GetSpace().GetEntitiesByType("prototype");
for (i=0; i<targets.size(); i++) {
std::ostringstream oss;
oss << "target" << i;
CPrototypeEntity& prototypeEntity = *any_cast<CPrototypeEntity*>(targets[oss.str()]);
entityId = "light" + std::to_string(i) + "-" + std::to_string(w);
CLightEntity* light = new CLightEntity(entityId, CVector3(-0.25,0.1,0.1), CColor::RED, 3);
light->SetMedium(ledMedium);
GetSpace().AddEntity(*light);
ledMedium.AddEntity(*light);
prototypeEntity.AddComponent(*light); // IS THIS WRONG? The LED doesn't move with the box. Also, when this is commented out, the crash at close doesn't occur anymore.
w++;
}
}
void MainLoopFunctions::Destroy() {
for (int i=0; i<lights.size(); i++) {
lights[i]->GetParent().RemoveComponent(lights[i]->GetId());
delete lights[i];
}
}
Thanks!