Search Unity

[Solved] NativeContainer result has not been assigned or constructed (but i did)

Discussion in 'Data Oriented Technology Stack' started by PJRM, Aug 15, 2018.

  1. PJRM

    PJRM

    Joined:
    Mar 4, 2013
    Posts:
    287
    Hi. the title says everything. here the details:
    The error:

    error.jpg
    The IJob script:
    Code (CSharp):
    1. using Surviver.Data;
    2. using Unity.Collections;
    3. using Unity.Jobs;
    4.  
    5. namespace Surviver.Jobs {
    6.    public struct MapGenRandomFillJob: IJob {
    7.       [ReadOnly] public int hashcode;
    8.       [ReadOnly] public int size;
    9.       [ReadOnly] public int fillTerrainPercent;
    10.       public NativeList<MapGeneratorTerrain> result;
    11.  
    12.       public void Execute() {
    13.          result = new NativeList<MapGeneratorTerrain>();
    14.          System.Random pseudoRandom = new System.Random(hashcode);
    15.  
    16.          for (int x = 0; x < this.size; ++x) {
    17.             for (int y = 0; y < this.size; ++y) {
    18.                if (x == 0 || x == size - 1 || y == 0 || y == size - 1) {
    19.                   result.Add(this.CreateMGT(x, y, TerrainType.Water));
    20.                } else {
    21.                   TerrainType TT = (pseudoRandom.Next(0, 100) < fillTerrainPercent) ? TerrainType.Terrain : TerrainType.Water;
    22.                   result.Add(this.CreateMGT(x, y, TT));
    23.                }
    24.             }
    25.          }
    26.       }
    27.  
    28.       /// <summary>
    29.       /// Creates new Terrain data for position X, Y
    30.       /// </summary>
    31.       /// <param name="x"></param>
    32.       /// <param name="y"></param>
    33.       /// <param name="type"></param>
    34.       /// <returns></returns>
    35.       private MapGeneratorTerrain CreateMGT(int x, int y, TerrainType type) {
    36.          return new MapGeneratorTerrain() {
    37.             TerrainType = type,
    38.             X = x,
    39.             Y = y
    40.          };
    41.       }
    42.    }
    43. }
    My method which have the error (The line of the error is in the "JobHandle handle = jobData.Schedule();"):

    Code (CSharp):
    1. private List<MapGeneratorTerrain> RandomFill(MapGeneratorComponent MGComp) {
    2.    //int size = MGComp.mapSize;
    3.    string seed = MGComp.seed;
    4.    if (MGComp.useRandomSeed)
    5.       seed = UnityEngine.Time.time.ToString();
    6.    /*
    7.    //System.Random pseudoRandom = new System.Random(seed.GetHashCode());*/
    8.    List<MapGeneratorTerrain> Ts = new List<MapGeneratorTerrain>();
    9.    #region job
    10.    NativeList<MapGeneratorTerrain> resultData = new NativeList<MapGeneratorTerrain>();
    11.  
    12.    MapGenRandomFillJob jobData = new MapGenRandomFillJob();
    13.    jobData.fillTerrainPercent = MGComp.fillTerrainPercent;
    14.    jobData.hashcode = seed.GetHashCode();
    15.    jobData.size = MGComp.mapSize;
    16.    jobData.result = resultData;
    17.  
    18.    JobHandle handle = jobData.Schedule();
    19.    handle.Complete();
    20.  
    21.    for (int i = 0; i < resultData.Length; ++i) {
    22.       Ts.Add(resultData[i]);
    23.    }
    24.  
    25.    resultData.Dispose();
    26.    #endregion
    27.    /*
    28.    for (int x = 0; x < size; ++x) {
    29.       for (int y = 0; y < size; ++y) {
    30.          if (x == 0 || x == size-1 || y == 0 || y == size-1) {
    31.             Ts.Add(this.CreateMGT(x, y, TerrainType.Water));
    32.          } else {
    33.             TerrainType TT = (pseudoRandom.Next(0, 100) < MGComp.fillTerrainPercent) ? TerrainType.Terrain : TerrainType.Water;
    34.             Ts.Add(this.CreateMGT(x, y, TT));
    35.          }
    36.       }
    37.    }*/
    38.  
    39.    return Ts;
    40. }
    Am i missing something here? i'm following the documentation proccess step by step. i even initialized the variable resultData and assign it to the job. What I am doing wrong?
     
  2. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,310
    Try to remove this in the job

    result = new NativeList<MapGeneratorTerrain>();

    you already create the NativeList outside.
     
    PJRM likes this.
  3. Fido789

    Fido789

    Joined:
    Feb 26, 2013
    Posts:
    331
    I think you must use the other constructor to initialize NativeList, the default one without the params is there only because struct requires it.
     
    PJRM likes this.
  4. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    1,721
    As other commentators have said, you need an Allocator when defining NativeContainers
     
    PJRM likes this.
  5. PJRM

    PJRM

    Joined:
    Mar 4, 2013
    Posts:
    287
    Thank you i forgot to remove that try. hehehe. It isn't that.
    Indeed. that's it!

    Thank you so much!