godot icon indicating copy to clipboard operation
godot copied to clipboard

GDScript 2.0: @export_enum is missing support for arrays (`@export_enum("A", "B", "C")`)

Open h0lley opened this issue 2 years ago • 2 comments

Godot version

v4.0.alpha6.official [e4f0fc50f]

System information

Ubuntu

Issue description

In Godot 3, it is possible to export an array of predefined options where the choice is serialized as string:

export(Array, String, "Option A", "Option B", "Option C") var options := ["Option B"]

In Godot 4, it appears what is meant to replace this is the @export_enum annotation. Although it is currently broken, according to the docs it should be possible to use a string type hint to indicate that the option should be stored as string:

@export_enum("A", "B", "C") var option: String
@export_enum("A", "B", "C") var option := "B"

However, even then support for arrays like we have in Godot 3 is still missing.

image

@export var options: Array[MyEnum] is not a proper solution for this as it serializes the choices as integers. There's cases where that is problematic such as when the enum is being edited and the stored integers do no longer align or when the stored values need to remain readable.

An argument can be made that integer based enums should always be used, but then I don't really see any use-case remaining for @export_enum and would suggest removing it.

Steps to reproduce

Attempt to recreate this functionality of Godot 3: export(Array, String, "A", "B", "C") var options Note that it doesn't seem to be possible

Minimal reproduction project

No response

h0lley avatar Apr 25 '22 17:04 h0lley

today someone asked how to export an array of @export_global_file("*.png") var tool_image. made me realize that this issue is not just about @export_enum but any specific export (@export_*).

h0lley avatar Nov 30 '22 02:11 h0lley

@h0lley So there are at least scenarios that need fixing:

@export_enum("Rebecca", "Mary", "Leah") var character_name: String
@export_enum("Rebecca", "Mary", "Leah") var character_name: Array[String]
@export_enum("Warrior", "Magician", "Thief") var character_class : Array[int]
  • default initializers for both the int and String cases

Error I am getting, which was not listed above, is: "@export_enum annotation requires a variable of type "int" but type "String" was given instead.",

red1939 avatar Dec 06 '22 08:12 red1939

@red1939 yes pretty much, the issue where @export_enum can't be typed as String despite the docs saying it should be supported is reported separately: #54828

I like the idea to be able to add the array hint through the typing Array[Type] for any of the @export_* annotations. perhaps that's even how it's supposed to work and it's currently bugged.

h0lley avatar Dec 06 '22 19:12 h0lley

I don't know if I am not opening a pandoras box here, but I will try to create a draft merge request with the support added for 2 types of enums: string and int. Arrays should be relatively easy to handle, but I will need to see how well do we deal with them already.

On Tue, 6 Dec 2022 at 20:27, Holly @.***> wrote:

@red1939 https://github.com/red1939 yes pretty much, the issue where @export_enum can't be typed as int despite the docs saying it should be supported is reported separately: #54828 https://github.com/godotengine/godot/issues/54828

I like the idea to be able to add the array hint through the typing Array[Type] for any of the @export_* annotations.

— Reply to this email directly, view it on GitHub https://github.com/godotengine/godot/issues/60516#issuecomment-1339883929, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADNME2DLK4E2ZIAS2SZAEFLWL6HRLANCNFSM5UJGH2RQ . You are receiving this because you were mentioned.Message ID: @.***>

red1939 avatar Dec 06 '22 22:12 red1939

I have good news and bad ones. Good one is that everything bar Array works. So you can have String or int or nothing, and it works correctly. No errors, warnings, and so on. Bad one is that Array needs a bit more love. During runtime there is no support/implicit conversion from String to int. This means that during serialization we would need to save ints and hopefully that would be all that's needed; property should (?) decode the value into a string.

red1939 avatar Dec 07 '22 00:12 red1939

Just ran into this error, very frustrating. Maybe the docs should be amended to make it more obvious this feature is missing? It seems unlikely it's going to make it into 4.0 at this rate.

ztc0611 avatar Dec 21 '22 00:12 ztc0611

I wonder if this issue should be renamed to GDScript 2.0: @export_* annotations are missing support for arrays (e.g. @export_enum)

h0lley avatar Dec 21 '22 01:12 h0lley