-
-
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
Match don't match #42224
Comments
I can reproduce this on Godot 3.2.3. That said, the first time I launched the project, I had a different output when clicking the Artistes/Albums button (it toggled between both states). It works if you add an var searching_mode: int = searching_by.ARTIST The variable's type can't be inferred for some reason… |
I have the same bug at different location on my script, and it's the same. Thanks for your tips, it works ! What's crazy is that there is a part of my code that worked perfectly fine... Then just broke... |
@karroffel implemented this, so he might be able to help. 🙂 |
@ShalokShalom karroffel no longer contributes to Godot core, so I'm afraid he can't help much here. |
I just tested this on the current master and could not reproduce it. So at least it seems to have been fixed during the GDScript rewrite for 4.0. |
That's good to hear ! Just have to wait for 4.0..... JUST ! ^^ |
JSON parse 0 as 0.0 float and you can't match float 0.0 with int 0 (not sure if it's a bug CC @vnen ) but the bug here is if a float value is a whole number it just prints the integer and ignore the decimal point which is harder for someone to debug. var value = JSON.parse('0').result ## <-- a float value not integer
func _ready():
print(value) ## <-- prints 0 not 0.0
match value:
0:
print('0')
1:
print('1')
0.0:
print('0.0') ## <-- matches here
_:
print('default') |
@ThakeeNathees GDScript is a strongly-typed language, so |
Then, why do the if condition work, but match doesn't ? |
So this isn't really a bug, you should cast for the type you expect it to be. Now I wonder if I broke something on 4.0 or if the test was made with something different, because it should still only match if the type is the same. |
@vnen my testing mentioned above didn't go through the json parser, my bad. it seems that it's still type-strict in 4.0 |
BTW, I think that matching a pattern with a literal, variable or constant should use the same mechanism and provide exactly the same convenience that the equality test But I guess, this change would have to go into a new proposal. |
I've encountered this same problem. Interestingly, the match statement works OK when launched from the editor, and works OK when exported to Windows with debug mode enabled. The match statement only fails when exported to Windows with debug mode disabled. Adding an explicit type of 'int' fixes the match statement. |
I have been working over 9 hours straight due to this issue thinking the problem was elsewhere. The following codes is used in my project to controls the value (and if active) of a TextureRect node. Here's the code using a match statement:
Even if I verify (print) that the val integer has the proper value, the match always applies the _ (default) value. If, instead, I use this:
It works 100%. A side note to the issue: |
@dongohu Considering the comment from Poobslag, do you also experience this bug only, when you export to Windows with no debug mode enabled? |
No, I experience this via: There's no error or issues being pointed out, but every instance of match I uses in GDscript seems to return the default value. = EDIT = Ha! I just found one thing that works with match, but this reflects that the problem is the engine's interpretation of the match cases. If you place the default _: case as the last case, it works!
This is the revision of my previous code and this works. I'm kinda happy that I found this work-around now while I'm still working on the small stuff that is relatively easy to review because I'm on the verge of working on a dynamic world generation system that I build in another engine that is using match statement as its basics for determining the biomes and its content from simple values. |
There's no reordering done and it is not expected to be. If the wildcard is the first pattern it will always match anything. You do get a warning if you add patterns after a wildcard or unconditional variable bind. |
That's actually something relatively unique to Godot then and there is no warning currently for having anything after the wild card in Godot 4 neither during the coding nor actual test/play phases. (Otherwise, I would have noticed it and would have corrected the code right away and wouldn't have taken as much time finding the issue.) To give examples, Unity, Unreal, CryEngine and even RPG Maker apply a reordering to the Switch function, during compilation for Android and iOS build, which replace switch statements by if/ifelse/else functions which is why I though it could have something to do with it. On other builds, they are managing the switch function by only returning the wild card if none of the other expressions are true. To quote Microsoft's own documentation on switch function: The Switch function argument list consists of pairs of expressions and values. The expressions are evaluated from left to right, and the value associated with the first expression to evaluate to True is returned. (...) Switch evaluates all of the expressions, even though it returns only one of them. Godot is the only case I have found that uses a true value for its wild card for its switch function instead of using a null value as, otherwise, it would detect that a later expression is true and would only return the wild card if none are true. |
@dongohu And there is a warning for that. I do think warnings are a bit hidden in the editor, but the warning exists: |
@dongohu |
As stated above and described in the GDScript documentation: 1. Note We could add a warning when the types of matching and pattern values are known, unequal, and implicitly convertible to each other. However, this may have false positives. 2. Wildcard ( So the only actionable suggestion I see here is to add a warning when trying to compare |
Found nothing like my issue with : is:issue "match" in:title label:bug state:open searching's filter.
Godot version:
Godot Engine v3.2.3.stable.official
OS/device including version:
Ubuntu 20.04
GPU : Nvidia GeForce GTX 970
GPU driver : nvidia driver 450 (libre)
Proc : AMD® Ryzen 9 3900x
Issue description:
Match is supposed to match with the first option... but don't.
Steps to reproduce:
Don't know too much.
Made a variable match with a enum's const.
Minimal reproduction project:
issue_minimal_project.zip
The text was updated successfully, but these errors were encountered: