Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to use List<Vector3>.Contain() function with Unity Jobs system?

Discussion in 'Entity Component System' started by MagicianArtemka, Mar 16, 2020.

  1. MagicianArtemka

    MagicianArtemka

    Joined:
    Jan 15, 2019
    Posts:
    46
    Hello everybody :)
    I started to learn about Unity Jobs and have some problems with Burst compilator.
    So, I have a simple job

    Code (CSharp):
    1. private struct JobCheckSun : IJobParallelForTransform
    2.     {
    3.         public NativeArray<int> have_sun;
    4.         public NativeArray<int> cell_lvl;
    5.  
    6.         public void Execute(int index, TransformAccess transform)
    7.         {
    8.             var pos = transform.position;
    9.             var lvl = 3;
    10.            
    11.             for (var i = 0; i < 3; i++)
    12.             {
    13.                 pos += Vector3.up;
    14.                 if (WorldController.CheckCoords(pos))
    15.                 {
    16.                     --lvl;
    17.                 }
    18.             }
    19.            
    20.             if (lvl == 0)
    21.             {
    22.                 have_sun[0] = 0;
    23.             }
    24.             else
    25.             {
    26.                 have_sun[0] = 1;
    27.             }
    28.  
    29.             cell_lvl[0] = lvl;
    30.         }
    31.     }
    WorldController.CheckCoords(pos)[/code] in 14th line is static readonly function from another static script

    Code (CSharp):
    1. public static bool CheckCoords(Vector3 need_coord)
    2.     {
    3.         return _coordMap.Contains(need_coord);
    4.     }
    The job works fine, except when I try to use [BurstCompile] tag. What I do this, Unity get an error

    C:\projects\3D-tree-evolution\Assets\Scripts\WorldController.cs(130,9): Burst error BC1016: The managed function `System.Collections.Generic.List`1<UnityEngine.Vector3>.Contains(System.Collections.Generic.List`1<UnityEngine.Vector3>* this, UnityEngine.Vector3 item)` is not supported

    at WorldController.CheckCoords(UnityEngine.Vector3 need_coord) (at C:\projects\3D-tree-evolution\Assets\Scripts\WorldController.cs:130)
    at CellController.JobCheckSun.Execute(CellController.JobCheckSun* this, int index, UnityEngine.Jobs.TransformAccess transform) (at C:\projects\3D-tree-evolution\Assets\Scripts\CellController.cs:121)
    at UnityEngine.Jobs.IJobParallelForTransformExtensions.TransformParallelForLoopStruct`1<CellController.JobCheckSun>.Execute(ref CellController.JobCheckSun jobData, System.IntPtr jobData2, System.IntPtr bufferRangePatchData, ref Unity.Jobs.LowLevel.Unsafe.JobRanges ranges, int jobIndex)


    While compiling job: System.Void UnityEngine.Jobs.IJobParallelForTransformExtensions/TransformParallelForLoopStruct`1<CellController/JobCheckSun>::Execute(T&,System.IntPtr,System.IntPtr,Unity.Jobs.LowLevel.Unsafe.JobRanges&,System.Int32)
    at C:\projects\3D-tree-evolution\Assets\Scripts\WorldController.cs:line 130

    What do I need to do? Can somebody explain to me, how I can change List<Vector3> type to something else (maybe NativeList, idk) to make it works?

    Thank you for help :)
     
  2. Sarkahn

    Sarkahn

    Joined:
    Jan 9, 2013
    Posts:
    440
  3. tim_jones

    tim_jones

    Unity Technologies

    Joined:
    May 2, 2019
    Posts:
    287
    You could use something like NativeArray - there's a `Contains` extension method for it.

    Code (CSharp):
    1. NativeArray<Vector3> _coordMap; // Initialize this somewhere
    2.  
    3. public static bool CheckCoords(Vector3 needCoord)
    4. {
    5.     return _coordMap.Contains(needCoord);
    6. }