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

GDScript: Casting an enum type to an int generates an error. #85882

Closed
jadoc opened this issue Dec 7, 2023 · 8 comments · Fixed by #90580
Closed

GDScript: Casting an enum type to an int generates an error. #85882

jadoc opened this issue Dec 7, 2023 · 8 comments · Fixed by #90580

Comments

@jadoc
Copy link
Contributor

jadoc commented Dec 7, 2023

Tested versions

v4.2.stable.official [46dc277]
v4.1.1.stable [bd6af8e]

System information

Godot v4.2.stable - Windows 10.0.22621 - Vulkan (Forward+) - dedicated AMD Radeon RX 6900 XT (Advanced Micro Devices, Inc.; 31.0.22023.1014) - 12th Gen Intel(R) Core(TM) i7-12700K (20 Threads)

Issue description

GDScript enum types can be directly used wherever and int can be used, but if an attempt is made to cast to an int, an error is thrown. This is astonishing and confusing, especially to those learning GDScript. The error message is stating that the types are incompatible when they are very much compatible.

While casting enums to ints isn't strictly necessary, it should be allowed for consistency. GDScript shouldn't generate misleading error messages.

Steps to reproduce

enum Foo { A = 2, B = 5, C  = 7 }

func bar(p: Foo) -> int:
	return p as int * 10

func _ready():
	bar(3)

Error produced:
Invalid cast. Cannot convert from "tmpsqfisqxs.gd::__GeneratedGodotClass__.Foo" to "int".

Minimal reproduction project (MRP)

enum-cast-repro.zip

@dalexeev
Copy link
Member

dalexeev commented Dec 7, 2023

As a workaround you can use:

func bar(p: Foo) -> int:
    return int(p) * 10

or

func bar(p: Foo) -> int:
    return p * 10

Wherever int is expected, you can use any enum value without an explicit cast to int, including math and comparison operations between an enum value and int.

@andrew-wilkes
Copy link

In Godot 4.2.1.stable we cannot cast an emum to an int as suggested in the error message when running the above code.

Error message: Integer used when an enum value is expected. If this is intended cast the integer to the enum type.

I had a use case where this would be nice to use and I would not regard it as bad practice. In a state machine, a state is entered with 3 possible enums and we advance to the next state related to the entry point enum by adding 1 to it. For example, enter with RST0 and exit with RST1 where both of these are enums. It saves having a match statement for something that is obvious.

@vnen
Copy link
Member

vnen commented Apr 12, 2024

In Godot 4.2.1.stable we cannot cast an emum to an int as suggested in the error message when running the above code.

Error message: Integer used when an enum value is expected. If this is intended cast the integer to the enum type.

I had a use case where this would be nice to use and I would not regard it as bad practice. In a state machine, a state is entered with 3 possible enums and we advance to the next state related to the entry point enum by adding 1 to it. For example, enter with RST0 and exit with RST1 where both of these are enums. It saves having a match statement for something that is obvious.

@andrew-wilkes This is a different case, the error you're getting (which is actually a warning) is about using an int as enum, so you need to cast the int to the enum, this is expected. The issue in the OP is the opposite, it's trying to cast the enum as an int, which gives an actual error message, when it shouldn't.

@LaraSQP
Copy link

LaraSQP commented Jun 27, 2024

The casting to int remains an issue in 4.2.2

enum Characters {FIGHTER, ROGUE, MAGE}

for i in Characters:
    print(i)
    print(int(i))

output:

FIGHTER
0
ROGUE
0
MAGE
0

Here is a link to the script running on Godot Engine 4.1.1.stable.bd6af8e0e

https://gd.tumeo.space/?KYDwLsB2AmDOAEA5A9tYAodUCuBbeAwgBYCGATiQMYRkIDeAYgJIDiAEgCoCiASgDTweAeRYBVLgICyAQRZcAvpnQAzbJErwA+mWAloATwAUASgBc6eJfgWry5GXgBLJ5EKkK1YLXNXf8AA5kjpBgho7GNn4BQSFhseHGQA

@AThousandShips
Copy link
Member

AThousandShips commented Jun 27, 2024

Yes this was fixed for 4.3 only currently, it might be possible to cherry pick it, also you mention 4.2.2 but then say 4.1.1, which was it?

@LaraSQP
Copy link

LaraSQP commented Jun 27, 2024

In both.

Like I said, the casting to int remains an issue in 4.2.2 but the script I linked to runs 4.1.1

Thanks for looking into this.

@AThousandShips
Copy link
Member

But indeed this might be considered for cherry-picking for 4.2.x but it might not be possible due to the changes

@LaraSQP
Copy link

LaraSQP commented Jun 27, 2024

I'm running 4.2.2 and the issue was looping through the enum.

However, the conversion works fine in some cases.

For example, the following function call works as expected:

enum Layers { Ground, Environment }

tillemap.set_layer_enabled( Layers.Ground, true )

where

void set_layer_enabled( layer: int, enabled: bool )

So, there is that. Hope it helps.

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

Successfully merging a pull request may close this issue.

6 participants