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

Question How to access NativeArray by index when using ScheduleParallel

Discussion in 'Entity Component System' started by lambch0p, Jul 21, 2022.

  1. lambch0p

    lambch0p

    Joined:
    Oct 22, 2014
    Posts:
    62
    Hi,

    I'm running through a Udemy course on ECS that's quite out of date, so am trying to update the code to run in 0.51.0-preview.32. I have a NativeArray that contains 4 float3 waypoints. If I run the job using .Schedule() it works, but runs at 8fps for 1000 GameObjects. If I try to use .ScheduleParallel() it doesn't work and throws this exception:
    Code (CSharp):
    1. IndexOutOfRangeException: Index 0 is out of restricted IJobParallelFor range [0...-1] in ReadWriteBuffer.
    I believe this is due to the NativeArray being broken into chunks but don't understand how to fix it. Can anyone shed any light on how to make the code bloew work when using ScheduleParallel() please? It's line 8 that throws the error, tankData.currentWP holds a value between 0 & 3, and waypointPositions has 4 items stored in it.

    Code (CSharp):
    1. protected override void OnUpdate()
    2.     {
    3.         float deltaTime = Time.DeltaTime;
    4.         NativeArray<float3> waypointPositions = new NativeArray<float3>(GameDataManager.instance.wps, Allocator.TempJob);
    5.         var dep = Entities.WithName("MoveSystem").ForEach((ref Translation position, ref Rotation rotation, ref TankData tankData) =>
    6.         {
    7.            
    8.             float3 currPos = waypointPositions[tankData.currentWP];
    9.             float3 heading = currPos - position.Value;
    10.             heading.y = 0;
    11.             quaternion targetDirection = quaternion.LookRotation(heading, math.up());
    12.             rotation.Value = math.slerp(rotation.Value, targetDirection, deltaTime * tankData.rotationSpeed);
    13.             position.Value += deltaTime * tankData.speed * math.forward(rotation.Value);
    14.             if (math.distance(position.Value, waypointPositions[tankData.currentWP]) < 1)
    15.             {
    16.                 tankData.currentWP++;
    17.                 if (tankData.currentWP >= waypointPositions.Length)
    18.                 {
    19.                     tankData.currentWP = 0;
    20.                 }
    21.             }
    22.  
    23.         }).WithDisposeOnCompletion(waypointPositions).ScheduleParallel(Dependency);
    24.         this.Dependency = dep;
    25.        
    26.     }
     
  2. lambch0p

    lambch0p

    Joined:
    Oct 22, 2014
    Posts:
    62
    Thanks, that's got it working (still something wrong as it's still only managing 8fps but that's probably just some dodgy code somewhere).

    I didn't think the Dependecy stuff was required but i'd been trying to piece the solution together from multiple sources, so ended up with a bit of frankencode.

    M
     
  3. lclemens

    lclemens

    Joined:
    Feb 15, 2020
    Posts:
    714
    How did you get it working?