C# - How to create Godot.Collections.Dictionary

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Supatier

Im trying to add a settings to the project with ProjectSettings.AddPropertyInfo() the input is Godot.Collections.Dictionary but i don’t know how to use it. I know how to create a dictionary in C# with new Dictionary<string, object>() but godot doesn’t accept it.

:bust_in_silhouette: Reply From: Zylann

Such dictionary works like a C# dictionary, except keys and values are both object. It doesn’t implement IDictionary for some reason, maybe it will in the future.

See https://godotsharp.net/api/3.1.0/Godot.Collections.Dictionary/

This is how you create one:

var d = new Godot.Collections.Dictionary();

d.Add("name", "property_name");

// In GDScript it's TYPE_*, I believe GodotSharp has it in Variant.Type.*
d.Add("type", Variant.Type.String);


And these are the expected keys and values:

Tbh it would be nice if a struct could be accepted there, because this function expects a specific set of keys and values and in C# it forces you to build this quite unexpected dictionary. I guess it’s a dict because it’s less work to bind and was easier so far to write in GDScript…

It worked. Thanks

Supatier | 2019-12-12 00:20

Unfortunately, this gives me the following error in Godot 4 beta1:

Argument type 'Godot.Variant.Type' is not assignable to parameter type 'Godot.Variant'

structed | 2022-09-17 22:36