Topic was automatically imported from the old Question2Answer platform.
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
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.
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() <------
root.add_child(next_level)
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?)))
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.
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.
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.
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).