So I have a ComponentSystem that destroys Entities and one that checks components on Entities. The destroy-System runs shortly before the check-System (but both are ComponentSystems, so both on the Main Thread, right?)
Now when I am checking the Components of an Entity that was destroyed the same frame, EntityManager.Exists returns true, but the Component of the Entity I am trying to access already got removed, so I get an ArgumentException: A component with type:Translation has not been added to the entity.
Is this a bug or are there some multithreaded/other things going on that have to be kept in mind when using EntityManager.Exists?
(Using Entities 0.5.0)
Code:
// Runs first
public class DeathSystem : ComponentSystem
{
protected override void OnUpdate()
{
Entities.ForEach((Entity e, ref Health health) =>
{
if (health.Value <= 0)
{
Debug.Log("Entity Died: " + e);
EntityManager.DestroyEntity(e);
// Same result with PostUpdateCommands.DestroyEntity(e)
}
});
}
}
// Runs second
public class DummyKISystem : ComponentSystem
{
protected override void OnUpdate()
{
Entities.ForEach((ref DummyKIComponent dummy, ref Translation translation, ref Rotation rotation, ref MovementSpeed speed) =>
{
if (dummy.CurrentTarget == Entity.Null)
{
...
}
else if (!EntityManager.Exists(dummy.CurrentTarget))
dummy.CurrentTarget = Entity.Null;
else
{
if (!EntityManager.HasComponent<Translation>(dummy.CurrentTarget))
Logger.Log("Blargh! " + dummy.CurrentTarget);
// ... (Access Translation Component)
}
});
}
}
Result: