Search Unity

Iterating Through Entities In Ijobchunk

Discussion in 'Entity Component System' started by FrankvHoof, Apr 15, 2019.

  1. FrankvHoof

    FrankvHoof

    Joined:
    Nov 3, 2014
    Posts:
    258
    How does one get the Entities in the current Chunk in a IJobChunk?
    GetNativeArray seems to be aimed at getting componentdata, not Entities...

    (Trying to copy Entities to a list if one of their components matches a certain value)
     
  2. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,555
    Use GetNativeArray with ArchetypeChunkEntityType
     
    FrankvHoof likes this.
  3. kork

    kork

    Joined:
    Jul 14, 2009
    Posts:
    280
    Here is an example:

    Code (CSharp):
    1.  
    2. protected override JobHandle OnUpdate(JobHandle inputDeps)
    3. {
    4.     var entityType = GetArchetypeChunkEntityType();
    5.     var coordinatesType = GetArchetypeChunkComponentType<Coordinates>();
    6.    
    7.    JobChunkExtensions.Schedule(new AddNewUnitsToCacheJob
    8.    {
    9.     EntityType = entityType,
    10.     CoordinatesType = coordinatesType
    11.    }, _uncachedUnits);
    12. }
    13.  
    14. private struct AddNewUnitsToCacheJob : IJobChunk
    15.  {
    16.             [ReadOnly] public ArchetypeChunkEntityType EntityType;
    17.             [ReadOnly] public ArchetypeChunkComponentType<Coordinates> CoordinatesType;
    18.  
    19.             public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex)
    20.             {
    21.                 var entities = chunk.GetNativeArray(EntityType);
    22.                 var coordinates = chunk.GetNativeArray(CoordinatesType);
    23.                 for (var i = 0; i < chunk.Count; i++)
    24.                 {
    25.          // do what you need to do
    26.                 }
    27.             }
    28. }