Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

dependency error with ComponentDataFromEntity

Discussion in 'Entity Component System and C# Job system' started by Spy-Shifty, Sep 16, 2018.

  1. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    373
    Hi,

    I've these two System and I get the following dependency error:

    InvalidOperationException: The system MovementSystem reads FieldReference via TurnCommandSystem:Job 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.

    But both are read only so. How to fix this?


    Code (CSharp):
    1. public class TurnCommandSystem : JobComponentSystem {
    2.  
    3.     //[BurstCompile]
    4.     struct Job : IJobProcessComponentDataWithEntity<TurnCommand> {
    5.         public EntityCommandBuffer.Concurrent CommandBuffer;
    6.         [ReadOnly] public ComponentDataFromEntity<FieldReference> FieldReferences;
    7.         public void Execute(Entity entity, int index, [ReadOnly] ref TurnCommand data) {
    8.             CommandBuffer.DestroyEntity(index, entity);
    9.  
    10.             CommandBuffer.SetComponent(index, data.Figure, new FieldReference { Value = data.Field });
    11.             CommandBuffer.RemoveComponent<FigureReference>(index, FieldReferences[data.Figure].Value);
    12.         }
    13.     }
    14.  
    15.     [Inject] EndFrameBarrier endFrameBarrier;
    16.     protected override JobHandle OnUpdate(JobHandle inputDeps) {
    17.         var job = new Job() {
    18.             CommandBuffer = endFrameBarrier.CreateCommandBuffer().ToConcurrent(),
    19.             FieldReferences = EntityManager.GetComponentDataFromEntity<FieldReference>(true),
    20.         };
    21.         return job.Schedule(this, inputDeps);
    22.     }
    23. }
    Code (CSharp):
    1. public class MovementSystem : JobComponentSystem {
    2.  
    3.  
    4.  
    5.     struct ChessboardData {
    6.         [ReadOnly] public ComponentDataArray<Chessboard> Chessboard;
    7.         public readonly int Length;
    8.         public bool Any => Length > 0;
    9.     }
    10.  
    11.     [Inject] ChessboardData chessboardData;
    12.  
    13.     protected override JobHandle OnUpdate(JobHandle inputDeps) {
    14.         if (!chessboardData.Any) {
    15.             return inputDeps;
    16.         }
    17.  
    18.         var job = new Job() {
    19.             dt = Time.deltaTime,
    20.             Chessboard = chessboardData.Chessboard[0],
    21.             GridPositionFromEntity = EntityManager.GetComponentDataFromEntity<GridPosition>(true),
    22.         };
    23.         return job.Schedule(this, inputDeps);
    24.     }
    25.  
    26.     struct Job : IJobProcessComponentData<FieldReference, Position> {
    27.         public float dt;
    28.         [ReadOnly] public Chessboard Chessboard;
    29.         [ReadOnly] public ComponentDataFromEntity<GridPosition> GridPositionFromEntity;
    30.         public void Execute([ReadOnly] ref FieldReference fieldReference, ref Position position) {
    31.             float3 pos = position.Value;
    32.             position.Value = math.lerp(position.Value, Chessboard.InverseTransformPoint(Chessboard, GridPositionFromEntity[fieldReference.Value].value), dt);
    33.         }
    34.     }
    35. }
    36.  
     
  2. dartriminis

    dartriminis

    Joined:
    Feb 3, 2017
    Posts:
    121
    In TurnCommandSystem, try injecting your ComponentDataFromEntity<FieldReference>, rather than getting it from the entity manager.
     
  3. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    373
    Ah, thank you!

    I didn't knew that I can inject the ComponentDataFromEntity directly!

    Now it works!
     
  4. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    4,175
    injecting is one way of doing it but you can also just call GetComponentDataFromEntity on the system as opposed to the EntityManager
     
    eizenhorn and Spy-Shifty like this.
  5. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    583
    in your case, the error is that you pass a copy of the data to the Job, without initializing the variable for them, do not forget about the difference between ref and out, ref requires that the variable be initialized.
     
  6. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    373
    What do you mean?

    Anyway the correct answer was already given by @dartriminis and @Joachim_Ante
     
  7. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    583
    Oh sorry, I’m not read error correctly :) forget what I saying :) Vacation makes me lazy :) upload_2018-9-19_11-52-19.jpeg
     
  8. LurkingNinjaDev

    LurkingNinjaDev

    Joined:
    Jan 20, 2015
    Posts:
    1,275
    Nice water texture, where you have got it from? Is it working with HDRP? :p :D
    (BTW, you're a show off... :p )
     
    rigidbuddy and optimise like this.