Search Unity

NativeArray<T>[,] can't add anything

Discussion in 'Entity Component System' started by PiterQ70, Dec 13, 2020.

  1. PiterQ70

    PiterQ70

    Joined:
    Mar 3, 2015
    Posts:
    82
    Hi. I have urgent problem :)

    Why I can't add value to given index in NativeArray?


    Code (CSharp):
    1.  
    2. NativeArray<int>[,] map = new NativeArray<int>[width, height];
    3. /.../
    4. map[x,y] = 1; < //error ??
    Also how can I check if?

    Code (CSharp):
    1. map[x,y] == 1  //?? Error
     
    Last edited: Dec 13, 2020
  2. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    You created a 2D array of native arrays of ints. Each index in map is an uninitialized native array. I don't know what your goal is mixing arrays and native arrays, but maybe you could just use arrays instead?
     
  3. PiterQ70

    PiterQ70

    Joined:
    Mar 3, 2015
    Posts:
    82
    I try understund jobs :) I try convert my current project to use jobs system. Everywhere they use NativeArrays. Now I have
    Code (CSharp):
    1. int[,]
    array.
     
  4. nyanpath

    nyanpath

    Joined:
    Feb 9, 2018
    Posts:
    77
    Use flat arrays and subindices.
     
  5. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,770
    As other pointed out, you try to use 2D array on 1D Native Array of ints. NativeArray can only be 1D. So you need convert your 2D array into flattened 1D array and use index offset, to access rows and columns.
     
  6. nyanpath

    nyanpath

    Joined:
    Feb 9, 2018
    Posts:
    77
    I think maybe if the data amount is unpredictable it would be better to use a NativeList instead and keep track of the indices for whenever there is a new part added.