Search Unity

NativeArray for Jobs System Need Help

Discussion in 'Entity Component System' started by kiwixiaosheng, Jun 8, 2022.

  1. kiwixiaosheng

    kiwixiaosheng

    Joined:
    Mar 15, 2022
    Posts:
    5
    Hello, this is my first post

    Is there a work around to this code to make it work?

    Code (CSharp):
    1. NativeArray<float[,]> MatricesResults = new NativeArray<float[,]>(Matrices.Count, Allocator.Persistent);
    Error I get:
    The type 'float[*,*]' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'NativeArray<T>'

    I want to get multidimensional arrays as result using the Jobs System.
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Hi, can't have native collections inside native collections, unless its an unsafe one.
    Also, no managed types are available either.

    Solution to this example however, is to convert multi-dimensional array to a single dimensional array.

    Size will be [row * column], index would be [index / row, index % row] + you'd be able to utilize NativeArray<float>.
    (actual index access may vary, and depend on implementation)
     
    Last edited: Jun 8, 2022
    kiwixiaosheng and Antypodish like this.
  3. kiwixiaosheng

    kiwixiaosheng

    Joined:
    Mar 15, 2022
    Posts:
    5
    Hello, thank you.

    I'll give it a try and see if it works :D
     
  4. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Forgot to mention, if you need matrices - there's float2x2 or float3x3, so you could just use that instead. Its part of Unity Mathematics.

    https://docs.unity3d.com/Packages/com.unity.mathematics@0.0/api/Unity.Mathematics.float2x2.html
    https://docs.unity3d.com/Packages/com.unity.mathematics@0.0/api/Unity.Mathematics.float3x3.html
    https://docs.unity3d.com/Packages/com.unity.mathematics@0.0/api/Unity.Mathematics.float4x4.html

    Those are structs, so they'd work with NativeArray just fine. Plus burst compatible.
     
    Last edited: Jun 8, 2022
  5. kiwixiaosheng

    kiwixiaosheng

    Joined:
    Mar 15, 2022
    Posts:
    5
    ok, I finally settled with NativeMultiHashmap<int, float> MatriceResults
    so i can have multiple multidimensional array as results in a IJobParallelFor.

    the [row * column] seems to be working fine, Thank You <3

    though I find it weird that the values are reversed after I put them into the NativeMultiHashMap.
    So I just put the results in a List first and traverse it backwards.
     
  6. Soaryn

    Soaryn

    Joined:
    Apr 17, 2015
    Posts:
    328
    Iirc the hashmap is non-ordererd where I believe they have a hashset that is order bound. I remember seeing those in the collection package docs