Search Unity

Is using IJobForEach_B must call Complete()?

Discussion in 'Entity Component System' started by eterlan, Jul 10, 2019.

  1. eterlan

    eterlan

    Joined:
    Sep 29, 2018
    Posts:
    177
    Hi! I meet this problem several times, whenever I scheduled a IJobForEach_B Job, there would be an exception.
    System.InvalidOperationException : The previously scheduled job System:TestBuffer writes to the NativeArray TestBuffer.Iterator. You must call JobHandle.Complete() on the job System:TestBuffer, before you can read from the NativeArray safely.

    Why was that? I would like to know if there is any way not to call complete with ForEach API.
    Code (CSharp):
    1.     public class System : JobComponentSystem
    2.     {
    3.         protected override JobHandle OnUpdate(JobHandle inputDeps)
    4.         {
    5.             var testBufferJob = new TestBuffer();
    6.             var handle        = testBufferJob.Schedule(this, inputDeps);
    7.             inputDeps = handle;
    8.             return inputDeps;
    9.         }
    10.  
    11.         private struct TestBuffer : IJobForEachWithEntity_EB<Buffer>
    12.         {
    13.             public void Execute
    14.             (Entity                entity,
    15.              int                   index,
    16.              DynamicBuffer<Buffer> b0)
    17.             {
    18.                 if (index == 0) { b0.Add(new Buffer {Value = 1}); }
    19.             }
    20.         }
    21.     }
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    As far as I'm aware, there is a bug with the various IJobForEach_B jobs where if you ever combine them in your project with previous methods of access (from entity etc) you get access violations like this.

    I'd avoid them till next version and stick with buffer from entity.
     
    eterlan likes this.
  3. eterlan

    eterlan

    Joined:
    Sep 29, 2018
    Posts:
    177
    For those meets this problem.

    "System.InvalidOperationException : The previously scheduled job System:TestBuffer writes to the NativeArray TestBuffer.Iterator. You must call JobHandle.Complete() on the job System:TestBuffer, before you can read from the NativeArray safely."

    I guess there is a bug. If I simply add a timer to schedule this kind of job(IJobForEach_B series), everything works fine.