Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Do entities with same sharedcomponentdata value retain their instantiation order?

Discussion in 'Entity Component System' started by yc960, Jul 19, 2018.

  1. yc960

    yc960

    Joined:
    Apr 30, 2015
    Posts:
    228
    Let's say I instantiate entities sharing the same sharedcomponentdata value, do they retain their order when I inject them into systems? If yes, are there situations where their order would be reshuffled somehow, and can I control if that happens?
     
  2. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    556
    never assume entity ordering. ECS can and will reorder things whenever it's convenient, e.g. if you add/remove a component it will remove the data from the old archetype, filling the hole with the entity that is currently last in memory, then add that data at the end of the new archetype
     
    yc960 likes this.
  3. yc960

    yc960

    Joined:
    Apr 30, 2015
    Posts:
    228
    Will add/remove ISystemStateComponentData also trigger reshuffle? It seems to me that ISystemStateComponentData is system bound rather than archetype bound.
     
  4. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    556
    technically is still a component attached to entities, so it should be stored together with the other components.
    the only difference is that it will remain alive when the entity is destroyed, until the owning system removes it.

    if you need to track order of entities, use a component for that, like
    Code (csharp):
    1. public struct InstantiationOrder {public int/*or long*/ value;}
    and the system that instantiate the entities keeps a counter and assigns an increasing value to each entity

    if you just need a persistent way to access the same entity, you can use the entity id for that. check out
    EntityArray
    and
    ComponentDataFromEntity<T>