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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Accessing other Entities within a Job

Discussion in 'Entity Component System' started by Zeraphan, Feb 25, 2020.

  1. Zeraphan

    Zeraphan

    Joined:
    Nov 20, 2019
    Posts:
    14
    I have a ForEach job that is going through a set of TargetChange entities when they are created. Within this job I want to be able to access a set of Ship entities so that I can update their targets.

    This is what I have put together so far:
    Code (CSharp):
    1. public class TargetUpdatedSystem : JobComponentSystem
    2. {
    3.     private EndSimulationEntityCommandBufferSystem endSimCommandBufferSystem;
    4.     private EntityQuery m_Group;
    5.     public struct TargetUpdatedJob : IJobForEachWithEntity<TargetChangeComponent>
    6.     {
    7.         public EntityCommandBuffer.Concurrent CommandBuffer;
    8.         [DeallocateOnJobCompletion]public NativeArray<Entity> ships;
    9.        
    10.         public void Execute(Entity entity, int index,ref TargetChangeComponent target)
    11.         {
    12.             foreach (var v in ships)
    13.             {
    14.                 Ship s = Unity.Entities.World.DefaultGameObjectInjectionWorld.EntityManager.GetComponentData<Ship>(v);
    15.                 s.OrbitTarget = target.Target;
    16.             }
    17.             CommandBuffer.DestroyEntity(index,entity);
    18.            
    19.         }
    20.  
    21.     }
    22.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    23.     {
    24.         var job = new TargetUpdatedJob()
    25.         {
    26.             ships = m_Group.ToEntityArray(Allocator.TempJob),
    27.             CommandBuffer = endSimCommandBufferSystem.CreateCommandBuffer().ToConcurrent()
    28.         };
    29.  
    30.         var jobHandle = job.Schedule(this, inputDeps);
    31.         endSimCommandBufferSystem.AddJobHandleForProducer(jobHandle);
    32.         return jobHandle;
    33.     }
    34.    
    35.     protected override void OnCreate()
    36.     {
    37.         m_Group = GetEntityQuery(typeof(Ship));
    38.         endSimCommandBufferSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
    39.         base.OnCreate();
    40.     }
    41. }
    42.  
    Lines 12 and 14 are giving me errors right now. This is probably not the correct way to go about accessing this data, I have pieced this together looking through examples and posts I have found here on the forum.

    Line 14 gives me an error about ScheduleBatchedJobsAndComplete can only be called from the main thread.

    If taken out Line 12 then gives me an error IndexOutOfRangeException: Index 1 is out of restricted IJobParallelFor range [0...0] in ReadWriteBuffer.
     
  2. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,695
    You need to use a for loop no IEnumarable in NativeArry I think. Some correct me if I’m wrong
     
  3. Zeraphan

    Zeraphan

    Joined:
    Nov 20, 2019
    Posts:
    14
    Changing it to a standard for loop seems to solve part of the problem. The Line 14 error still exists.
     
  4. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,655
    You can’t use EntityManager inside job (except ExclusiveEntityTransaction). Look at samples and read docs about accessing data inside jobs, or use search, It’s discussed in this forum soooooo many times :) Search by ComponentDataFromEntity. In addition, looking to your code - it’s definitely not safe if you have many TargetChange entities and iterating through same ships every time it will be race condition when your TargetChange archetype become more than one chunk. If you have only one TargetChange at time - revert you logic which iterate through all ships in parallel and set target for them and in next job in chain just destroy this target by ECB.
     
    Last edited: Feb 25, 2020
    OUTTAHERE likes this.