Search Unity

[request] Include An Ijobforeachwithentity Interface With No Icomponentdata Type Parameters

Discussion in 'Entity Component System' started by TRS6123, Apr 10, 2019.

  1. TRS6123

    TRS6123

    Joined:
    May 16, 2015
    Posts:
    246
    Say I wanted to use a job to add a component data to entities with a component tag. You can use an IJobParallelFor with a NativeArray of Entities you want to operate on and an entity command buffer:
    Code (CSharp):
    1. struct AddIntComponents : IJobParallelFor
    2. {
    3.     public EntityCommandBuffer.Concurrent Ecb;
    4.     public NativeArray<Entities> Entities;
    5.  
    6.     public void Execute(int i)
    7.     {
    8.         Ecb.AddComponent(i, entities[i], new MyInt { Value = 0f });
    9.     }
    10. }

    Or you can use an IJobForEachWithEntity with the tag component as a type parameter:
    Code (CSharp):
    1. struct AddIntComponents : IJobForEachWithEntity<MyComponentTag>
    2. {
    3.     public EntityCommandBuffer.Concurrent Ecb;
    4.  
    5.     public void Execute(Entity e, int i, ref MyComponentTag tag)
    6.     {
    7.         Ecb.AddComponent(i, e, new MyInt { Value = 0f });
    8.     }
    9. }

    Since the tag component isn't read from or written to, I believe a version of IJobForEachWithEntity with no type parameters would be more convenient and performant than the above options, as shown here:
    Code (CSharp):
    1. [RequireComponentTag(typeof(MyComponentTag))]
    2. struct AddIntComponents : IJobForEachWithEntity
    3. {
    4.     public EntityCommandBuffer.Concurrent Ecb;
    5.  
    6.     public void Execute(Entity e, int i)
    7.     {
    8.         Ecb.AddComponent(i, e, new MyInt { Value = 0f });
    9.     }
    10. }
    Are there any plans for offering this option?
     
    Last edited: Apr 10, 2019
    Xerioz likes this.
  2. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    546
    Use IJobChunk and create an EntityQuery for your required types


    Code (CSharp):
    1. public class MySystem : JobComponentSystem {
    2.  
    3.         struct Job : IJobChunk {
    4.             [ReadOnly] public ArchetypeChunkEntityType entityType;
    5.             public EntityCommandBuffer.Concurrent CommandBuffer;
    6.             public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex) {
    7.                 for (int i = 0; i < chunk.Count; i++) {
    8.                    var entities = chunk.GetNativeArray(entityType);
    9.                     for (int j = 0; j < entities.Length; j++) {
    10.                         CommandBuffer.AddComponent(chunkIndex, entities[j], new MyComponent());
    11.                     }
    12.                 }
    13.             }
    14.         }
    15.  
    16.         private SomeBarriere EndFrameBarrier;
    17.         protected override void OnCreateManager() {
    18.             base.OnCreateManager();
    19.             EndFrameBarrier = World.GetOrCreateManager<SomeBarriere>();
    20.             myQuery = GetEntityQuery(ComponentType.ReadOnly<MyTag>());
    21.         }
    22.  
    23.         protected override JobHandle OnUpdate(JobHandle inputDeps) {
    24.  
    25.             var job = new Job() {
    26.                 CommandBuffer = EndFrameBarrier.CreateCommandBuffer().ToConcurent(),
    27.                 entityType = GetArchetypeChunkEntityType(),
    28.             };
    29.             return job.Schedule(myQuery, inputDeps);
    30.  
    31.         }
    32.     }
     
    Last edited: Apr 10, 2019
    tarahugger and xVergilx like this.
  3. Xerioz

    Xerioz

    Joined:
    Aug 13, 2013
    Posts:
    104
    I'd love to have this as well, got few systems that only require component tags and entity array access
     
    TRS6123 likes this.
  4. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    529
    All places I required this in the past were replaced with the batch Add/RemoveComponent APIs of EntityManager which is much faster and even less code. You only have to make sure not to introduce additional barriers, eg by putting the CS doing it right before/after the already existing barrier.

    There still could be other use cases eg using ComponentDataFromEntity but I currently don't require this.
     
  5. TRS6123

    TRS6123

    Joined:
    May 16, 2015
    Posts:
    246
    I appreciate your quick reply, but you have a LOT of boiler plate code in your above example. I'm mainly making this request for the sake of convenience.

    Thanks for your contribution. This solves a part of my problem. Perhaps my presented use case wasn't the best one to use for this request, as many of my jobs that would benefit from my request do use conditional logic (as you considered above) or don't even use entity command buffers.
     
    Spy-Shifty likes this.
  6. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    +1 Would be nice to have
     
  7. PaulUsul

    PaulUsul

    Joined:
    Nov 20, 2012
    Posts:
    29