Search Unity

How to process two different chunks with same ISharedComponentData in same or 2 jobs?

Discussion in 'Entity Component System' started by IcenEdel, Nov 20, 2018.

  1. IcenEdel

    IcenEdel

    Joined:
    Jun 18, 2017
    Posts:
    4
    Hello. I'm currently struggling with a chunk iteration problem.

    I have two different archetypes named bullets and bullet systems. Every active bullet entity in the scene is "paired up" with a bullet system through an ISharedComponentData, but the bullet system itself does not keep track of the bullet entity through a buffer/list/array/queue.

    I'm trying to use chunk iteration to do some additional processing on the bullet system based on an arbitrary amount of active bullets and systems by using the ISharedComponentData as the query. But I receive two different chunks according to the debugger(which I had expected to fail but tried to do anyways), so I'm kind of lost as how to link the two chunks data together.

    Here is some stripped down code:

    Code (CSharp):
    1.     public ComponentGroup EmitterQuery;
    2.  
    3.     protected override void OnCreateManager() {
    4.  
    5.         var archeTypeQuery = new EntityArchetypeQuery() {
    6.  
    7.             All = new ComponentType[] {
    8.                 typeof(BulletSystemIdentity),
    9.  
    10.  
    11.             },
    12.  
    13.             Any = new ComponentType[] {
    14.                 typeof(BulletComponent),
    15.                 typeof(EmitterComponent),
    16.             },
    17.  
    18.             None = new ComponentType[] {
    19.  
    20.                 typeof(FinishedState),
    21.             }
    22.  
    23.         };
    24.  
    25.         EmitterQuery = GetComponentGroup(archeTypeQuery);
    26.  
    27.         base.OnCreateManager();
    28.     }
    29.  
    30.     protected override void OnDestroyManager() {
    31.  
    32.         base.OnDestroyManager();
    33.     }
    34.  
    35.     [BurstCompile]
    36.     struct CalculateJob : IJobChunk {
    37.  
    38.        [ReadOnly] public ArchetypeChunkSharedComponentType<BulletSystemIdentity> bulletSystemIdentities;
    39.        [ReadOnly] public ArchetypeChunkComponentType<BulletComponent> bulletComponents;
    40.  
    41.        public ArchetypeChunkComponentType<EmitterComponent> emitters;
    42.  
    43.         public void Execute(ArchetypeChunk chunk, int chunkIndex) {
    44.  
    45.             var bulletComponentsChunks = chunk.GetNativeArray(bulletComponents);
    46.          
    47.             var emitterChunks = chunk.GetNativeArray(emitters);
    48.  
    49.  
    50.             //two different chunks, what to do here....
    51.             //
    52.  
    53.  
    54.         }
    55.     }
    56.  
    57.     protected override JobHandle OnUpdate(JobHandle inputDeps) {
    58.  
    59.      
    60.         var activeBulletsData = EntityManager.GetArchetypeChunkComponentType<BulletComponent>(true);
    61.  
    62.         var emitterData = EntityManager.GetArchetypeChunkComponentType<EmitterComponent>(false);
    63.  
    64.         var sharedBulletSystemIdentities = EntityManager.GetArchetypeChunkSharedComponentType<BulletSystemIdentity>();
    65.  
    66.         var calculateJob = new CalculateJob {
    67.  
    68.             bulletComponents = activeBulletsData,
    69.             bulletSystemIdentities = sharedBulletSystemIdentities,
    70.             emitters = emitterData,
    71.  
    72.         };
    73.  
    74.         return getBulletCountsJob.Schedule(EmitterQuery, inputDeps);
    75.  
    76.     }
    77. }
    I'm considering using IJobParallelFor/Filter, but I don't know how to do that to be honest. I couldn't find any example code on it