Search Unity

How to batch remove multiple component types from a group

Discussion in 'Entity Component System' started by joseph-t83, May 23, 2019.

  1. joseph-t83

    joseph-t83

    Joined:
    Mar 28, 2014
    Posts:
    22
    Anybody have any idea how to batch remove two components from a group? If I try either of the following only the first component gets removed.
    Code (CSharp):
    1. EntityManager.RemoveComponent(m_Group, typeof(Component1));
    2.             EntityManager.RemoveComponent(m_Group, typeof(Component2));
    3.  
    4. //||
    5.  
    6.             var componentTypes = new ComponentTypes(typeof(Component1), typeof(Component2));
    7.             EntityManager.RemoveComponent(m_Group, componentTypes);
     
  2. Singtaa

    Singtaa

    Joined:
    Dec 14, 2010
    Posts:
    492
    After the first line, the query will no longer be matched for your 2nd line.

    Code (CSharp):
    1. EntityManager.RemoveComponent(m_Group, typeof(Component1));    // Archetype will be changed after this
    2. EntityManager.RemoveComponent(m_Group, typeof(Component2));    // No entities will be matched by the query here
    And the ComponentTypes version is basically doing the same thing under the hood.

    Code (CSharp):
    1. public void RemoveComponent(EntityQuery entityQueryFilter, ComponentTypes types)
    2. {
    3.     if (entityQueryFilter.CalculateLength() == 0)
    4.         return;
    5.  
    6.     // @TODO: Opportunity to do all components in batch on a per chunk basis.
    7.     for (int i = 0; i != types.Length; i++)
    8.         RemoveComponent(entityQueryFilter, types.GetComponentType(i));
    9. }
    So you probably need an additional different query to batch remove the 2nd comp.
     
  3. joseph-t83

    joseph-t83

    Joined:
    Mar 28, 2014
    Posts:
    22
    That's what I figured was happening. It kind of defeats the purpose of having a ComponentTypes overload for queries then. I guess I'll do it per entity till the batch works. Thanks.
     
    Last edited: May 24, 2019
    Singtaa likes this.
  4. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,194
    I am not sure if they fixed this in a recent release, but from my test it looks like it is fixed. Can anyone else confirm?