+51 votes

like the title says how do i do it i have seen other code for changing scene and it is a bit different from the others
i want to know what is the base code of scene changing

in Engine by (400 points)

normally I use the following code in the scene shown first (the "welcome") to load to the "main" scene

var game = preload ("the path to your scene"). instance ()
gettree (). getroot (). add_child (game)
hide ()

7 Answers

+63 votes

The simplest is this:

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

Under the hood, changing scene is as simple as replacing a node in the tree by another:

# Remove the current level
var level = root.get_node("Level")
root.remove_child(level)
level.call_deferred("free")

# Add the next level
var next_level_resource = load("res://path/to/scene.tscn)
var next_level = next_level_resource.instance()
root.add_child(next_level)

root can be any node you want. In the first simplest example, root is the root of the whole scene tree. Once you understand how this works, you can apply this anywhere, and you'll realize "changing scene" is a relative concept. You could change everything if you do it on the root, but you can do that to a part of the tree, so that not everything has to change.

pgregory suggested background loading but it's not required to change scene. You might need it only if your scene is too big to the point of needing a loading screen.

by (29,118 points)
edited by

If you want it to be compatible with SceneTree.change_scene you may need to set SceneTree.set_current_scene as well. See also Custom scene switcher.

Thanks back to building

You have little misspell in you code

var next_level_resource = load("res://path/to/scene.tscn)
var next_level = next_leve_resource.instance() <------
root.add_child(next_level)

When you change scene, the scene is reloaded. But What can I do if a need somethings in the scene that is preserved. For example, you complete a level catching 3 coins like Mario Bros, and if you play the level again in the future then you can't catch the coins

I've been searching for this solution

Thank bro

After all, there is save to file, why not use it?
(Or was it not yet available in 2020?)))

+6 votes
by (26 points)
+2 votes

Here's how I do it:
Well I just type get_tree().change_scene_to(load('res://folder/subfolder/scene.tscn'))
and obviously replace the "res://folder/subfolder/scene.tscn" with the path to your scene.

by (92 points)
+1 vote

so, the easiest way is to do this:

                              (scene name)
                                      I
                                      I
                                      v
get_tree().change_scene("res://yourAmazingScene!")
by (223 points)

Yeah I figured that one out a couple days ago on a YouTube tutorial. Seems easier.

+2 votes

All of the above answers work fine, however, if you then scale your game to more levels, it will soon get out of control. Take this snippet from a game I was working on:

get_tree().change_scene("res://levels/level"+str(int(get_tree().current_scene.filename[-6])+1)+".tscn")

it's a bit of a mouthful, but what it's doing is changing the scene to "res://levels/level(1/2/3...).tscn".

what it does is it starts by appending the location of all the levels. Then it gets the name of the current scene and then gets the character at [-6] which is a number (because all levels end in level(int).tscn, int being the level number. it ten converts that from a string to an int, and adds 1, getting the next level.

Hope this is useful for someone!

by (248 points)
+1 vote
  • The simplest method
get_tree().change_scene("res://some_folder/some_scene.tscn")
var current_scene = null

func _ready():
    var root = get_tree().get_root()
    current_scene = root.get_child(root.get_child_count() - 1)

func change_scene(path: String):
    # 1. It is now safe to remove the current scene
    current_scene.queue_free()

    # 2. Load the new scene.
    var new_scene = ResourceLoader.load(path)

    # 3. Instance the new scene.
    current_scene = new_scene.instance()

    # 4. Add it to the active scene, as child of root.
    get_tree().get_root().add_child(current_scene)

    # Optionally, to make it compatible 
    # with the SceneTree.change_scene() API.
    get_tree().set_current_scene(current_scene)
  • Or you can look at the AppDelegate module, it loads/unloads modules and their resources. It can be used on any version of Godot.
by (162 points)
edited by
0 votes

I'm not sure if its what you're looking for, but I recently wrote about my solution for this in a topdown 2d rpg here:
https://4days.dev/devlog/2021/12/26/breaking-and-entering.html

With help from @zylan's answer, I recently made a teleport system that allows the player to press a button when they are in an Area2D node and get "teleported" to another Area2D node (in any scene).

by (40 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.