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

When exporting a enum with no zero value defined, the editor takes the wrong default value #76933

Closed
AndreSacilotto opened this issue May 11, 2023 · 4 comments

Comments

@AndreSacilotto
Copy link

Godot version

4.1.0-dev.1

System information

Win10

Issue description

Export a enum without a value defined for 0 can create a visual bug in the editor that makes the default value wrong.

Godot consider the default value of all exported enums to be always 0, but a enum dont necessary will have a 0 defined.

So by default it assumes all enum always start with 0, but since the enum dont have a 0 it shows the first element instead, but it dont actually use its value.

Steps to reproduce

Create a c# enum with no zero value and other class that export that enum.

Example:

public enum TDirection : int
{
     RightToLeft = -1,
     LeftToRight = 1,
}
public partial class Test : Node
{
     [Export] public TDirection tDir;
}

Minimal reproduction project

EnumBugDefault.zip

@AndreSacilotto AndreSacilotto changed the title C# when exporting a enum with no zero value defined the editor takes the wrong string C# when exporting a enum with no zero value defined the editor takes the wrong default value May 11, 2023
@AndreSacilotto
Copy link
Author

AndreSacilotto commented May 11, 2023

Yes there is a workaround that is simply explicitly defining the default value:

public partial class Test : Node
{
     [Export] public TDirection tDir = TDirection.RightToLeft;
}

What I'm trying to point out in this issue is that the editor shows the wrong enum key in the editor when you dont define a 0 value in your enum.
image

N1 is A, but as a value of 0
N2 is also A, but as a value of -1 (that is because I reselected A)

@Calinou
Copy link
Member

Calinou commented May 11, 2023

Can you reproduce this in GDScript?

@AndreSacilotto
Copy link
Author

Well I thought it was a c# bug, but seens like it's not - @Calinou yes it also happens on GDScript.

enum State {A = -1, B = 1}

@export var n1 :State
@export var n2 :State

func _ready():
	print(n1)
	print(n2)

@Calinou Calinou changed the title C# when exporting a enum with no zero value defined the editor takes the wrong default value When exporting a enum with no zero value defined, the editor takes the wrong default value May 11, 2023
@akien-mga
Copy link
Member

Duplicate of #74123 (and I'm pretty sure this is discussed in other issues, but I can't find them easily, GitHub search is failing me).

It's indeed a known design issue with the way enums work and the fact that exported variables get zero'ed by default, even if their export hint limits the valid range to something that doesn't contain zero.

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

3 participants