How do I make it so that if I click the button I jump to another scene?

godot-4

Question

Hi! I’m working on my own game in Godot but I have a problem with the script because I don’t know how to make it so that if I click a button I go to another scene. I’m asking here because even the tutorials and documentation don’t help.

Solution:
extends Node2D

func _on_StartButton_pressed():

get_tree().change_scene_to_file("res://path/to/your/scene.tscn")

func _ready():
var start_button = $StartButton
if start_button:
start_button.connect(“pressed”, Callable(self, “_on_StartButton_pressed”))
else:
print(“StartButton not found!”)

ps: this is for Node2D

@KubusHelper Use get tree and then either the change scene to file or change scene to packed functions. You need to have a reference to a packed scene or file path, so you could use an export var of type packed scene to supply it.

1 Like

BTW, the methods this other person was talking about are get_tree() to get the scene tree, and either change_scene_to_file or change_scene_to_packed to change the scene, depending on what’s convenient for you.

(Also, you can read this doc for an example: Using SceneTree — Godot Engine (stable) documentation in English)

Please do not comment on this topic, the problem has been solved

Hi there, if you found a solution, please post it! That way other people can benefit from the knowledge too. Be sure to mark your own answer as the solution once you posted it, that way other people will see that the question does not need answers anymore.
image