Match statement odd behavior

Godot Version

godot 4.3

Question

i am using a switch statement to change the visibility of cursors on set places, this function is called on the _process() so it checks every frame, but no matter what the vlaue is, the game behaves as if the first condition is always true (only showing the cursor on the same position depute the variable selectedOption updating properly)

func DrawCursor() -> void:
	match selectedOption:
		0:
			$CursorContainer/Option0.visible = 1
			$CursorContainer/Option1.visible = 0
			$CursorContainer/Option2.visible = 0
		1:
			$CursorContainer/Option0.visible = 0
			$CursorContainer/Option1.visible = 1
			$CursorContainer/Option2.visible = 0
		2:
			$CursorContainer/Option0.visible = 0
			$CursorContainer/Option1.visible = 0
			$CursorContainer/Option2.visible = 1

That is strange as your code looks fine.

At first I thought it was because 1 and 0 are integers and visible expects a boolean. However a quick experiment confirms 0 is interpreted as false when converted to a boolean and to my suprise any other integer is interpreted as true. (Even -1)

So it can only be that selectedOption is somehow always being interpreted as 0, the visibilities are being overwritten somewhere else or your references are not pointing where you think they are.

What happens when you add a print statement before the match?

print("selectedOption: ", selectedOption, " Type: ", typeof(selectedOption))

Also, if it is failing, perhaps you could also add into the options:

print("Option 0 selected")
print("Option 1 selected")
etc

Finally perhaps a catch all at the end in case no match is being found:

        _:
            push_error("Invalid selected option: " + str(selectedOption))

But the code you have posted here is fine and does not have any issues. My best guess is selectedOption is not what you think it is.

1 Like

I really have no clue what is going on at least i know i am not completely incompetent at coding, i have already tried what you said and it only confirms how the values ARE in fact updating both in and out of the function with the match statement what i could say is maybe how i declared selectedOption globally so it could be used by all of the functions within the main menu script and idk if the fact that i initialize as zero on the _ready function like so:

extends Node2D
var selectedOption

func _ready() -> void:
	selectedOption = 0

here are the results of the print statements:
selectedOption: 2 Type: 2
selectedOption: 0 Type: 2

and adding the exception catcher does not do much because it fully accepts the 0 as it’s value
Edit:
while typing my response i thought maybe changing the value to something else when setting it on the _ready function would shed some light on it but nope, no changes

It seems your selectedOption is indeed an int and your match statement is working. Perhaps in the rest of your code somewhere you are calling this twice so it sets the option 2 as you wanted but then next frame (or same frame) another piece of code is resetting it back to 0.

Have you tried putting a debug break point on the match statement and seeing what the code is doing by stepping through it? Other than that I have no idea, but the code itself you have posted is fine and should work.

Just tried using a print statement for every option and well, i know for sure it does not set back to zero, my logic is maybe not the Issue maybe the way i change visibility is the problem? . Because the variable and the match statement for that matter are behaving as expected.

Try replacing .visible to true/false instead of 0/1

Was actually the first thing I tried, same behavior, pretty universally 0 is false and 1 is true, this in fact applies to gdscript