Skip to content

How do I change scenes?

Godot 4.x

One line does it. Put it wherever the switch should happen, such as when the player touches the exit or presses a button:

level_1.gd
extends Node2D
func go_to_next_level() -> void:
get_tree().change_scene_to_file("res://level_2.tscn")

The path must be the full res:// path to the .tscn file, and it is case-sensitive: res://Level_2.tscn won’t find level_2.tscn. Don’t type it by hand. Right-click the file in the FileSystem dock and pick Copy Path, then paste it between the quotes.

If you’d rather not deal with paths as strings, load the scene into a constant at the top of the script and pass that instead:

level_1.gd
extends Node2D
const LEVEL_2 := preload("res://level_2.tscn")
func go_to_next_level() -> void:
get_tree().change_scene_to_packed(LEVEL_2)

The editor checks the preload() path as soon as you save the script, and renaming or moving the file in the FileSystem dock updates it for you.

The scene you were in is freed. Every node on it is gone, along with its scripts and their variables, so a score stored on level_1.gd doesn’t make it into level_2.tscn. To keep a value across scenes, put it in an autoload.