Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | denxi |
I had a problem that I have figured out a workaround for, however I’m curious as to what the mechanics behind it actually were, so I can better understand how to avoid it in the future. The problem was get_tree().reload_current_scene() causing other errors in my scene change set-up.
I’ve pulled the code for scene changes right out of the manual here: Singletons (AutoLoad) — Godot Engine (3.0) documentation in English
var current_scene = null
func _ready():
var root = get_tree().get_root()
current_scene = root.get_child(root.get_child_count() -1)
func goto_scene(path):
call_deferred("_deferred_goto_scene", path)
func _deferred_goto_scene(path):
# Free Current Scene
current_scene.free()
# Load new scene.
var s = ResourceLoader.load(path)
# Instance the new scene.
current_scene = s.instance()
# Add it to the active scene, as child of root.
get_tree().get_root().add_child(current_scene)
# Optional, to make it compatible with the SceneTree.change_scene() API.
get_tree().set_current_scene(current_scene)
The problem I’m having is that when I use get_tree().reload_current_scene() is that it seems to wipe what my current_scene variable is. I was under the impression that reloading my current scene wouldn’t include any singletons, but I don’t have any other explanation for what’s happening. If someone could give me a heads up as to what I’m missing in the process, I’d appreciate it.
BTW the workaround I’m using is just
goto_scene(current_scene.filename)