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

Problem when iterating over chunks inside foreach

Discussion in 'Entity Component System' started by Knosis_Co-Verse, Apr 24, 2021.

  1. Knosis_Co-Verse

    Knosis_Co-Verse

    Joined:
    Apr 23, 2019
    Posts:
    12
    I'm getting this error:

    System.IndexOutOfRangeException: Index {0} is out of restricted IJobParallelFor range [{1}...{2}] in ReadWriteBuffer.



    Code (CSharp):
    1.  
    2. protected override void OnStartRunning()
    3. {
    4.         cellsQuery = GetEntityQuery(ComponentType.ReadOnly<BasicCell>());
    5.         cellsArray = cellsQuery.ToEntityArray(Allocator.Persistent);
    6.         chunks = cellsQuery.CreateArchetypeChunkArray(Allocator.Persistent);
    7. }
    8.  
    9. protected override void OnUpdate()
    10. {
    11.         float dt = Time.DeltaTime;
    12.         NativeArray<Entity> cellsArray = this.cellsArray;
    13.         NativeArray<ArchetypeChunk> chunks = this.chunks;
    14.  
    15.         var translationType = GetComponentTypeHandle<Translation>();
    16.         var idType = GetComponentTypeHandle<ID>();
    17.  
    18.         Entities.
    19.         WithReadOnly(translationType).
    20.         WithReadOnly(idType).
    21.         ForEach((ref PhysicsVelocity curVell, in Translation translation, in ID id, in BasicCell cell) =>
    22.         {
    23.             float3 attraction = new float3();
    24.             BasicCell curCell = cell;
    25.  
    26.             for (int c = 0; c < chunks.Length; c++)
    27.             {
    28.                 ArchetypeChunk chunk = chunks[c];
    29.                 NativeArray<Translation> translations = chunk.GetNativeArray(translationType);
    30.                 NativeArray<ID> ids = chunk.GetNativeArray(idType);
    31.  
    32.                 for (int i = 0; i < chunk.Count; i++)
    33.                 {
    34.                     int otherID = ids[i].Value;
    35.  
    36.                     if (id.Value == otherID) continue;
    37.  
    38.                     float3 otherPosition = translations[i].Value;
    39.  
    40.                     float distance = math.distance(translation.Value, otherPosition);
    41.                     if (distance < curCell.attractionDistance)
    42.                     {
    43.                         attraction += math.normalize(otherPosition - translation.Value);
    44.                     }
    45.                 }
    46.  
    47.                 translations.Dispose();
    48.                 ids.Dispose();
    49.             }
    50.  
    51.             if (attraction.x != 0 || attraction.y != 0 || attraction.z != 0)
    52.             {
    53.                 curVell.Linear += math.normalize(attraction) * dt * cell.acceleration;
    54.             }
    55.         }).ScheduleParallel();
    56.     }


    I think I understand from where the error is coming from but isn't it what chunks were supposed to solve? Am I mistaken on the use of chunks? All the sources that i found on this topic seemed to be using them in that manner.

    Anyways I'm trying to iterate over entitys inside a foreach in a SystemBase.OnUpdate method using scheduleParallel (everything works fine with schedule).

    Weirdly enougth, yesterday, I was getting the error only twice per play, but today, it appears in every loop. The only change i made was updating from 2021.1.3f to 2021.1.4f but the packages are at the same version, i think.
     
  2. Knosis_Co-Verse

    Knosis_Co-Verse

    Joined:
    Apr 23, 2019
    Posts:
    12
    Welp. Immediately after making the post I found the solution: adding a WithReadOnly(chunks). Ops!