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

How to sequence dependant jobs with structural changes?

Discussion in 'Entity Component System' started by abob101, Mar 31, 2022.

  1. abob101

    abob101

    Joined:
    Oct 28, 2017
    Posts:
    24
    Hi all,

    DOTS newb here. I am trying to understand how to sequence multiple jobs that makes structural changes (e.g. new entities) where they must run in a specific order. The example I am working with currently is a map generator (think Civilization style hex map).

    Lets say we have these systems that schedule jobs to do the work:

    System A -> Job A: Hex Map Generator (creates entities reperesenting an empty hex map structure)
    System B -> Job B: Terrain Generator (assigns terrain types to the hex map)
    System C -> Job C: Region Generator (divides the map into sections, creating Region entities for each section)
    System D -> Job D: Faction Generator (creates Faction entities, assigning them to a Region)
    System E, F, G etc as required to complete map generation

    The idea is to schedule jobs to do the work in the background while the main thread idles (perhaps updating a progress bar). The jobs are dependant... each needs the entities from the previous job(s) to be created and/or updated. The jobs are using EntityCommandBuffers. I believe I understand how to establish the job dependancies by grabbing the JobHandle for each and including it as a dependancy (using JobHandle.CombineDependencies) in subsequent job(s). However....

    The problem/challenge is that even though the jobs have dependancies set up... I think that because the structural changes (e.g. entity creation) are made using EntityCommandBuffers.... when System B runs to schedule it's job the entities created in Job A do not yet exist (and likewise for Jobs C, D etc). I have all these systems updating in the default SimulationSystemGroup, sequenced using UpdateAfter. The ECB for each job is using EndSimulationEntityCommandBufferSystem. I'm guessing that the entity query is running when systems B, C, D call their OnUpdate and at that point the entities created in A don't exist.

    At what point exactly are the changes made using the ECB updated to the World so that the next System/Job can access them? I have looked at this:

    https://docs.unity3d.com/Packages/com.unity.entities@0.5/manual/system_update_order.html

    If i'm using EndSimulationEntityCommandBufferSystem for Job A, does this mean the entities it creates will be synced to the World when the EndSimulationEntityCommandBufferSystem runs ?

    If that is the case, how do I work around this.... should I use the same ECB for all 4 jobs?

    I hope this makes sense, any assistance greatly appreciated.

    Thanks
     
  2. Chris-Herold

    Chris-Herold

    Joined:
    Nov 14, 2011
    Posts:
    115
    You can add your own EntityCommandBufferSystems and define when they should run using the UpdateBefore/After/UpdateInGroup attributes.