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 What is GatherEntitiesJob and why is it failing on me when I use ToEntityArrayAsync

Discussion in 'Entity Component System' started by Santonian, Sep 8, 2021.

  1. Santonian

    Santonian

    Joined:
    Sep 23, 2020
    Posts:
    20
    Hi there,
    I try to use a NativeArray with entities inside my Entity.forEach. This is what I do. As you can see, I don't even use the idleCharacterLst inside the foreach.

    Code (CSharp):
    1. protected override void OnUpdate()
    2.     {
    3.         var idleCharacterLst = idleCharacterQuery.ToEntityArrayAsync(Allocator.TempJob, out var jobHandle1);
    4.  
    5.  
    6.  Entities
    7.   //         .WithDisposeOnCompletion(idleCharacterLst)
    8.             .ForEach((Entity jobEntity, int entityInQueryIndex, ref JobData jobData) => {
    9.  
    10.             }).ScheduleParallel();
    This is what I get:
    InvalidOperationException: The previously scheduled job GatherEntitiesJob reads from the Unity.Entities.EntityTypeHandle GatherEntitiesJob.JobData.EntityTypeHandle. You must call JobHandle.Complete() on the job GatherEntitiesJob, before you can deallocate the Unity.Entities.EntityTypeHandle safely.

    What does this mean... and how can I solve this?

    cheers
    Santo.
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,626
    jobHandle1

    you just have a hanging job handle. Your generating your idleCharacterLst asynchronously so you need to tell the job to wait on it by passing in the handle

    Dependency = JobHandle.CombineDependencies(Dependency, jobHandle1);
     
  3. Santonian

    Santonian

    Joined:
    Sep 23, 2020
    Posts:
    20
    Thanks for pointing that out, you are probably right about this. However, I simply reduced the code and commented out everything to figure out, what was causing the error. If I do this, I get the same error. (Maybe it's still wrong, but should be correct in my understanding.

    Code (CSharp):
    1. protected override void OnUpdate()
    2.     {
    3.         var idleCharacterLst = idleCharacterQuery.ToEntityArrayAsync(Allocator.TempJob, out var jobHandle1);
    4.         Dependency = JobHandle.CombineDependencies(Dependency, jobHandle1);
    5. Dependency = Entities
    6.            .WithDisposeOnCompletion(idleCharacterLst)
    7.             .ForEach((Entity jobEntity, int entityInQueryIndex, ref JobData jobData) => {
    8.            
    9.             for(int i=0; i<idleCharacterLst.size(); i++{
    10.                //do stuff
    11.             }
    12.             }).ScheduleParallel(Dependency);
    13.            
    14. }
     
  4. Santonian

    Santonian

    Joined:
    Sep 23, 2020
    Posts:
    20
    Thank you tertle for pointing out the correct problem. Next time I should post all of the code. :)

    I had this:

    Code (CSharp):
    1.        
    2. var idleCharacterLst = idleCharacterQuery.ToEntityArrayAsync(Allocator.TempJob, out var jobHandle1);
    3.         if(idleCharacterLst.Length <= 0) {
    4.             //leave if there is no idle character
    5.             return;
    6.         }
    7.         Dependency = JobHandle.CombineDependencies(Dependency, jobHandle1);
    8.  
    9.        ....
    10.  
    But I need to do it like this, and now its working:

    Code (CSharp):
    1.        
    2. var idleCharacterLst = idleCharacterQuery.ToEntityArrayAsync(Allocator.TempJob, out var jobHandle1);
    3.         Dependency = JobHandle.CombineDependencies(Dependency, jobHandle1);
    4.  
    5.         if(idleCharacterLst.Length <= 0) {
    6.             idleCharacterLst.Dispose(Dependency);
    7.             //leave if there is no idle character
    8.             return;
    9.         }