Search Unity

How to query for 2 different entity archetypes in one system?

Discussion in 'Entity Component System' started by Skyblade, Mar 28, 2019.

  1. Skyblade

    Skyblade

    Joined:
    Nov 19, 2013
    Posts:
    77
    i have a JobComponentSystem and use IJobProcessComponentDataWithEntity<Collider> to query for mouse click target Entities.
    Then I need to iterate over another entity archetype with a Spring component
    How can I do that?

    I can not use GetComponentDataFromEntity, because these are completely different entities.
     
  2. Deleted User

    Deleted User

    Guest

  3. Skyblade

    Skyblade

    Joined:
    Nov 19, 2013
    Posts:
    77
  4. Micz84

    Micz84

    Joined:
    Jul 21, 2012
    Posts:
    451
    There is an example. You use two archetype queries and combine them:
    Code (CSharp):
    1. protected override void OnCreateManager()
    2. {
    3.    var query0 = new EntityArchetypeQuery
    4.    {
    5.        All = new ComponentType[] {typeof(RotationQuaternion)}
    6.    };
    7.  
    8.    var query1 = new EntityArchetypeQuery
    9.    {
    10.        All = new ComponentType[] {typeof(RotationSpeed)}
    11.    };
    12.  
    13.    m_Group = GetComponentGroup(new EntityArchetypeQuery[] {query0, query1});
    14. }
     
    Last edited: Apr 4, 2019
    Skyblade likes this.
  5. Skyblade

    Skyblade

    Joined:
    Nov 19, 2013
    Posts:
    77
    Now I have a problem - the systems run for the first time (I can enter OnCreateManager in the debugger), but I don't seen any presense of the system when a program running. Entity debugger shows other systems, but not the system with the IJobChunk.

    My code:
    Code (CSharp):
    1. using Unity.Collections;
    2. using Unity.Entities;
    3. using Unity.Jobs;
    4. using UnityEngine;
    5.  
    6. namespace Ecs
    7. {
    8.     public class ClickHandleSystem : JobComponentSystem
    9.     {
    10.         struct ColliderGroupJob : IJobChunk
    11.         {
    12.             [ReadOnly]
    13.             public ArchetypeChunkComponentType<Collider> Colliders;
    14.             [ReadOnly]
    15.             public ArchetypeChunkComponentType<Node> Nodes;
    16.             [ReadOnly]
    17.             public ArchetypeChunkComponentType<Spring> Springs;
    18.             public ArchetypeChunkComponentType<DirectedConnection> Connections;
    19.  
    20.  
    21.             public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex)
    22.             {
    23.                 var chunkColliders = chunk.GetNativeArray(Colliders);
    24.                 var chunkSprings = chunk.GetNativeArray(Springs);
    25.                 for (int i = 0; i < chunk.Count; i++)
    26.                 {
    27.                     Debug.Log(chunkColliders[i].Radius);
    28.                     Debug.Log(chunkSprings[i].Stiffness);
    29.                 }
    30.             }
    31.         }
    32.  
    33.         ComponentGroup group;
    34.  
    35.         protected override void OnCreateManager()
    36.         {
    37.             var nodes = new EntityArchetypeQuery { All = new ComponentType[] { typeof(Collider), typeof(Node) } };
    38.             var springs = new EntityArchetypeQuery { All = new ComponentType[] { typeof(Spring), typeof(DirectedConnection) } };
    39.             group = GetComponentGroup(new EntityArchetypeQuery[] { nodes, springs });
    40.         }
    41.         protected override JobHandle OnUpdate(JobHandle inputDeps)
    42.         {
    43.             var colliders = GetArchetypeChunkComponentType<Collider>(true);
    44.             var nodes = GetArchetypeChunkComponentType<Node>(true);
    45.             var spring = GetArchetypeChunkComponentType<Spring>(true);
    46.             var connections = GetArchetypeChunkComponentType<DirectedConnection>(false);
    47.             return new ColliderGroupJob {
    48.                 Colliders = colliders,
    49.                 Connections = connections,
    50.                 Nodes = nodes,
    51.                 Springs = spring
    52.             }.Schedule(group, inputDeps);
    53.         }
    54.     }
    55. }
    Am I missing something?
     
  6. Micz84

    Micz84

    Joined:
    Jul 21, 2012
    Posts:
    451
    I do not know why it is not showing but I think you have an error in the job. You are aware that some of your chunks may not contain both components? You should check if this chunk contains them.
     
    Skyblade likes this.
  7. Skyblade

    Skyblade

    Joined:
    Nov 19, 2013
    Posts:
    77
    Thank you. There was type mismatch in my code, so system failed to execute.

    The last (I hope) question: what is the difference between chunk.Has() and chunk.HasChunkComponent()?
    Both methods receive the same arguments and return bool. But Has() works while HasChunkComponent() is not.
     
  8. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    Has check existance type in archetype in which chunk presented. HasChunkComponent check existance of component on chunk (now we have components associated not only with entity but with chunk too)
     
    WAYNGames and Skyblade like this.
  9. Skyblade

    Skyblade

    Joined:
    Nov 19, 2013
    Posts:
    77
    How can I know that I finished processing all entitiy archetypes when using IJobChunk?
    I don't see any advantage over two separate systems in my case.