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 loops in Job System

Discussion in 'C# Job System' started by Rewaken, Nov 19, 2018.

  1. Rewaken

    Rewaken

    Joined:
    Mar 24, 2015
    Posts:
    126
    Hello, I want to calculate the distance between the list of game objects So element will check its distance with every other element and so on, I am doing this with following code it works but not efficient enough. Can anyone tell me the correct way to use loops in job system.
    Code (CSharp):
    1. struct Field: IJobParallelForTransform
    2.     {
    3.         public NativeArray<float> Dist;
    4.         public Vector3 PlayerPos;
    5.  
    6.         public void Execute(int i, TransformAccess transform)
    7.         {
    8.  
    9.               Dist[i] = Vector3.Distance(PlayerPos, transform.position);
    10.  
    11.            
    12.         }
    13.     }
    Code (CSharp):
    1.     public List<Transform> Childrens;//All gameobjects list
    2.  
    3.     public TransformAccessArray ALlObjects;
    4.  
    5.  
    6.  
    7.     NativeArray<float> distances;
    8.  
    9.  
    10.  
    11.     void Start ()
    12.  
    13.     {
    14.  
    15.  
    16.  
    17.         ALlObjects = new TransformAccessArray(0, -1);
    18.  
    19.        
    20.  
    21.         distances = new NativeArray<float>(Childrens.Count, Allocator.Persistent);
    22.  
    23.  
    24.  
    25.     }
    26.  
    27.  
    28.  
    29.        void Update()
    30.  
    31.     {
    32.  
    33.         for (int i = 0; i < Childrens.Count; i++)
    34.  
    35.         {
    36.  
    37.             Field job1 = new Field()
    38.  
    39.             {
    40.  
    41.                 Dist = distances,
    42.  
    43.                 PlayerPos= Childrens[i].position,
    44.  
    45.  
    46.  
    47.             };
    48.  
    49.             JobHandle jH = job1.Schedule(ALlObjects);
    50.  
    51.             jH.Complete();
    52.  
    53.             for (int j = 0; j < distances.Length; j++)
    54.  
    55.             {
    56.  
    57.  
    58.  
    59.                 if (distances[j] <= 0.8f && distances[j] > 0)
    60.  
    61.                 {
    62.  
    63.                  
    64.  
    65.                     break;
    66.  
    67.                 }
    68.  
    69.                 if (distances[j] > 0.8f )
    70.  
    71.                 {
    72.  
    73.            
    74.  
    75.                 }
    76.  
    77.  
    78.  
    79.             }
    80.  
    81.  
    82.  
    83.         }
    84.  
    85.     }
     
  2. meanmonkey

    meanmonkey

    Joined:
    Nov 13, 2014
    Posts:
    148
  3. Matt_De_Boss_Developer

    Matt_De_Boss_Developer

    Joined:
    Oct 17, 2014
    Posts:
    46
    Also, consider what you are doing when you are calling Job.Complete(). Essentially, you are forcing the main thread to wait for your job to finish, which kind of destroys the entire purpose of what you are doing here. Make sure that you call Schedule() at the beginning of your game logic updates, and then call Complete() at the end of your update loops. Otherwise, call Complete() after some time has passed to be clever.
     
  4. Rewaken

    Rewaken

    Joined:
    Mar 24, 2015
    Posts:
    126
    Hello, In last thread I was checking distance of player against all other objects but here I am checking every objects distance against eachother. I thought rather than calling job from every single object I can use loop and check from main parent object
     
  5. Rewaken

    Rewaken

    Joined:
    Mar 24, 2015
    Posts:
    126
    Thanks, I changed the code and it helps performance