How to load and change scenes

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Newby

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

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 ()
get_tree (). get_root (). add_child (game)
hide ()

Kao_Shinpa | 2021-04-05 18:44

:bust_in_silhouette: Reply From: Zylann

The simplest is this:


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")

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

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.

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.

someone | 2018-05-29 18:22

Thanks back to building

Merlin1846 | 2019-11-12 06:06

#You have little misspell in you code
var next_level_resource = load("res://path/to/scene.tscn)
var next_level = next_leve_resource.instance() <------

Dzejkob | 2019-11-27 14:54

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

lolinoh | 2020-05-27 13:22

I’ve been searching for this solution

Thank bro

schoolmill | 2020-09-05 15:26

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

Animaliss | 2023-04-29 13:20

:bust_in_silhouette: Reply From: spiritabsolute

I saw this way in the documentation:

:bust_in_silhouette: Reply From: boston2029

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.

:bust_in_silhouette: Reply From: theMX89

so, the easiest way is to do this:

                              (scene name)

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

boston2029 | 2021-07-12 14:54

:bust_in_silhouette: Reply From: Snail0259

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:


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!

:bust_in_silhouette: Reply From: kurtsev0103
  • The simplest method
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

# 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.

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

:bust_in_silhouette: Reply From: ccpixel

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:

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).

Mahadev Dp

Love Status

God Wallpaper

lakki365 | 2023-03-11 16:14