junkdog / artemis-odb

A continuation of the popular Artemis ECS framework
BSD 2-Clause "Simplified" License
777 stars 112 forks source link

[Question] How to best avoid processing deleted entities #615

Closed Aardvarklord closed 4 years ago

Aardvarklord commented 4 years ago

If I understand correctly entities deleted via world.delete(entityId) are only removed from the list of entities to be processed at the end of a system's process loop over all entities. This means that if I while processing entityA do world.delete(entityB) there will still be one call to system.process(entityB) during the ongoing world.process iteration.

Is there any convenient way of within a system.process call checking whether an entity is scheduled for deletion or do I need to keep track of this myself with some kind of isDead flag in a component on the entity?

genaray commented 4 years ago

If I understand correctly entities deleted via world.delete(entityId) are only removed from the list of entities to be processed at the end of a system's process loop over all entities. This means that if I while processing entityA do world.delete(entityB) there will still be one call to system.process(entityB) during the ongoing world.process iteration.

Is there any convenient way of within a system.process call checking whether an entity is scheduled for deletion or do I need to keep track of this myself with some kind of isDead flag in a component on the entity?

If i understood your question correctly... this is right. The real deletion of an entity is triggered after the system finished. Normally this shouldnt be a problem because your systems are running after each other. That means your next system in queue wont process the deleted entity. In case you still need to check if the entity is queued for deletion you could either check out...

world.getEntityManager().isActive(entityID), write your own "Manager"/"System" for tracking removed entities ( Some sort of list you insert deleted entities by yourself )/DeadFlag or mark the entity with some sort of "Destroy" component and having a "DestroySystem" that takes care of destroying those entities at the end of the frame.

Aardvarklord commented 4 years ago

Thanks genaray, then I will implement some kind of tracking of deleted entities myself. My application is a game/simulation type application where I need to prevent an entity from performing its action if it was killed by a previously processed entity within the same system within the same world iteration. (isActive seems to return true also for deleted entities within the same system and world iteration)