2014-02-10 01:10:30 +00:00
|
|
|
extends Node
|
|
|
|
|
|
|
|
|
|
|
|
var current_scene = null
|
|
|
|
|
2015-04-18 17:00:15 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2015-04-12 23:57:26 +00:00
|
|
|
func _deferred_goto_scene(path):
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2015-04-12 23:57:26 +00:00
|
|
|
# Immediately free the current scene,
|
2015-04-18 17:00:15 +00:00
|
|
|
# there is no risk here.
|
2015-04-12 23:57:26 +00:00
|
|
|
current_scene.free()
|
|
|
|
|
|
|
|
# Load new scene
|
|
|
|
var s = ResourceLoader.load(path)
|
|
|
|
|
|
|
|
# Instance the new scene
|
2014-02-10 01:10:30 +00:00
|
|
|
current_scene = s.instance()
|
2015-04-12 23:57:26 +00:00
|
|
|
|
|
|
|
# Add it to the active scene, as child of root
|
2014-11-06 00:20:42 +00:00
|
|
|
get_tree().get_root().add_child(current_scene)
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
func _ready():
|
2015-04-12 23:57:26 +00:00
|
|
|
# Get the current scene, the first time.
|
2014-02-10 01:10:30 +00:00
|
|
|
# it is always the last child of root,
|
2015-04-12 23:57:26 +00:00
|
|
|
# after the autoloaded nodes.
|
|
|
|
|
2014-11-06 00:20:42 +00:00
|
|
|
var root = get_tree().get_root()
|
2014-02-10 01:10:30 +00:00
|
|
|
current_scene = root.get_child( root.get_child_count() -1 )
|