Use Case:
- Use engine.getEntitiesFor(Family...) on input event to remove all entities in the family
Code that fails:
ImmutableArray<Entity> currentGunEntities = getEngine().getEntitiesFor(Family.one(GunComponent.class, WeaponDecorationComponent.class).get());
for(Entity e:currentGunEntities){
getEngine.removeEntity(e);
}
The above code runs without error, but only half of of the entities are removed, as it apperas the underlying Array<> is modified immediately on the removeEntity() call, which appears to cause the iterator to adjust positions.
Work Around for now:
//Declared at System class level
Array<Entity> removableEntities = new Array<Entity>();
//...
//event code
ImmutableArray<Entity> currentGunEntities = getEngine().getEntitiesFor(Family.one(GunComponent.class, WeaponDecorationComponent.class).get());
removableEntities.clear();
for(int i=0;i<currentGunEntities.size();i++){
removableEntities.add(currentGunEntities.get(i));
}
for(Entity e:removableEntities){
getEngine().removeEntity(e);
}
This caught me by surprise, as I expected to be able to trust the ImmutableArray to remain unchanged. I have not tested this same code inside of and update of an EntitySystem, I'm currently doing this on an InputProcessor keydown event. I'm not sure if this is the expected behavior outside of a system update() method.
Use Case:
Code that fails:
The above code runs without error, but only half of of the entities are removed, as it apperas the underlying Array<> is modified immediately on the removeEntity() call, which appears to cause the iterator to adjust positions.
Work Around for now:
This caught me by surprise, as I expected to be able to trust the ImmutableArray to remain unchanged. I have not tested this same code inside of and update of an EntitySystem, I'm currently doing this on an
InputProcessorkeydown event. I'm not sure if this is the expected behavior outside of a systemupdate()method.