2014-02-10 02:10:30 +01:00
|
|
|
extends Node
|
|
|
|
|
2015-11-21 13:31:25 +01:00
|
|
|
# Member variables
|
2014-02-10 02:10:30 +01:00
|
|
|
var current_scene = null
|
|
|
|
|
2015-04-18 19:00:15 +02: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.
|
2015-12-09 08:38:23 +01:00
|
|
|
|
2015-04-18 19:00:15 +02:00
|
|
|
# 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:
|
2015-12-09 08:38:23 +01:00
|
|
|
|
2015-04-18 19:00:15 +02:00
|
|
|
call_deferred("_deferred_goto_scene",path)
|
|
|
|
|
|
|
|
|
2015-04-13 01:57:26 +02:00
|
|
|
func _deferred_goto_scene(path):
|
|
|
|
# Immediately free the current scene,
|
2015-12-09 08:38:23 +01:00
|
|
|
# there is no risk here.
|
2015-04-13 01:57:26 +02:00
|
|
|
current_scene.free()
|
2015-12-09 08:38:23 +01:00
|
|
|
|
2015-04-13 01:57:26 +02:00
|
|
|
# Load new scene
|
|
|
|
var s = ResourceLoader.load(path)
|
2015-12-09 08:38:23 +01:00
|
|
|
|
2015-04-13 01:57:26 +02:00
|
|
|
# Instance the new scene
|
2014-02-10 02:10:30 +01:00
|
|
|
current_scene = s.instance()
|
2015-12-09 08:38:23 +01:00
|
|
|
|
2015-04-13 01:57:26 +02:00
|
|
|
# Add it to the active scene, as child of root
|
2014-11-06 01:20:42 +01:00
|
|
|
get_tree().get_root().add_child(current_scene)
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
func _ready():
|
2015-11-22 18:25:19 +01:00
|
|
|
# Get the current scene at the time of initialization
|
|
|
|
current_scene = get_tree().get_current_scene()
|