-
-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Comments
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 |
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. |
The casting to int remains an issue in 4.2.2
output:
Here is a link to the script running on |
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? |
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. |
But indeed this might be considered for cherry-picking for 4.2.x but it might not be possible due to the changes |
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:
where
So, there is that. Hope it helps. |
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
Error produced:
Invalid cast. Cannot convert from "tmpsqfisqxs.gd::__GeneratedGodotClass__.Foo" to "int".
Minimal reproduction project (MRP)
enum-cast-repro.zip
The text was updated successfully, but these errors were encountered: