+31 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 (392 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 ()

9 Answers

+42 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 (27,825 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

+5 votes
by (24 points)
–5 votes

gettree().changescene("res://name.tscn")

by (254 points)

The most-upvoted answer (as of writing) already has this line of code at the top. Please don't write duplicate answers :)

Oh sorry. I did as well.

also, I think it should be 'get_tree().change_scene()'

+1 vote

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 (77 points)
+1 vote

Most easy way to change scene is

get_tree().change_scene(res://#####)

And load scene via

var s = preload(res://#####).instance()
add_child(s)
by (881 points)

also, if you don't want the child to move with the parent node, you can go:

var childNode = preload("res://#####").instance()
get_tree().get_root().add_child(childNode)
0 votes

gettree().changescene("res://path/to/scene.tscn")

by (179 points)
0 votes

so, the easiest way is to do this:

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

This answer has been stated before, no need to say it again

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

+1 vote

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 (272 points)
0 votes
  • 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 (108 points)
edited by
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 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 webmaster@godotengine.org with your username.