Search Unity

  1. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Accessing a 2D grid of int2 in a Job

Discussion in 'Data Oriented Technology Stack' started by theodor349, Feb 18, 2020.

  1. theodor349

    theodor349

    Joined:
    Mar 6, 2016
    Posts:
    11
    I am working on pathfinding where a lot of units use different maps to determine which way they should go. (Flow Field Pathfinding)

    So, in my MoveSystem, I need to access all the maps (there could be more than 100 maps).

    To do this I need a two-dimensional NativeArray. However, I currently do not know how to make this if it is even possible.

    Code (CSharp):
    1. public class MoveSystem : JobComponentSystem
    2. {
    3.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    4.     {
    5.         // Get Flow Maps (Currently reference type and not Value type)
    6.         var maps = new int2[PathfindingSystem.FlowfieldMaps.Count][];
    7.         for (int i = 0; i < maps.Length; i++)
    8.         {
    9.             maps[i] = new int2[PathfindingSystem.FlowfieldMaps[i].Length];
    10.             for (int j = 0; j < PathfindingSystem.FlowfieldMaps[i].Length; j++)
    11.             {
    12.                 maps[i][j] = PathfindingSystem.FlowfieldMaps[i][j];
    13.             }
    14.         }
    15.         var deltaTime = UnityEngine.Time.deltaTime;
    16.  
    17.         // Job
    18.         inputDeps = Entities.ForEach((MoveSpeed moveSpeed, PathMapIndex pathMapIndex, ref Translation translation) =>
    19.         {
    20.             var dir = maps[pathMapIndex.Value][GetIndex(translation)];
    21.             var offset = new float3();
    22.  
    23.             offset.x += dir.x * deltaTime;
    24.             offset.z += dir.y * deltaTime;
    25.             offset *= moveSpeed.Value;
    26.        
    27.             translation.Value += offset;
    28.         }).Schedule(inputDeps);
    29.  
    30.         return inputDeps;
    31.     }  
    32. }
    If you know of a better way to handle this amount of data I am open for suggestions :D
     
  2. Sarkahn

    Sarkahn

    Joined:
    Jan 9, 2013
    Posts:
    277
    You can represent a 2d array as a 1d array by constructing it with a size of width * height and accessing indices via
    y * width + x
    .
     
  3. theodor349

    theodor349

    Joined:
    Mar 6, 2016
    Posts:
    11
    @Sarkahn yea that is right I could do something like that. However, all the Maps might not be the same size.
    But, I could probably get around by having another array which stores the size of each map and then uses that as my width.
     
  4. siggigg

    siggigg

    Joined:
    Apr 11, 2018
    Posts:
    247
    I recently implemented a system that does exactly this (for influence maps). I used a big array that holds all the data with an allocate function that can also grow the combined array if needed. Then I keep a separate list of structs holding information about the maps, including start index in the combined array, size etc. It works really well and is super fast.
     
    Sarkahn likes this.
  5. theodor349

    theodor349

    Joined:
    Mar 6, 2016
    Posts:
    11
    @siggigg That is great. Then that is what I will do.
     
    siggigg likes this.
  6. siggigg

    siggigg

    Joined:
    Apr 11, 2018
    Posts:
    247
    btw @theodor349 do your maps ever change or do you deal with dynamic obstacles? If they are static then you could go one step further and bake the maps into blob arrays.
     
    Kuptsevych-Yuriy likes this.
  7. Kuptsevych-Yuriy

    Kuptsevych-Yuriy

    Joined:
    Oct 1, 2008
    Posts:
    17
  8. theodor349

    theodor349

    Joined:
    Mar 6, 2016
    Posts:
    11
    @siggigg no my maps do change, but it is not all the time maybe once every few seconds. So that might be a good optimizations. Any good references other than their documentation?

    @Kuptsevych-Yuriy That sure is interesting. But what I need is a dynamic array where each sub-array might not be the same length. I that something your library can do because I donøt seem to be able to find that.
     
  9. siggigg

    siggigg

    Joined:
    Apr 11, 2018
    Posts:
    247
    @theodor349 Blobs are mainly used for immutable data. This talk is still one of the best examples of that (towards the end of it):
     
    Kuptsevych-Yuriy likes this.
  10. theodor349

    theodor349

    Joined:
    Mar 6, 2016
    Posts:
    11
    Thanks, @siggigg that sure is something I can use.
     
    siggigg likes this.
unityunity