Search Unity

How to handle nested iterations of different entity archetypes?

Discussion in 'Entity Component System' started by Vuh-Hans, Jan 14, 2019.

  1. Vuh-Hans

    Vuh-Hans

    Joined:
    Sep 24, 2013
    Posts:
    42
    Let's say we need to iterate over two or more entity archetypes with different components to related them in some manner. In the generic case:

    Code (CSharp):
    1.  
    2. foreach a in EntityArchetypeA
    3.    foreach b in EntityArchetypeB
    4.       foreach c in EntityArchetypeC
    5.          DoSomething(a, b, c);
    6.  
    This can be easily done in a single-threaded manner using ComponentDataArray and three separate ComponentGroups, one for each entity archetype. You can also setup an IJobParallelFor in the same manner.

    However, since the prevalent feeling here is that IJobProcessComponentData and IJobChunk should be the main workhorse of ECS (and that ComponentDataArray might be deprecated soon), how would you go about doing such nested iterations with them?
     
  2. JooleanLogic

    JooleanLogic

    Joined:
    Mar 1, 2018
    Posts:
    447
    Good timing as tertle just posted an example of this here
     
  3. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    My example is slightly different than just iterating over different archetypes but the idea is the same. You can actually have different archetypes in a single component group

    ComponentGroup GetComponentGroup(params EntityArchetypeQuery[] query)


    And then use the previous mentioned example, separate them by comparing the array size for each chunk.
     
  4. Vuh-Hans

    Vuh-Hans

    Joined:
    Sep 24, 2013
    Posts:
    42
    Thanks!