Removing an entity before it was added leads to unexpected behaviour.
NLogSpace opened this issue · 3 comments
I don't know if this should be considered a bug, but at least for me the behaviour was unexpected.
While the engine was updating, I accidentally called engine.remove(entity);
on an entity that was not yet added to the engine.
Only later I actually added the entity to the engine. When I wanted to remove this entity from the engine (also while the engine was updating), calling engine.remove(entity);
simply had no effect, so the entity just stayed in the engine and was not removed.
I think it is unexpected that calling engine.remove(entity);
does not remove the entity from the engine.
I think I also figured out the reason for this behaviour. Calling engine.remove(entity);
while the engine is updating and before the entity was added to the engine leads to the following: The operation of removing is delayed and we set entity.scheduledForRemoval = true;
. When the removing operation is executed, it has no effect, since the entity is not present in the engine. Later, when the entity is added to the engine, the attribute scheduledForRemoval is still true. When calling engine.remove(entity);
, while the engine is updating, the engine wants to delay the operation of removing the entity, but since scheduledForRemoval is still true, no such operation is added to the array of pending operations.
Some suggestions how this could be improved:
- When an entity is added to the engine, scheduledForRemoval should be set to false.
- When a pending operation of removing an entity is executed, but the entity is not in the engine anymore, scheduledForRemoval should be set to false.
- When
engine.remove(entity);
is called on an entity that is not in the engine, an Exception could be thrown.
Thanks for submitting an issue. I think option 1 seems the most reasonable and what I would expect the behaviour to be.
What do you think?
I agree, the first option seems most reasonable.