Search Unity

The Previously Scheduled Job Writes To The Nativearray You Must Call Jobhandle.complete()

Discussion in 'Entity Component System' started by Micz84, Apr 11, 2019.

  1. Micz84

    Micz84

    Joined:
    Jul 21, 2012
    Posts:
    451
    I am getting used to ECS after changes (I haven't updated my code for a while) and I am getting exceptions in this simple situation. I have a timer system that decreases time on each timer. And I have a spawner system which spans an entity when time on the timer runs out and resets the timer.
    Code (CSharp):
    1. [UpdateInGroup(typeof(SimulationSystemGroup))]
    2. public class CountdownTimerSystem : JobComponentSystem
    3. {
    4.  
    5.     [BurstCompile]
    6.     struct CountdownTimerSystemJob : IJobForEach<CountDownTimer>
    7.     {
    8.         public float deltaTime;
    9.      
    10.         public void Execute(ref CountDownTimer timer)
    11.         {
    12.             timer.Value -= deltaTime;              
    13.         }
    14.     }
    15.  
    16.     protected override JobHandle OnUpdate(JobHandle inputDependencies)
    17.     {
    18.         var job = new CountdownTimerSystemJob
    19.         {
    20.             deltaTime = Time.deltaTime
    21.         };
    22.      
    23.         return job.Schedule(this, inputDependencies);
    24.     }
    25. }
    Code (CSharp):
    1. [UpdateInGroup(typeof(SimulationSystemGroup))]
    2. [UpdateAfter(typeof(CountdownTimerSystem))]
    3. public class SpawnSystem : JobComponentSystem
    4. {
    5.     private EndSimulationEntityCommandBufferSystem _EndFrameBarier;
    6.  
    7.  
    8.     struct SpawnSystemJob : IJobForEachWithEntity<CountDownTimer, Spawner, Translation>
    9.     {
    10.  
    11.         public int JobIndex;
    12.         public EntityCommandBuffer.Concurrent CommandBuffer;
    13.  
    14.         public void Execute(Entity e, int index, ref CountDownTimer timer, [ReadOnly] ref Spawner spawner, [ReadOnly] ref Translation position)
    15.         {
    16.             if (timer.Value <= 0)
    17.             {
    18.                 timer.Value = spawner.SpawnEvery;
    19.                 var entity = CommandBuffer.Instantiate(index, spawner.Prefab);
    20.                 CommandBuffer.SetComponent(index, entity, new Translation() { Value = position.Value });
    21.             }
    22.         }
    23.     }
    24.  
    25.     protected override void OnCreate()
    26.     {
    27.         base.OnCreate();
    28.         _EndFrameBarier = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
    29.     }
    30.  
    31.     protected override JobHandle OnUpdate(JobHandle inputDependencies)
    32.     {
    33.         var job = new SpawnSystemJob() {
    34.             CommandBuffer = _EndFrameBarier.CreateCommandBuffer().ToConcurrent()
    35.         };
    36.         return job.Schedule(this, inputDependencies);
    37.     }
    38. }
    I am getting this exception:

    ArgumentException: The previously scheduled job SpawnSystem:SpawnSystemJob writes to the NativeArray SpawnSystemJob.Data.CommandBuffer. You must call JobHandle.Complete() on the job SpawnSystem:SpawnSystemJob, before you can write to the NativeArray safely.
    EntityCommandBuffer was recorded in SpawnSystem and played back in Unity.Entities.EndSimulationEntityCommandBufferSystem.
    at (wrapper managed-to-native)

    What am I doing wrong?
     
  2. elcionap

    elcionap

    Joined:
    Jan 11, 2016
    Posts:
    138
    You're missing AddJobHandleForProducer.

    Code (CSharp):
    1.  
    2. protected override JobHandle OnUpdate(JobHandle inputDependencies)
    3. {
    4.     var job = new SpawnSystemJob() {
    5.         CommandBuffer = _EndFrameBarier.CreateCommandBuffer().ToConcurrent()
    6.     };
    7.     inputDependencies =  job.Schedule(this, inputDependencies);
    8.     _EndFrameBarier.AddJobHandleForProducer(inputDependencies);
    9.     return inputDependencies;
    10. }
    11.  
    []'s
     
  3. Deleted User

    Deleted User

    Guest

    Much appreciate the answer. Moving away from [Inject] requires you to add the dependency manually.
     
    Last edited by a moderator: Apr 12, 2019
  4. Micz84

    Micz84

    Joined:
    Jul 21, 2012
    Posts:
    451
    O, I forgot about that thank you :)
     
  5. Spark59op

    Spark59op

    Joined:
    Jan 22, 2021
    Posts:
    3
    I am new to unity, i don't know where to apply this.... It happens to me when i add trail renderer to my bullet