I've been a Unity dev (hobbyist) for a few years now. Recently I decided to make the switch to Godot, and while I'm really enjoying it, Dictionaries seem a bit weird to me.

I'm used to the statically-typed Dictionary<Tkey,Tvalue> class, but as of Godot 4.beta12, they apparently can't be exported to the editor.
Godot has its own Dictionary, which can be exported (kinda), but is not statically typed and is implemented differently.

Can Godot Dictionaries be typed?
If not, is there a way to get the standard Dictionary to play nice with Godot? Or should I just suck it up and re-learn the new one?

  • TwistedTwigleg replied to this.
  • MoxieCat Godot Dictionaries be typed?

    Unfortunately, I do not think so. Godot’s dictionary is dynamic like GDScript and I do not think there is any way to type constrain it in C# or GDScript.

    If you still need a key-value pair but exported to the Godot editor, what you can do is make two typed lists and export those, and then in the _ready function construct a dictionary from the data (and then wipe the lists to save a tiny bit of memory). It is not super elegant but I have done that method a few times to export dictionaries with types.

    Now I could be completely off on this but my understanding is the dictionaries 'type' would be 'dictionary' and it'd likely be individual keys that could be of a certain type such as 'int' or 'float' or 'string' or such. But I tend to stick to GDScript as a dynamic language usually so I haven't really experimented much with typed scripting. Maybe typed arrays might work better for you?

    MoxieCat Godot Dictionaries be typed?

    Unfortunately, I do not think so. Godot’s dictionary is dynamic like GDScript and I do not think there is any way to type constrain it in C# or GDScript.

    If you still need a key-value pair but exported to the Godot editor, what you can do is make two typed lists and export those, and then in the _ready function construct a dictionary from the data (and then wipe the lists to save a tiny bit of memory). It is not super elegant but I have done that method a few times to export dictionaries with types.