EcsRx / ecsrx

A reactive take on the ECS pattern for .net game developers
MIT License
520 stars 36 forks source link

fix: entity disposal when an observable group is created late #32

Closed Fijo closed 2 years ago

Fijo commented 2 years ago

This was one of the issues I was encountering with my game after updating from EcsRx 5.0.110 to 6.0.131. Executing dispose on an entity was leading causing an KeyNotFoundException in CollectionObservableGroupTracker.GetState(int entityId).

I was eventually able to narrow the issue down to the fact that certain ObservableGroups were created after some of the entities they would end up watching were already present. It turns out there was a discrepancy between what the CollectionObservableGroupTracker was expecting from the constructor parameter initialEntities and what it was being provided with.

In contrast to the previously it was expecting all entities of the targeted entity collections. However it was only provided with already matching entities, which lead to it trying to later access the state of some entities which weren't matching but should have been present in EntityIdMatchTypes as GroupMatchingType.NoMatchesNoExcludes or something similar.

As this issue requires different services to work together it ended up adding an integration test to detect that issue. I also made the required changes so that test ends up passing now.

Sorry for the noise due to my trailing whitespace removal plugin. Let me know if it bugs you and I can get rid of it.