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 2.0: Cannot parse match with typeof() and enum constants from other script #63535

Closed
cassandrus opened this issue Jul 27, 2022 · 5 comments

Comments

@cassandrus
Copy link

cassandrus commented Jul 27, 2022

Godot version

Godot Engine v4.0.alpha12.official

System information

Windows 10

Issue description

When enum from another script (auto-load singleton) is used as case for a match expression, it fails with "Expression in match pattern must be a constant".
Built-in enums like TYPE_STRING work as expected. Enums declared in the same script work too.

I've decided not to add this report as part of #52033 as error texts are different. Also, I've recently filed report to #44375 where I also had problems with enums from singleton but on static typing (perhaps problems are related).

Followup:
I tried to use script that is declared through class_name insted of singleton auto-load script. It works this way. So the problem is only with singleton declarations (I;m starting to doubt if this is intended?).

Error:
image

Steps to reproduce

Just use enum from another script. I've also added mini-project to save your time.

Minimal reproduction project

game-client_test2.zip

@Calinou Calinou changed the title Cannot parse match with typeof() and enum constants from other script GDScript 2.0: Cannot parse match with typeof() and enum constants from other script Jul 27, 2022
@Calinou Calinou added this to the 4.0 milestone Jul 27, 2022
@AntonioDell
Copy link
Contributor

I'm looking into it :)

@AntonioDell
Copy link
Contributor

AntonioDell commented Aug 5, 2022

@cassandrus @Calinou Actually after much debugging I think this may not be an error, it could be intended behavior.
In the example project, the autoload in Project settings is created from the GameSettings.tscn, not the GameSettings.gd.
When changing this to GameSettings.gd, the error dissappears.
Now I am unsure whether or not an enum declared in an autoloaded node should be global, the example in the documentation shows an autoloaded script, so if it is a feature, it is not explicitly stated in the documentation (see here).

Original Project settings (.tscn is autoload) resulting in error:
Screenshot from 2022-08-05 09-07-49

Modified Project settings (.gd is autoload):
Screenshot from 2022-08-05 09-08-07
Screenshot from 2022-08-05 09-08-56

@cassandrus
Copy link
Author

cassandrus commented Aug 9, 2022

Firstly, if it has common reason with #44375 (and it's very much possible as it's the same project and "incorrectly enabled" auto-load singleton) and it is intended, it's still seems like a bug as it shows "parser error" there (instead of sane explanation of why I can't do this).

Secondly, I don't think it's intended behavior or incorrect use of singleton. Auto-load also says "Node name" -> "Add". I'm sure more experienced devs can say more, but when you use these global enums, they actually work in runtime without issues, the only problem is declaring them as constant types in switch or in type hints..

Thirdly, if it's unsupported feature, I think it should be supported. What I'm trying here is to make auto-load Node with a bunch of @export vars in script that are used for some global game settings. Thus, I use them both in scripts and edit them in Inspector - very handy for fast editing of configs.

@MarianoGnu
Copy link
Contributor

@cassandrus i don´t think your code example makes sense, when you call typeof(x) it will always return a value from Variant::Type, in this case TYPE_INT and if it ever enter a match condition it will be the one whose index matches with TYPE_INT

i think the right example would be

var x = GameSettings.ActorPositionType.FLOOR
match x:
    GameSettings.ActorPositionType.FLOOR:
        print("floor")
    [...]

that being said, the error still occurs, i am wondering if it could have to do with the order in which the GdScript parser analizes the sources

@adamscott
Copy link
Member

adamscott commented Jan 8, 2023

Could not reproduce on master (fcba87e) as I ran the MRP sucessfully, without any errors (both the editor and the runtime)! I consider this issue fixed. @akien-mga

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Status: Done
Development

No branches or pull requests

6 participants