Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to calculate EntityCommandBuffer.Concurrent jobIndex with IJobChunk?

Discussion in 'Entity Component System' started by Jay-Pavlina, Jan 26, 2019.

  1. Jay-Pavlina

    Jay-Pavlina

    Joined:
    Feb 19, 2012
    Posts:
    195
    I am unsure of how to calculate the jobIndex needed for EntityCommandBuffer.Concurrent when using an IJobChunk. Does anyone know?

    Code (CSharp):
    1. struct Job : IJobChunk {
    2.     public EntityCommandBuffer.Concurrent commandBuffer;
    3.  
    4.     public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex) {
    5.        var entities = chunk.GetNativeArray(entityType);
    6.        for (var i = 0; i < chunk.Count; i++) {
    7.            var entity = entities[i];
    8.            var jobIndex = ?
    9.            commandBuffer.AddComponent(jobIndex, entity, new Whatever());
    10.        }
    11.     }
    12. }
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,626
    You can just use the chunkIndex.

    Alternatively you can always inject the thread index.

    Code (CSharp):
    1. [NativeSetThreadIndex]
    2. private int ThreadIndex;
    But yeah, just use chunkIndex.
     
    tango209 likes this.
  3. Jay-Pavlina

    Jay-Pavlina

    Joined:
    Feb 19, 2012
    Posts:
    195
    Thanks. Do you know why they provide firstEntityIndex? What is that useful for?
     
  4. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,626
    Not sure, it was a new addition to IJobChunk in p23 so I'm sure they have some type of pattern in mind.
     
  5. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,653
    May be It like chunk.StartIndex which was removed in past I think :)
     
    GilCat likes this.
  6. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    chunkIndex is enough to achieve deterministic playback.