Search Unity

replacement for ComponentDataArray

Discussion in 'Entity Component System' started by SocialSimulator, Feb 14, 2019.

  1. SocialSimulator

    SocialSimulator

    Joined:
    Sep 19, 2017
    Posts:
    26
    I am struggling to find a sample for how to access an array of entity components in a job system such as IJobProcessComponentDataWithEntity.

    With ComponentSystem, one can create an array of entities using something like this:
    using (var selectedEntities = m_selectedVEntities.ToEntityArray(Allocator.TempJob))
    ...
    but that uses only the main thread.

    Is there an alternative with IJobProcessComponentDataWithEntity ?

    Many thanks
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    ToEntityArray has an optional parameter giving you back a job handle. So a dedicated job will extract the entities and your own code can depend on that job handle to ensure it completes before your job consumes the entity array.
     
    AndesSunset likes this.
  3. SocialSimulator

    SocialSimulator

    Joined:
    Sep 19, 2017
    Posts:
    26
    Thanks, Joachim. I am sorry to sound like a dimwit noob. Do you have a short worked example, I could follow from?
     
  4. NoDumbQuestion

    NoDumbQuestion

    Joined:
    Nov 10, 2017
    Posts:
    186
    I write jobs like this and it magically pass entity to job
    Code (CSharp):
    1. public class TestSystem : JobComponentSystem
    2. {
    3.     private ComponentGroup m_group;
    4.     protected override void OnCreateManager()
    5.     {
    6.         m_group = GetComponentGroup(ComponentType.Create<Position>());
    7.     }
    8.  
    9.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    10.     {
    11.         inputDeps = new Job().Schedule(this, inputDeps);
    12.         return inputDeps;
    13.     }
    14.  
    15.     struct Job : IJobProcessComponentDataWithEntity<Position>
    16.     {
    17.         public void Execute(Entity entity, int index, ref Position c0)
    18.         {
    19.             // Do stuff
    20.         }
    21.     }
    22. }
    For IJob it would look something like this I think
    Code (CSharp):
    1. public class TestSystem : JobComponentSystem
    2. {
    3.     private ComponentGroup m_group;
    4.     protected override void OnCreateManager()
    5.     {
    6.         m_group = GetComponentGroup(ComponentType.Create<Position>());
    7.     }
    8.  
    9.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    10.     {
    11.         var job = new Job() {array = m_group.ToEntityArray(Allocator.TempJob, out inputDeps)};
    12.         inputDeps = job.Schedule(inputDeps);
    13.         return inputDeps;
    14.     }
    15.  
    16.     struct Job : IJob
    17.     {
    18.         [DeallocateOnJobCompletion]
    19.         public NativeArray<Entity> array;
    20.  
    21.         public void Execute()
    22.         {
    23.             // Do another stuff
    24.         }
    25.     }
    26. }
     
    Last edited: Feb 19, 2019
  5. SocialSimulator

    SocialSimulator

    Joined:
    Sep 19, 2017
    Posts:
    26
    That's great. Thanks )