If you want to know how to change scenes in Godot Engine, this is how to change scenes in Godot!
The code itself is pretty easy, it’s just a line of code.
extends Node2D #Godot v3.5.1 func _physics_process(delta): if SomeInput: get_tree().change_scene("scenePath")
Godot is based on Nodes, every scene is basically a collection of Nodes saved with the “.tscn” extension. You can open nodes with whatever text editor you want and read them like text.
SceneTree manages all the Nodes hierarchy in a scene. If you want more info visit the official documentation here.
get_tree() | This calls the sceneTree |
change_scene(“scenePath”) | This makes the real switch between two scenes, it needs a path of the scene you want to load to work. The path is just a string. |
Packed Scenes
To make everything easier and manageable you may decide to export variables and use packed scenes. This is very useful if you want more flexibility with your levels.
Exporting variables allows you to change their value directly in the editor. This means that you don’t need to change the variable inside the .gd file each time you want to make changes.
extends Node2D #Godot v3.5.1 export (PackedScene) var myNextLevel
If you want to change scene using a packedScene you need to use change_scene_to(packedSceneVariable).
extends Node2D #Godot v3.5.1 export (PackedScene) var myNextLevel func _physics_process(delta): if SomeInput: get_tree().change_scene_to(myNextLevel)
And this is how to change scene in Godot engine.