"if name ==" not working with labels?

Godot Version

v4.4.1

Question

I’m fairly new to properly coding and am currently attempting to create a game. I decided to start working on the title scene but have been stuck trying to figure out why this if statement isn’t working, as it looks almost identical to several other examples and the brief documentation I’ve read so far hasn’t shown me it shouldn’t.

func _gui_input(event: InputEvent) -> void:
if event.is_action_pressed("guiClick"):
print("Click!")
if name == "new game":
# animation for game intro or something along those lines
print("New Game")
var scene : PackedScene = load("res://scenes/game.tscn")
get_tree().change_scene_to_packed(scene)
if name == "continue":
#scene 2 temp for testing purposes
print("Continue")
var scene2 : PackedScene = load("res://scenes/game_2.tscn")
get_tree().change_scene_to_packed(scene2)

I’m using labels as the buttons- they register as being pressed in debug and the intial “Click!” print does work, but I have no idea why the other statements aren’t working.

What does your scene tree look like, and what node did you add the script to?

if name == "new game":
    ...

if name == "continue":
    ...

These two if statements are found in the same function and they both test what the value of name is. name is a property of a node, in this case of the node the script is attached to. Is this script attached to one label? A node can’t have more than one name, so I wonder why _gui_input has those two if statements.

There are 3 different label nodes utilising this script, since they all have the same basic interactivity and visual flavour but will go to different scenes:

I do think case matters here. What happens if you change if name == "continue" to if name == "Continue"?

…I totally thought I had done that already. ;-; Thank you so much

1 Like