Search Unity

How To Resize Array For Job System?

Discussion in 'Data Oriented Technology Stack' started by ajrosenbach, Dec 17, 2018.

  1. ajrosenbach

    ajrosenbach

    Joined:
    Oct 29, 2018
    Posts:
    3
    Hi! New programmer here! I've been experimenting with the Job System and I think it is super cool! However, I've been struggling to find a way to resize a NativeArray for when it runs into it's max length. The way I have it written now is as follows:
    Code (CSharp):
    1.     private void ResizeNativeArray(NativeArray<float> na)
    2.     {
    3.         maxSize *= scaleFactor;
    4.         NativeArray<float> nativeArray = new NativeArray<float>(maxSize, Allocator.Persistent);
    5.         moveHandle.Complete();
    6.         randNums.CopyFrom(nativeArray);
    7.     }
    Unfortunately I am still getting an index out of bounds error and an ArgumentException. This is probably really simple to fix and I'm probably just being somewhat stupid. Unfortunately, there are almost no resources on how to do this!

    I would like to add that the transforms array can be added dynamically, is there a way to replicate this behavior?
    Code (CSharp):
    1.         transforms.Add(bat.transform);
    Edit: I am a fool! I did some research with some C code as what they do with resizing arrays is very similar and here's the fixed code!
    Code (CSharp):
    1.     private void ResizeNativeArray()
    2.     {
    3.         maxSize *= scaleFactor;
    4.         NativeArray<float> nativeArray = new NativeArray<float>(maxSize, Allocator.Persistent);
    5.         moveHandle.Complete();
    6.         randNums.Dispose();
    7.         randNums = nativeArray;
    8.     }
     
    Last edited: Dec 17, 2018
  2. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,261
    You may use NativeList which automatically resize itself x2 capacity when over the limit.
     
  3. ajrosenbach

    ajrosenbach

    Joined:
    Oct 29, 2018
    Posts:
    3
    Does NativeList work with the Job System? I thought it didn't.
     
  4. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    521
    Sure it works!
    But you can't use it in concurrent way like native arrays.

    So you can create a single job that is writing to a List but you can't use it with IParallel for.
    Parallel reading is ok!
     
    ajrosenbach likes this.
  5. ajrosenbach

    ajrosenbach

    Joined:
    Oct 29, 2018
    Posts:
    3
    Oh! I see, you just have to mark it [ReadOnly] that cleans up a bunch of my code and most likely makes it safer, thanks!
     
    Spy-Shifty likes this.
  6. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    4,646
    You can cast a NativeList to NativeArray, also if you have a job with a dependency that might resize the list, you can use ToDeferredArray(); to make the job system extract the length & pointer at the time the job runs, not when the job is scheduled.