-
-
Notifications
You must be signed in to change notification settings - Fork 21.1k
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
Can't access parent's named enum from match statement #37631
Comments
Just wondering, does it work if you move |
It does. This does too: var left = $"..".Sides.LEFT
match side:
left:
print("Left side") |
I don't think this is a bug, the match pattern can't be a function call match side:
f(): ## <-- invalid
print("f() matched") and match side:
get_node("..").Side.LEFT: ## <-- also invalid
print("Left side") |
@ThakeeNathees I understand what you mean. If I move only the function call to a separate var (what @Zireael07 suggested), it works: func foo(side):
var node = $".."
match side:
node.Sides.LEFT:
print("Left side")
node.Sides.RIGHT:
print("Right side") But in that case, couldn't there be a way to evaluate a function at compile-time, to allow cases such as these to work? |
the match pattern can't be an expression, because they're only labels for compiler to jump to the next address unlike switch case (compile time evaluable pattern) the gdscript match statement evaluates pattern at at runtime ( like runtime constants) and all it does is check if it's equal and jump to the next address. switch (val_1) {
case 1: do_something(); break;
case 2: do_something_else();
}
// is equal to
goto case_1; // runtime
label case_1:
do_something();
goto case_break;
label case_2: // <--- just a label, can't use expressions here
do_something_else();
label case_break; but some match pattern (binding pattern , array pattern) allow to declare a var and assign the match value inside the match statement scope, which is a syntax sugar for a default case declare and assign the match value immediately inside. |
In that case, wouldn't it be possible to do the same "tricks" for this use-case? If the expression is evaluated at run-time, could a NodePath also be evaluated before-hand? I think this would be useful, since the But if what I'm asking is, in practice, very hard to implement for the current code structure, I can just use the working sample (it's not pretty, but it isn't that ugly either) and close this. |
In Godot 4, this is errors out by design with the following error:
The problem here is the fact that extends Node
const Parent = preload("./parent.gd")
func foo(side):
match side:
($".." as Parent).Sides.LEFT:
print("Left side")
($".." as Parent).Sides.RIGHT:
print("Right side") So, I suggest that for 3.x, it would be a "won't fix" and we could close this issue for 4.x as "as designed". |
|
I think that even if the example in this issue is unsuccessful, in general this is a valid observation. I see no reason for the restrictions, in my opinion a pattern should be allowed to be any dynamic expression. |
This issue is then linked to godotengine/godot-proposals#2337 |
My reasoning at the time was because this works properly with Regardless, the suggested workaround works, so I don't have a particularly strong opinion whether this should be supported or not. Whether to support this or not, I'm fine with the decision you'll make 🙂 |
No, currently |
Godot version: 3.2.1
OS/device including version: Arch Linux (kernel 5.5.13)
Issue description: Godot throws an error when I try to access a named enum from a parent inside a
match
statement. If I change to be a classicif/elif/else
block, it works fine.The error is
built-in:8 - Parse Error: Invalid operator in pattern. Only index (A.B) is allowed
, which suggests that Godot won't allow to access a field more than once.Looks related to #10571
Steps to reproduce: Have a scene tree like:
Parent.gd:
Child.gd
Minimal reproduction project: project.zip
The text was updated successfully, but these errors were encountered: