sebas77/Svelto.ECS

SwapEntityGroup does not work in entity pool scenario

Closed this issue · 3 comments

uds commented

I'm trying to build an entity pool using SwapEntityGroup method.

  1. I'm creating an entity in group of "active entities"
  2. I'm swapping this entity to group of "pooled entities", e.g. "pooling" operation
  3. I'm swapping entity back from "pooled entities" group back to "active entities" group, e.g. "un-pooling"

The 3rd step will fail deep inside during lookup in one of the internal entityview lists.
Here is a pseudo code of my scenario

var entityId = 1
var activeEntitesGroup = 101;
var pooledEntitesGroup = 102;

entityFactory.BuildEntityInGroup(entityId, activeEntitiesGroup, ...);

entityFunctions.SwapEntityGroup(entityId, activeEntitiesGroup, pooledEntitesGroup);

entityFunctions.SwapEntityGroup(entityId, pooledEntitesGroup, activeEntitiesGroup);

As far as I can see this is the conceptual problem with the way how the EGID.GID is used and fact that group ID of the entity does not change after the swap into another group (apparently in attempt to keep GID stable).

I was able to make the above scenario to work by making EGID.GID independent from group ID. I simply made GID to return value of entityID (assuming that entityID should be globally unique) and added groupID as a standalone member variable in EGID struct. This allows to keep EGID.GID stable even if the group of entity has been changed.

The second step I did is to change the group ID of the swapped entity during the swap.
Admittedly, this is not very clean way as it requires to change EGID of the entity. But I think it's important to keep group ID in sync with the group it belongs.

Perhaps it's worth considering to decouple groupID from EGID altogether which will allow it's clean change during the group swap operations?

the EGID design is still under consideration, but the fact that the groupID doesn't changer after the swap sounds like a bug. I didn't use the swap operation much so far, but if you write a unit test to repro the bug for me, I will add it in the unit tests repository.

I confirm there were many problems with the function, I am fixing it and adding a pooling example for the next version.

this is fixed in the entityviewsasstruct branch. Still pending unit testing.