godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
91.55k stars 21.27k forks source link

Improve enum usage in Dictionary exports for C# #28536

Open JarLowrey opened 5 years ago

JarLowrey commented 5 years ago

Godot version:

3.1.1

OS/device including version:

Win10

Issue description:

Feature request:

I am trying to export a dictionary of enum: float. In the editor my enum shows up as an int, instead of a string. This makes it difficult to use. There is also no auto-complete available. If possible these would be great features to have.

Steps to reproduce:

[Export]
public Godot.Collections.Dictionary Weights = new Godot.Collections.Dictionary() {
    {EnumName1, 0.5}
};

Appears as the following in the editor

image

If you type the dictionary, it will correctly set the value as a float (otherwise initializing to 1 will cause it to be an int in the editor), but the enum is still not auto-translated to a string in the editor.

[Export]
public Godot.Collections.Dictionary<ClassName.EnumNames, float> Weights = new Godot.Collections.Dictionary<ClassName.EnumNames, float> () {
    { EnumName1, 1 } // 1 will be a float in the editor, but EnumName1 will still show up as 11 and not offer auto-complete
};

Using a C# dictionary causes the exported var to not show up at all in the editor.

raulsntos commented 2 years ago

I am trying to export a dictionary of enum: float.

The C# specification says the underlying type of an enum can be any integral type other than char (see https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/enums#182-enum-declarations). Even if you could, it's probably not a good idea because of precision loss.

In the editor my enum shows up as an int, instead of a string. [...] There is also no auto-complete available.

Without typing information the editor can't know that your values represent an enum. The editor receives a value of the underlying type, which means an integral type.

IntelliSense also can't know what is the type of the keys or the values of the Dictionary without the typing information, since you are using the non-generic Godot.Collections.Dictionary the typing information is not available.

If you use the generic Godot.Collections.Dictionary<TKey, TValue> the typing information is available and IntelliSense can help you when using the dictionary keys/values, this information needs to be passed to the editor but currently this is not supported.

For the typing information to be supported in the editor we'd need to implement type hints for dictionaries, there is already a proposal for this: godotengine/godot-proposals#56.