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

Resolved Why I need to call jobhandle.complete,and use the job1.jobhandle as the job2's dependency not work

Discussion in 'Entity Component System' started by yifanchu183, Jan 21, 2023.

  1. yifanchu183

    yifanchu183

    Joined:
    Jul 4, 2019
    Posts:
    41
    Here is the error:
    InvalidOperationException: The previously scheduled job CalculateCostJob writes to the BufferTypeHandle<CellBuffer> CalculateCostJob.cells. You must call JobHandle.Complete() on the job CalculateCostJob, before you can read from the BufferTypeHandle<CellBuffer> safely.
    Unity.Entities.WorldUnmanagedImpl+UnmanagedUpdate_000014A0$BurstDirectCall.Invoke (System.Void* pSystemState) (at <4a8bc4d753ec4ca78e91db456a7e73fb>:0)

    And Here is the update code
    Code (CSharp):
    1.  
    2. [BurstCompile]
    3.     public void OnUpdate(ref SystemState state)
    4.     {
    5.         var flowFieldSetting = SystemAPI.GetSingleton<FlowFieldSettingData>();
    6.         var cellBuffer = SystemAPI.GetSingletonBuffer<CellBuffer>().Reinterpret<CellData>();
    7.  
    8.         state.EntityManager.CompleteDependencyBeforeRO<PhysicsWorldSingleton>();
    9.         state.EntityManager.CompleteDependencyBeforeRO<LocalTransform>();
    10.  
    11.         localTransformList.Update(ref state);
    12.         physicsMassList.Update(ref state);
    13.  
    14.         var settingData = SystemAPI.GetSingleton<FlowFieldSettingData>();
    15.  
    16.         var costJob = new CalculateCostJob()
    17.         {
    18.             cells = cellBuffer.AsNativeArray(),
    19.             physicsWorld = SystemAPI.GetSingleton<PhysicsWorldSingleton>().PhysicsWorld,
    20.             pgaInms2 = SystemAPI.GetSingleton<TimerData>().curPGA * Constants.gravity,
    21.             cellRadius = settingData.cellRadius,
    22.             localTransformList = localTransformList,
    23.             physicsMassList = physicsMassList
    24.         }.Schedule(cellBuffer.Length, 64, state.Dependency);
    25.  
    26.         // costJob.Complete();
    27.  
    28.         var integrationJob = new CalCulateIntegration_FastMarchingMethodJob()
    29.         {
    30.             cells = cellBuffer.AsNativeArray(),
    31.             settingData = settingData
    32.         }.Schedule(costJob);
    33.  
    34.  
    35.         state.Dependency = integrationJob;
    36.     }
    37.  
    is the way i use the dependency is wrong?
     
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,653
    upload_2023-1-21_12-47-6.png
    This is the issue. You already assigned and
    Scheduled
    CalculateCostJob
    and it can run at any time, which means on next row - 30 you can't call
    .AsNativeArray()
    as it's not safe (previous job already can write to that memory).
    upload_2023-1-21_12-49-12.png

    To solve this just write this on row 15 and use that in both jobs.
    Code (CSharp):
    1. var cells = cellBuffer.AsNativeArray();
     
    MichealChiNguyen and yifanchu183 like this.
  3. yifanchu183

    yifanchu183

    Joined:
    Jul 4, 2019
    Posts:
    41
    Thanks a lot, it FIXED