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

Question How automatic are job dependencies in ISystem?

Discussion in 'Entity Component System' started by Ryetoast, Jan 2, 2023.

  1. Ryetoast

    Ryetoast

    Joined:
    Mar 8, 2015
    Posts:
    48
    I see multiple examples that seem to handle it differently. Some just seem to call:
    new Job {}.Schedule()

    while others do:
    state.Dependency = new Job{}.Schedule(state.Dependency)

    Do these end up doing the same behavior? Can I just call the first version without issue or is that a potential source of bugs if the job runs long enough for some other job to attempt to access the same data?

    Also do component lookups automatically factor in their read/write status when a job uses one?
    I've been getting some error messages like "The system <systemname> writes <componentname> via <jobname> but that type was not assigned to the Dependency property. To ensure correct behavior of other systems, the job or a dependency must be assigned to the Dependency property before returning from the OnUpdate method." I've only seen this issue show up on jobs that access via component lookups. I'm not sure if the problem is not properly assigning the dependency of the job schedule, or if there's something going wrong with how I am grabbing and passing component lookups.

    Basically I'm trying to convert a lot of my SystemBase stuff over to ISystem and I'm kind of confused exactly what needs to be updated. There's so much more boilerplate and data passing I need to do manually with ISystem, but the perf on SystemBase is pretty bad =(
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,983
    For IJobEntity, yes. For anything else, use the latter form.

    Acquiring the lookup inside the system is when the dependency is registered with the system.
     
    Egad_McDad, Occuros and Ryetoast like this.
  3. Ryetoast

    Ryetoast

    Joined:
    Mar 8, 2015
    Posts:
    48
    Ok, good to know IJobEntity can do it automatically, that covers the majority of my uses.

    If I understand what you mean about the lookups correctly, acquiring the lookup anywhere (e.g. OnCreate or OnUpdate) will apply it to the system's dependencies, but in the case of dependencies between jobs within a single system, I'd need to manage their dependencies manually if they aren't all IJobEntity, right?
     
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,983
    Yes. Usually that involves getting and assigning state.Dependency.
     
  5. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,653
    I would suggest always use latter for code consistency and explicit look :)
     
    Krajca likes this.
  6. Micz84

    Micz84

    Joined:
    Jul 21, 2012
    Posts:
    436
    I agree, one thing less to remember and it is more explicit.
     
    Krajca likes this.
  7. Ryetoast

    Ryetoast

    Joined:
    Mar 8, 2015
    Posts:
    48
    In case anyone stumbles upon this thread looking for "The system <systemname> writes <componentname> via <jobname> but that type was not assigned to the Dependency property. To ensure correct behavior of other systems, the job or a dependency must be assigned to the Dependency property before returning from the OnUpdate method."

    This turned out to just be a nonsense error caused by a different error that led to the OnUpdate not completing normally and leaving things in kind of a broken state. In my case a copy paste error leading to both a writable ComponentLookup<ComponentA> and a (ref ComponentA) for the same component in a job throwing an error in one of my jobs for a duplicate alias.
     
    CheMBurN likes this.
  8. joepl

    joepl

    Unity Technologies

    Joined:
    Jul 6, 2017
    Posts:
    85
    To expand a bit on what others have said, the automatic dependency tracking working works only with jobs that know something about the containing system - currently Entities.ForEach (in SystemBase) and IJobEntity. If you want to see the specific dependency tracking that is happening, both Rider and Visual Studio will show you the generated backing type of the system (hit F12 while selecting the original system type name in Rider). This will show you the explicit usage of the Dependency property. You will see a method that corresponds to the user method with all of the additional generated code.
     
    Sirius64 and xVergilx like this.
  9. Singtaa

    Singtaa

    Joined:
    Dec 14, 2010
    Posts:
    485
    Just want to add to @Ryetoast 's comment (and also because this thread is the top search result for the aforementioned error msg).

    The error msg can be deceiving because there are many things that can indirectly lead to it. Here's one more (quite nasty to debug if you are not careful). This will throw the error:
    Code (CSharp):
    1. new FooJob() { // IJobEntity
    2.     entities = buffer.AsNativeArray()
    3. }.Schedule();
    4.  
    5. new BarJob() { // IJobEntity
    6.     entities = buffer.AsNativeArray()
    7. }.Schedule();
    And this will not:
    Code (CSharp):
    1. var entities = buffer.AsNativeArray()
    2.  
    3. new FooJob() {
    4.     entities = entities
    5. }.Schedule();
    6.  
    7. new BarJob() {
    8.     entities = entities
    9. }.Schedule();
     
    Last edited: Aug 15, 2023
    Ryetoast likes this.