Does UpdateAfter system wait for dependency job completion?

Theorycrafting some framework, need to know:

E.g. execution order:

  1. System that populates some native container via job;
  2. Second system that references first one, and grabs native container in OnUpdate.
    Then starts its own jobs. (UpdateAfter – set to first)

Will this setup wait for the first system jobs to complete, and only then perform OnUpdate in second system?
Or first system OnUpdate will run then second system OnUpdate will run independent of first system job completion state?

[UpdateBefore/After] has nothing to do with JobHandles and jobs. They are completely separate mechanisms.

1 Like

Always thought UpdateAfter would combine internal Dependency, but no they do not, you’re correct.
Just ran a small test, and the results in this case:

but after first OnUpdate.

For future readers, you can expose handle from one system, and combine with dependencies of another, and it should execute jobs in correct order:

public class NativeContainerTestSystem : SystemBase {
   public NativeMultiHashMap<int, int> Lookup;
   public JobHandle Handle { get; private set; }

   protected override void OnCreate() {
      base.OnCreate();

      Lookup = new NativeMultiHashMap<int, int>(4, Allocator.Persistent);
   }

   protected override void OnUpdate() {
      var lookup = Lookup;
      lookup.Clear();
     
      Entities.ForEach((in Position pos) => {
         lookup.Add(1, 123);
      }).Schedule();

      Handle = Dependency;
   }

   protected override void OnDestroy() {
      base.OnDestroy();
      if (Lookup.IsCreated) Lookup.Dispose();
   }
}

[AlwaysUpdateSystem]
[UpdateAfter(typeof(NativeContainerTestSystem))]
public class NativeReceiverTestSystem : SystemBase {
   private NativeMultiHashMap<int, int> _lookup;
   private NativeContainerTestSystem _system;
 
   protected override void OnCreate() {
      base.OnCreate();

      _system = World.GetOrCreateSystem<NativeContainerTestSystem>();
      _lookup = _system.Lookup;
   }

   protected override void OnUpdate() {
      NativeMultiHashMap<int, int> lookup = _lookup;
      Dependency = JobHandle.CombineDependencies(Dependency, _system.Handle);

      Entities.ForEach((BoidData test) => {
                 if (lookup.TryGetFirstValue(1, out int item, out _)) {
                    Debug.Log(item);
                 }
              })
              .WithReadOnly(lookup)
              .WithoutBurst()
              .ScheduleParallel();
   }
}
2 Likes

True, UpdateAfter/Before are only related with sorting of OnUpdate() calls inside a ComponentSystemGroup.
For ensuring job completion before feeding the result of a job to another system, we have to resort to Manual dependency management.
A good example is Unity Physics’ dependency management with its BuildPhysicsWorld / StepPhysicsWorld / ExportPhysicsWorld / EndFramePhysicsSystem systems.
I had the same doubt for days, UnityPhysics source code cleared it for me at last.:roll_eyes:

1 Like