Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Confused on dependency management

Discussion in 'Entity Component System' started by snacktime, Jun 16, 2018.

  1. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    So I have system A that reads components in OnUpdate, and system B that reads and writes to the same components in a job. But I'm getting errors because A's OnUpdate is being called before B's job is finished.

    Is this a bug or does dependency handling really only work for jobs and not systems?
     
  2. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    Hmm and it just went away without any code changes. Tried varying the load to get it to trigger but no luck.
     
  3. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    Just ran into a similar case, well not completely similar. I have a NativeMultiHashMap declared in a JobComponentSystem. In OnUpdate I clear it then pass it to an IJob on the next line. I start getting errors on the line calling Clear saying the job wasn't completed. Moving the clear to the job fixes it which makes sense.

    So it seems Complete is being called lazily and at the same time not able to track native containers you manage yourself outside of a job context. Or maybe it's just NativeMultiHashMap. I don't have the same pattern anywhere else that would even trigger this so not sure.
     
  4. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,555
    Auto dependencies only works for things in job because it packs the knowledge of deps in `inputDeps` job handle. If you use something outside of a job (ComponentSystem/JobComponentSystem OnUpdate) it will depends on the system execution order if the error will throw or not.

    If you are sure that what you want to use (out of a job) is inside a job which this system's job depends on, you can call .Complete on inputDeps argument of OnUpdate at the first line in OnUpdate to force completion. It might be not that efficient as all other deps will also be completed. (Might cause a longer than expected wait for job in timeline) To pinpoint the exact deps to complete sometimes I just inject the other system and get its public field's JobHandle that I stored before when scheduling to .Complete it.
     
  5. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    Ya I was thinking at first it was smarter then it is. And the docs aren't completely clear so it left me guessing as to what the behavior should be.
     
  6. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    So something else I'm missing.

    TacticsMapSystem first line in OnUpdate:
    Code (csharp):
    1.  
    2. if (!inputDeps.IsCompleted) inputDeps.Complete();
    3.  
    NavmeshSystem is very simple it returns this:
    Code (csharp):
    1.  
    2. return tacticsJob.Schedule(inputDeps);
    3.  
    But in TacticsMapSystem where in OnUpdate I go to iterate over components that the NavmeshSystem job works with, I sporadically get this. I don't see how it's possible.

    InvalidOperationException: The previously scheduled job NavmeshSystem:TacticsQueryJob writes to the NativeArray TacticsQueryJob.Requests. You must call JobHandle.Complete() on the job NavmeshSystem:TacticsQueryJob, before you can read from the NativeArray safely.

    This is my player loop.
    upload_2018-6-16_23-2-36.png
     
  7. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    Nm wasn't thinking there (Have to call complete regardless of IsComplete value)
     
    5argon likes this.