C# - How to create dictionaries

Godot Version 4.2.1 Mono

Question

I want to create a dictionary like from the JSON code below:

{
 "0": {
  "NAME": "Stick",
  "TEXTURE": "stick.png",
  "AMOUNT": "16",
  "DESCRIPTION": "Веточка... похоже дерева, с которого она упала, уже нет"
 },
 "1": {
  "NAME": "Wood",
  "TEXTURE": "wood.png",
  "AMOUNT": "32",
  "DESCRIPTION": "Материал для небольших построек. Хорошо горит"
 },
 "2": {
  "NAME": "Stone",
  "TEXTURE": "stone.png",
  "AMOUNT": "32",
  "DESCRIPTION": "Небольшой камень. Возможно из него можно много чего сделать"
 }
}

However, I don’t fully understand the “syntax” of dictionaries. I tried to do the same as with JSON file, however it didn’t work. help :slight_smile:

1 Like

new Godot.Collections.Dictionary<int, Godot.Collections.Dictionary<string,string>>

Example

Godot.Collections.Dictionary<int, Godot.Collections.Dictionary<string,string>> Dictionary = new();

Collections.Dictionary<string,string> InnerValue = new(){};

InnerValue.Add(“Test”,“data”);
Dictionary.Add(“0”,InnerValue);

Have not tested it though, but it should be something like that i think - whether or not its best practice i have no idea. But it should get the work done.

With that structure tho a array wrapping a dictionary is prob. better than double dictionaries. :slight_smile:

I forgot to specify the type (string) before accessing the main dictionary, so it didn’t work, but thanks anyway

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.