Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exported enum variable defaults to 0 even if first enum int value is set to non-0, when using c# #90987

Open
ana-rchy opened this issue Apr 21, 2024 · 6 comments

Comments

@ana-rchy
Copy link
Contributor

Tested versions

4.2.stable

System information

Godot v4.2.1.stable.mono - Arch Linux #1 SMP PREEMPT_DYNAMIC Thu, 11 Apr 2024 01:47:33 +0000 - X11 - Vulkan (Forward+) - dedicated NVIDIA GeForce RTX 3060 Ti (nvidia; 550.67) - AMD Ryzen 5 7600X 6-Core Processor (12 Threads)

Issue description

having an exported enum variable that uses an enum with a first value that isnt 0, e.g. enum SomeEnum { One = 1, Two, Three }, doesnt default to 1 when the export var is unchanged in the editor

if you click on another value and then back to the first one, it works as expected but shows an icon to revert back to the default

Steps to reproduce

refer to MRP

Minimal reproduction project (MRP)

enum-mrp.zip

@markdibarry
Copy link
Contributor

In C#, an enum's default will always be the type's default, so this is expected behavior.

//enum SomeEnum { One = 1, Two, Three }
SomeEnum someEnum = default;
GD.Print(someEnum); // 0

If you want it to display a different default in the editor you can do:

[Export] SomeEnum _testValue = SomeEnum.Two;

@ana-rchy
Copy link
Contributor Author

In C#, an enum's default will always be the type's default, so this is expected behavior.

//enum SomeEnum { One = 1, Two, Three }
SomeEnum someEnum = default;
GD.Print(someEnum); // 0

If you want it to display a different default in the editor you can do:

[Export] SomeEnum _testValue = SomeEnum.Two;

i still think itd be better for the editor to just do that automatically, makes it a bit confusing/annoying if someone isnt aware of it

@AThousandShips
Copy link
Member

AThousandShips commented Apr 21, 2024

i still think itd be better for the editor to just do that automatically

What and how should it pick though? The first value isn't the one picked by default otherwise, for example what should be done with:

enum SomeEnum { A = -1, B, C }

Should it be defaulted to A or B?

There's a warning in GDScript recently added, maybe something similar can be done here

@ana-rchy
Copy link
Contributor Author

i still think itd be better for the editor to just do that automatically

What and how should it pick though? The first value isn't the one picked by default otherwise, for example what should be done with:

enum SomeEnum { A = -1, B, C }

Should it be defaulted to A or B?

There's a warning in GDScript recently added, maybe something similar can be done here

* [GDScript: Warn when enum variable has no default #90756](https://github.com/godotengine/godot/pull/90756)

yeah warning seems better for this

@paulloz
Copy link
Member

paulloz commented Apr 22, 2024

As stated above, this is simply how enum types work in C#. It is already possible to enforce the existence of a zero value with CA10081.

Footnotes

  1. https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1008

@fire fire changed the title exported enum variable defaults to 0 even if first enum int value is set to non-0, when using c# Exported enum variable defaults to 0 even if first enum int value is set to non-0, when using c# Apr 22, 2024
@ana-rchy
Copy link
Contributor Author

i noticed just now that on animatedsprite2d's, the animation will sometimes be set to "default" instead of any of the animations, so perhaps if theres no 0-value in an enum it can be shown as "default" in the editor instead?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants