How do I change scenes?
Change to a scene by path
Section titled “Change to a scene by path”One line does it. Put it wherever the switch should happen, such as when the player touches the exit or presses a button:
extends Node2D
func go_to_next_level() -> void: get_tree().change_scene_to_file("res://level_2.tscn")using Godot;
public partial class Level1 : Node2D{ public void GoToNextLevel() { GetTree().ChangeSceneToFile("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.
Change to a preloaded scene
Section titled “Change to a preloaded scene”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:
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.
What happens to the old scene
Section titled “What happens to the old scene”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.