craftworkgames / MonoGame.Extended

Extensions to make MonoGame more awesome
http://www.monogameextended.net/
Other
1.44k stars 325 forks source link

Entity.Detach() method doesn't invoke EntityChanged until next Update of EntityManager #723

Closed SjaakAlvarez closed 6 months ago

SjaakAlvarez commented 3 years ago

When calling Detach() to remove a component, the entity is not updated until the next Update of the EntityManager.

For example:

When I Detach a component in a EntityProcessingSystem, the component bit is still set in the next EntityDrawSystem Update and the entity is still part of the ActiveEntities which might crash the code.

A quick and dirty solution is to modify EntityManager.OnComponentsChanged to:

private void OnComponentsChanged(int entityId)
{
    _changedEntities.Add(entityId);
    _entityToComponentBits[entityId] = _componentManager.CreateComponentBits(entityId);
    EntityChanged?.Invoke(entityId);
}
AristurtleDev commented 6 months ago

This was resolved in #724