Question about dictionaries and retrieving data

Hi there!

First of all, my IDE is based on Unity 3.5.7 pro + ios pro + android pro (can’t upgrade right now :stuck_out_tongue: not enough money).

Now, I’m parsing an XML document where I have several data, and I store everything inside a big dictionary with several sub-dictionaries, this is the structure:

GAMES
--GAME key:"game01" value: object_dictionary_game
----LEVEL key: "level01" value: object_dictionary_level
------NAME key:"name" value:"my_level_01"
------LEVEL_OPTIONS key: "option01" value: options_dictionary_option
------LEVEL_OPTIONS key: "option02" value: options_dictionary_option
------LEVEL_OPTIONS key: "option03" value: options_dictionary_option
----LEVEL key: "level02" value: object_dictionary_level
------NAME key:"name" value:"my_level_02"
------LEVEL_OPTIONS key: "option01" value: options_dictionary_option
------LEVEL_OPTIONS key: "option02" value: options_dictionary_option
------LEVEL_OPTIONS key: "option03" value: options_dictionary_option
--GAME key:"game02" value: object_dictionary_game
----LEVEL key: "level01" value: object_dictionary_level
------NAME key:"name" value:"my_level_01"
------LEVEL_OPTIONS key: "option01" value: options_dictionary_option
------LEVEL_OPTIONS key: "option02" value: options_dictionary_option
------LEVEL_OPTIONS key: "option03" value: options_dictionary_option
----LEVEL key: "level02" value: object_dictionary_level
------NAME key:"name" value:"my_level_02"
------LEVEL_OPTIONS key: "option01" value: options_dictionary_option
------LEVEL_OPTIONS key: "option02" value: options_dictionary_option
------LEVEL_OPTIONS key: "option03" value: options_dictionary_option
--GAME key:"game03" value: object_dictionary_game
----LEVEL key: "level01" value: object_dictionary_level
------NAME key:"name" value:"my_level_01"
------LEVEL_OPTIONS key: "option01" value: options_dictionary_option
------LEVEL_OPTIONS key: "option02" value: options_dictionary_option
------LEVEL_OPTIONS key: "option03" value: options_dictionary_option
----LEVEL key: "level02" value: object_dictionary_level
------NAME key:"name" value:"my_level_02"
------LEVEL_OPTIONS key: "option01" value: options_dictionary_option
------LEVEL_OPTIONS key: "option02" value: options_dictionary_option
------LEVEL_OPTIONS key: "option03" value: options_dictionary_option

Inside a GAME, a LEVEL and LEVEL OPTIONS there are several individual options, but apart those “nodes” I put there are also dictionaries, of course there are several games, levels and options, I hope I’m explaining this enough.

So far so good, the thing comes when I want to access those dictionaries, it should be easy, but I want to be able to list all the games that exists inside a dictionary, I want this in the console, but probably I’ll be using this in the future in the UI or something like that.

For example something like:

Dictionary GAMES

GAME01:object_dictionary_game
GAME02:object_dictionary_game
GAME03:object_dictionary_game

Of course I also want to list levels and options, for example:

Dictionary GAMES_GAME01

LEVEL01:object_dictionary_level
LEVEL02:object_dictionary_level

Dictionary GAMES_GAME01_LEVEL01

NAME:"my level 01"
OPTION01:object_dictionary_option
OPTION02:object_dictionary_option
OPTION03:object_dictionary_option

I hope my explanation is clear, so far I construc mi dictionaries using:

Dictionary<string,object> games = new Dictionary<string, object> ();

I think I know how to extract the data from the object and convert it to a boolean, and int or a string (which is what I need).

I know also how to get the count of “nodes”, for example GAMES.count would return 3, but I want to know the identifiers, the keys of those 3.

In the end I just need how to use the dictionaries to retrieve their data, i’ve looked into the MSDN library, but it is not too clear to me :stuck_out_tongue:

Hope someone can help.

Cheers.

you can foreach a dictionary

Dictionary<string, object> games = new Dictionary<string, object> ();
// read data here
foreach (var item in games)
   Debug.Log(item.key + " maps to " + item.value);

when your value type is object you need to explicitely cast it to the dictionary type of your choice/need. better you directly define it the right way.

Dictionary<string,bool> options;
Dictionary<string, Dictionary<string, int> > players;  // note the space between > and >, it is required to distinguish it from the binary shift operator

Interesting, I’ve never had to put a space between ‘>>’ when declaring a nested generic like that.

It’s only necessary in Unityscript (due to an oversight in the compiler), not C#.

–Eric

I’m not sure about what you mean with the second code group, I’m casting the “object” values when I reach them.

I did something similar to your solution before I saw your post :slight_smile:

Here is my solution:

		foreach (KeyValuePair<String,object> item in juegos)
		{
			Debug.Log(item.Key + ": " + item.Value);
		}
		
		//To get values of a dictionary inside another dictionary, "001" is the key where the dictionary is stored
		Dictionary<string,object> my_picked_game = juegos["001"] as Dictionary<string,object>;
			
		foreach (KeyValuePair<String,object> item in my_picked_game)
		{
			Debug.Log(item.Key + ": " + item.Value);
		}
	}

so when you cast them to the required type why don’t you let the dictionary store the required type and only cast it once when filling the dictionary (if it is required at all as i assume the correct type is restored from xml but i never used xml so i don’t know)? you mentioned that you need ints and bools so let your dictionary store them and don’t cast them each time you access them. this would make your code more type safe and would allow the compiler to check and optimize the stuff. casting is considered bad practice and should be avoided when possible. so if you have no good reason to use it you can only win.

unfortunately i cannot sense wether this is sarcasm/irony or not. please state explicitely.

thanks for clarification. i only remember i had an issue once with that but i don’t remember the correct circumstances (language). thats why i put the space there and be on the safe side.

Yeah - sorry. Definitely wasn’t sarcastic or anything. Genuinely found it odd. :slight_smile:

The thing is that inside the dictionary I get different types of values, I have standard strings and dictionaries, so I can’t cast on the creation of the dictionary, that is why I use object.

How can I create a dictionary where I can have, ints, bools, strings and dictionaries? the “object” way is the only way i can think about :stuck_out_tongue:

Cheers.

Why don’y you serialize the data to your own Game class and use a Dictionary to lookup based on game name?

i’m new to XML inside Unity.

I’ve used XML before, but just in ObjectiveC, and there I had a custom class created, I’m not too sure of what “serialize” means, and if I can create a custom class for the games, any tutorial or idea in how to optimize this is very welcome :slight_smile:

Cheers.

Unity doesn’t do anything different than .NET/Mono in terms of classes or XML serialization. Check out the XmlSerializer documentation and examples on MSDN.

how do you determine wich type to cast into? my suggestion would be to use multiple dictionaries each with its proper type and then store the key in a dataset to build a relation. or encapsulate the data in a cretain class which is used in your dictionary and which handles casting for you. but honestly i do not understand your setup and your specific requirements. when you don’t need the feature of using any type as indexer you could also store the data in specific lists and the elements connect to the entries in the lists by storing the index.