|
@@ -3,10 +3,25 @@ extends Node
|
|
|
|
|
|
var current_scene = null
|
|
var current_scene = null
|
|
|
|
|
|
|
|
+
|
|
|
|
+func goto_scene(path):
|
|
|
|
+
|
|
|
|
+ # This function will usually be called from a signal callback,
|
|
|
|
+ # or some other function from the running scene.
|
|
|
|
+ # Deleting the current scene at this point might be
|
|
|
|
+ # a bad idea, because it may be inside of a callback or function of it.
|
|
|
|
+ # The worst case will be a crash or unexpected behavior.
|
|
|
|
+
|
|
|
|
+ # The way around this is deferring the load to a later time, when
|
|
|
|
+ # it is ensured that no code from the current scene is running:
|
|
|
|
+
|
|
|
|
+ call_deferred("_deferred_goto_scene",path)
|
|
|
|
+
|
|
|
|
+
|
|
func _deferred_goto_scene(path):
|
|
func _deferred_goto_scene(path):
|
|
|
|
|
|
# Immediately free the current scene,
|
|
# Immediately free the current scene,
|
|
- # there is no risk here.
|
|
|
|
|
|
+ # there is no risk here.
|
|
current_scene.free()
|
|
current_scene.free()
|
|
|
|
|
|
# Load new scene
|
|
# Load new scene
|
|
@@ -18,19 +33,6 @@ func _deferred_goto_scene(path):
|
|
# Add it to the active scene, as child of root
|
|
# Add it to the active scene, as child of root
|
|
get_tree().get_root().add_child(current_scene)
|
|
get_tree().get_root().add_child(current_scene)
|
|
|
|
|
|
-func goto_scene(path):
|
|
|
|
-
|
|
|
|
- # This function will usually be called from a signal callback,
|
|
|
|
- # or some other function from the running scene.
|
|
|
|
- # Deleting the current scene at this point might be
|
|
|
|
- # a bad idea, because it may be inside of a callback or function of it.
|
|
|
|
- # The worst case will be a crash or unexpected behavior.
|
|
|
|
-
|
|
|
|
- # The way around this is deferring the load to a later time, when
|
|
|
|
- # it is ensured that no code from the current scene is running:
|
|
|
|
-
|
|
|
|
- call_deferred("_deferred_goto_scene",path)
|
|
|
|
-
|
|
|
|
|
|
|
|
func _ready():
|
|
func _ready():
|
|
# Get the current scene, the first time.
|
|
# Get the current scene, the first time.
|