Search Unity

Most Efficient Way to Change/Remove Shared Components?

Discussion in 'Entity Component System' started by spectre1989, Mar 11, 2020.

  1. spectre1989

    spectre1989

    Joined:
    Oct 6, 2009
    Posts:
    125
    Hi all,

    I was wondering about an efficient way to change or remove a shared component? I read that doing this per entity basically moves the entity to a different chunk, whereas I want to change/remove it for all entities with that specific shared component. Is this doable?

    Cheers
     
  2. Sarkahn

    Sarkahn

    Joined:
    Jan 9, 2013
    Posts:
    440
    You can create an entity query of your shared component, then add a filter with
    eq.SetSharedComponentFilter()
    , this will make it so your query only matches entities with that scd value. You can then pass that query to entityManager.RemoveComponent or ecb.RemoveComponent.
     
  3. spectre1989

    spectre1989

    Joined:
    Oct 6, 2009
    Posts:
    125
    So using an entity query won't cause it change the the shared component for each individual entity (and therefore moving them to a new chunk) but rather set it once per chunk?
     
  4. Sarkahn

    Sarkahn

    Joined:
    Jan 9, 2013
    Posts:
    440
    As far as I know using an entity query causes it to do batch operations which are much more efficient, yes. Although now that I think about it it may not apply the filter when passing the query, you'd have to test it. If not using a command buffer to defer the structural changes is your best bet.

    Also I'm not sure if I misunderstood your question but just to be clear it will still move your entities to a new chunk if you change or remove their SCD component, that's not something you can avoid. But batch operations are the fastest way to do that.
     
    Last edited: Mar 12, 2020
  5. spectre1989

    spectre1989

    Joined:
    Oct 6, 2009
    Posts:
    125
    So let's say I just want to remove the shared component for all entities in the batch, it can't just remove it from the batch, it still has to move all entities to a new chunk?
     
  6. Sarkahn

    Sarkahn

    Joined:
    Jan 9, 2013
    Posts:
    440
    Yes. This is the foundation of Unity's ECS. Which chunks entities exist in is a product of their archetype. Their archetype is defined by the the components they have and the values of their shared components. When you change their archetype, they are moved to a new chunk.
     
    spectre1989 likes this.
  7. spectre1989

    spectre1989

    Joined:
    Oct 6, 2009
    Posts:
    125
    Thanks
     
  8. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    IIRC it's a bit smarter, if all antities in chunk change their archetype (for example from all entities in chunk removed same component) they not move anywhere and chunk just change it's archetype header, or in case if most part of entities in chunk change archetype they stay in current chunk and chunk changes it's own archetype (because they bigger part of this chunk) and other entities (smallest part which wasn't changed) moves to new chunks. It was long time ago when it was discussed on forum with @5argon, Unity can confirm or discard my assumptions, @Joachim_Ante ? :)