Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

How to iterate over all other ComponentDatas in a job?

Discussion in 'Data Oriented Technology Stack' started by jni97, Apr 12, 2018.

  1. jni97

    jni97

    Joined:
    May 26, 2015
    Posts:
    2
    Hi,
    I'm trying to create a basic flocking behaviour in the ECS & Job System.
    However, I can't figure out, how I can use all ComponentDatas of one type in a job.

    This is my 'Boid' Data:
    Code (CSharp):
    1. [System.Serializable]
    2. public struct Boid : IComponentData
    3. {
    4.  
    5.     public Vector3 position;
    6.     public Vector3 direction;
    7.  
    8. }
    This is my JobComponentSystem:
    Code (CSharp):
    1. public class BoidSystem : JobComponentSystem
    2. {
    3.  
    4.     struct BoidCalculationJob : IJobProcessComponentData<Boid>
    5.     {
    6.         public float deltaTime;
    7.  
    8.         public void Execute(ref Boid data)
    9.         {          
    10.             // I WANT TO ITERATE OVER ALL BOIDS HERE TO DO CALCULATIONS BASED ON THE DATA OF OTHER BOIDS
    11.         }
    12.     }
    13.  
    14.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    15.     {
    16.         Debug.Log(boidGroup.boids[0]);
    17.         var job = new BoidCalculationJob()
    18.         {
    19.             deltaTime = Time.deltaTime
    20.         };
    21.         return job.Schedule(this, 64, inputDeps);
    22.     }
    23.  
    24. }
    I already tried to add a ComponentDataArray to the BoidCalculationJob struct, but however I tried it, it resulted in errors.
    So, how do I pass or access all Boids to the Job?
     
  2. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    528
    This code will do the job

    Code (CSharp):
    1.  
    2. public class BoidSystem : JobComponentSystem {
    3.     struct BoidCalculationJob : IJobParallelFor {
    4.         public float deltaTime;
    5.         //[WriteOnly] // if you  only write
    6.         //[ReadOnly] // if you only read    
    7.         //[NativeDisableParallelForRestriction] // required if you write
    8.         public ComponentDataArray<Boid> boids;
    9.         public void Execute(int index) {
    10.             // do what you wanna do ...    boids[index]
    11.         }
    12.     }
    13.  
    14.     struct BoidsData {
    15.         public int Length; //is determined internally by unity
    16.         //[WriteOnly] // if you only write
    17.         //[ReadOnly] // if you only read
    18.         public ComponentDataArray<Boid> boids;
    19.     }
    20.     [Inject] BoidsData boidData;
    21.     protected override JobHandle OnUpdate(JobHandle inputDeps) {
    22.  
    23.         var job = new BoidCalculationJob() {
    24.             deltaTime = Time.deltaTime,
    25.             boids = boidData.boids
    26.         };
    27.         return job.Schedule(boidData.Length, 1, inputDeps);
    28.     }
    29. }
    30.  
     
    Krajca likes this.
  3. jni97

    jni97

    Joined:
    May 26, 2015
    Posts:
    2
    Thank you for your help! It's working now.
     
    Spy-Shifty likes this.