Search Unity

Does Generic IJobForEach Correctly Handlle Job Dependencies?

Discussion in 'Entity Component System' started by Piefayth, Apr 19, 2019.

  1. Piefayth

    Piefayth

    Joined:
    Feb 7, 2017
    Posts:
    61
    Created a generic system that takes a component type as a type argument, but when scheduling the job it appears to get a dependency error. If I remove the generic-ness and just use the component type directly, I do not get the same error. Any thoughts?

    Complete drop in example, requires Unity.Entities and Unity.Physics (uses the physics systems to demonstrate the error via Translation access).

    Code (CSharp):
    1. using Unity.Entities;
    2. using Unity.Jobs;
    3. using Unity.Transforms;
    4. using Unity.Physics.Systems;
    5. using Unity.Physics;
    6. using Unity.Collections;
    7. using UnityEngine;
    8.  
    9. [UpdateBefore(typeof(BuildPhysicsWorld))]
    10. public class DoTranslationThings : DoThings<Translation> { }
    11.  
    12. public class DoThings<T> : JobComponentSystem where T : struct, IComponentData {
    13.     EntityQuery query;
    14.  
    15.     protected override JobHandle OnUpdate(JobHandle inputDeps) {
    16.         return new DoComponentThingsJob<T> { }.Schedule(query, inputDeps);
    17.     }
    18.     protected override void OnCreateManager() {
    19.         query = EntityManager.CreateEntityQuery(typeof(T));
    20.         EntityManager.CreateEntity(typeof(T), typeof(LocalToWorld), typeof(NonUniformScale), typeof(PhysicsVelocity), typeof(Rotation), typeof(PhysicsCollider), typeof(PhysicsMass), typeof(PhysicsDamping));
    21.     }
    22. }
    23.  
    24. public struct DoComponentThingsJob<T> : IJobForEachWithEntity<T> where T : struct, IComponentData {
    25.     public void Execute(Entity e, int index, [ReadOnly] ref T component) {
    26.         Debug.Log("test");
    27.     }
    28. }
    Error:

    Code (CSharp):
    1. InvalidOperationException: The system Unity.Physics.Systems.BuildPhysicsWorld reads Unity.Transforms.Translation via DoComponentThingsJob`1 but that type was not returned as a job dependency. To ensure correct behavior of other systems, the job or a dependency of it must be returned from the OnUpdate method.
    2.  
    Edit: Error only happens if I schedule with an EntityQuery. If I .Schedule(this, inputDeps) it's fine.

    Edit2: Using ComponentSystem.GetEntityQuery instead of EntityManager.GetEntityQuery works. That makes sense. Resolved, I think!
     
    Last edited: Apr 19, 2019
    Abbrew likes this.