Search Unity

Resolved Problem with getting raycast Job to work and get results back from NativeList

Discussion in 'Entity Component System' started by FederalRazer89, Feb 3, 2023.

  1. FederalRazer89

    FederalRazer89

    Joined:
    Nov 24, 2019
    Posts:
    83
    Hello


    This is a noob question. I have updated to ECS 1.0 and I am trying to schedule some do raycast jobs form SystemBase. In ECS 1.0 dependency is different and I am getting errors with dependency. Was able to do it in ECS 0.17 with a NativeList on each entity. Now I have a different a bit of a different setup for my test, but I could schedule some other jobs in a similar way, but those jobs just did some calculations.

    The error points towards a CombineDependencies, but i am kind of confused of how to approach this error.
    InvalidOperationException: The previously scheduled job RayCastTestingJob writes to the Unity.Collections.NativeList`1[Unity.Mathematics.float3] RayCastTestingJob.raycastPositionListJob.
    You are trying to schedule a new job RayCastTestingJob, which writes to the same Unity.Collections.NativeList`1[Unity.Mathematics.float3] (via RayCastTestingJob.raycastPositionListJob).
    To guarantee safety, you must include RayCastTestingJob as a dependency of the newly scheduled job.


    Have I setup the test wrong?

    Code (CSharp):
    1.  
    2.                 int raycastAmount = 5;
    3.                 int count = 0;
    4.  
    5.                 for (int x = 0; x < raycastAmount; x++)
    6.                 {
    7.                     for (int y = 0; y < raycastAmount; y++)
    8.                     {
    9.  
    10.                         positionsList.Add(new NativeList<float3>(Allocator.Persistent));
    11.  
    12.                         float xPos = x - (size / 2);
    13.                         float yPos = y - (size / 2);
    14.                         float3 newPos = position + new float3(xPos, 0, yPos);
    15.                         RayCastTestingJob rayCastTestingJob = new RayCastTestingJob()
    16.                         {
    17.                             collisionWorldJob = collisionWorld,
    18.                             sizeJob = size,
    19.                             positionJob = newPos,
    20.  
    21.                             raycastPositionListJob = positionsList[count]
    22.                         };
    23.                         raycastJobsList.Add(rayCastTestingJob);
    24.                         count = count + 1;
    25.                     }
    26.                 }
    27.                 foreach (var item in raycastJobsList)
    28.                 {
    29.                     Dependency = JobHandle.CombineDependencies(Dependency, item.Schedule()); // This line causes the error.
    30.                     raycastJobHandleList.Add(item.Schedule(Dependency));
    31.  
    32.  
    33.                 }
    34.  
    35.                 activeJobs = true;
    36.  
    37.  
    38.                 foreach (var item in raycastJobHandleList)
    39.                 {
    40.                     item.Complete();
    41.                 }
    42.  

    Code (CSharp):
    1.  
    2.         protected override void OnUpdate()
    3.         {
    4.             collisionWorld = SystemAPI.GetSingletonRW<PhysicsWorldSingleton>().ValueRW.CollisionWorld;
    5.             EntityManager.CompleteDependencyBeforeRW<PhysicsWorldSingleton>();
    6.  
    7.             float3 position = UnityEngine.GameObject.FindObjectOfType<RandomTest>().originGO.transform.position;
    8.          
    9.  
    10.             if (updateRate >= 100)
    11.             {
    12.                 TestingRaycast(updateRate, position);
    13.             }
    14.             if (updateRate == 100)
    15.             {
    16.                 UnityEngine.Debug.Log("Test Started");
    17.             }
    18.  
    19.  
    20.                 updateRate = updateRate + 1;
    21.         }
    22.  

    Code (CSharp):
    1.  
    2.     [BurstCompile] public struct RayCastTestingJob : IJob
    3.     {
    4.         [ReadOnly] public CollisionWorld collisionWorldJob;
    5.         public NativeList<float3> raycastPositionListJob;
    6.         public float3 positionJob;
    7.         public int sizeJob;
    8.  
    9.         public void Execute()
    10.         {
    11.  
    12.  
    13.             ECSRaycastTools.StandardRayCast standardRayCast = new ECSRaycastTools.StandardRayCast() { collisionWorld = collisionWorldJob };
    14.  
    15.             for (int x = 0; x < sizeJob; x++)
    16.             {
    17.                 for (int y = 0; y < sizeJob; y++)
    18.                 {
    19.                     float xPos = x - (sizeJob / 2);
    20.                     float yPos = y - (sizeJob / 2);
    21.  
    22.                     float3 newPos = positionJob + new float3(xPos, 10, yPos);
    23.                     float3 target = newPos - new float3(0, -10, 0);
    24.  
    25.                     Unity.Physics.RaycastHit raycastHit = new Unity.Physics.RaycastHit();
    26.                     if (standardRayCast.CheckRay(newPos, target, out raycastHit))
    27.                     {
    28.                         raycastPositionListJob.Add(raycastHit.Position);
    29.                     }
    30.  
    31.                 }
    32.             }
    33.  
    34.  
    35.  
    36.         }
    37.     }
    38.  
    39.  
    40. namespace ECSRaycastTools
    41. {
    42.     public enum CustomCollisionLayers : byte
    43.     {
    44.         None = 1 << 0,
    45.         SolidObjects = 1 << 1,
    46.         MovementCapableObjects = 1 << 2,
    47.     }
    48.  
    49.     [BurstCompile] public struct StandardRayCast
    50.     {
    51.         [ReadOnly] public CollisionWorld collisionWorld;
    52.  
    53.         public bool CheckRay(float3 startingPosition, float3 endPosition, out Unity.Physics.RaycastHit raycastHit)
    54.         {
    55.  
    56.             var ray = new RaycastInput()
    57.             {
    58.                 Start = startingPosition,
    59.                 End = endPosition,
    60.                 Filter = new CollisionFilter()
    61.                 {
    62.  
    63.                     BelongsTo = (uint)(CustomCollisionLayers.None),
    64.                     CollidesWith = (uint)(CustomCollisionLayers.SolidObjects | CustomCollisionLayers.MovementCapableObjects)
    65.                 }
    66.             };
    67.  
    68.             return collisionWorld.CastRay(ray, out raycastHit);
    69.         }
    70.  
    71.     }
    72. }
    73.  
     
    Last edited: Feb 3, 2023
  2. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Are you calling the TestingRaycast-Code more than once?
    If so: Are you clearing the positionsList in between those calls?
     
    FederalRazer89 likes this.
  3. FederalRazer89

    FederalRazer89

    Joined:
    Nov 24, 2019
    Posts:
    83
    It should run once. But now when i look at the OnUpdate()

    Code (CSharp):
    1.             if (updateRate >= 100)// should be ==
    2.             {
    3.                 TestingRaycast(updateRate, position);
    4.             }
    5.  
    It look like i just made a typo. Going to check when get home. Thanks for pointing that out.
     
  4. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    My guess is that you're adding the lists to positionsList multiple times, but always using 0-count when adding to your jobs.
     
    FederalRazer89 likes this.
  5. FederalRazer89

    FederalRazer89

    Joined:
    Nov 24, 2019
    Posts:
    83
    Code (CSharp):
    1.             if (updateRate == 100) // changed
    2.             {
    3.                 RaycastTesting(updateRate, position);
    4.             }
    plus some other minor stuff but mostly that i called TestingRaycast multiple times. Well good thing it was just a typo, was about to start questioning my current knowledge about Job Dependency.

    Thanks for the help saved me from unnecessary debugging over the weekend.
     
    SF_FrankvHoof likes this.