Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Enumerating a Dictionary value of type List<List<int>>

Discussion in 'Scripting' started by sdviosx, Jan 17, 2015.

  1. sdviosx

    sdviosx

    Joined:
    Jan 4, 2015
    Posts:
    22
    Hello,

    I need help enumerating a dictionary value that is of type List<List<int>>, I want the content within the value which consist of a list of ones and zeros which define the structure of the level.

    I am approaching this by getting the key "level" which returns an object. I then try to assign that value of type object to a list, so I type cast it to List<List<int>> so i can then enumerate and structure the level based on what that value returns. The problem is that before I even begin enumerating the key "level", the list that is assigned the key value "level" returns null.

    Here is a function to test out some values, since it returns null I did not bother to actually try and enumerate the level until I find a proper way of getting the dictionary value without it returning null.
    Code (CSharp):
    1. void Test(){
    2.         /* Here is the content of the Level_1 JSON file
    3.          I would like to enumerate the ones and zeros
    4.         "level" : [ [1, 1, 1, 1, 1, 1, 1, 1, 1 ],
    5.                     [0, 0, 1, 1, 1, 1, 1, 0, 0 ],
    6.                     [1, 1, 1, 1, 1, 1, 1, 1, 1 ],
    7.                     [1, 1, 1, 1, 1, 1, 1, 1, 1 ],
    8.                     [0, 0, 0, 1, 1, 1, 0, 0, 0 ],
    9.                     [1, 1, 1, 1, 1, 1, 1, 1, 1 ],
    10.                     [1, 1, 1, 1, 1, 1, 1, 1, 1 ],
    11.                     [1, 1, 1, 0, 0, 0, 1, 1, 1 ],
    12.                     [1, 1, 1, 0, 0, 0, 1, 1, 1 ] ],
    13.      
    14.         "score" : 10,
    15.         */
    16.  
    17.         // Helper method, takes Json file and deserializes it as Dictionary<string,dynamic>
    18.         // The dictionary value is of type dynamic because the json file contains a list and an int, idk if this might be a problem
    19.         Dictionary<string,dynamic> dictionary = LoadJSONFile("Level_1");
    20.  
    21.         // Get level key value, have to type cast to List<List<int>> else it returns of type object
    22.         var level = dictionary["level"] as List<List<int>>;
    23.         var newList = new List<List<int>>(level); // This returns null
    24.  
    25.         // Get score key value
    26.         var score = dictionary["score"];
    27.         // Get level key value
    28.         var rawLevel = dictionary["level"];
    29.  
    30.         Debug.Log("score is: " + score);     // Logs: score is: 10
    31.         Debug.Log("level: " + newList);      // returns null, error message: Argument cant be null
    32.         Debug.Log("raw level: " + rawLevel); // Logs: System.Collections.Generic.List`1[System.Object]
    33.  
    34.     }
    Thanks in advance.
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    I'm pretty sure you cannot simply cast from int[][] to List<List<int>>, even if you try to circumvent this via dynamic object.
    It'll compile without error, but you get nothing in the variable.
    You'll need to cast to the proper type (probably int[][], might be int[,]), iterate manually and create lists yourself.
     
  3. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    Couple of concerns about your implementation:
    You're trying to recreate a binary system using a high level programming language and integers. This is super memory inefficient even before you store it all in a list of lists. There are lots of better ways to do this. XML serialization, plain text, just about anything will be better than trying to hack together your own binary format.
     
  4. sdviosx

    sdviosx

    Joined:
    Jan 4, 2015
    Posts:
    22
    I am going to consider using the plain text method if I can't manage to properly type cast the key. not sure of XML, I keep hearing of compatibility issue with IOS.