mlpcardgame/Scenes/Global/Loader.gd
2019-06-01 23:52:16 +02:00

50 lines
No EOL
1.1 KiB
GDScript

extends Node
var loadingScreen = preload("res://Scenes/Loading.tscn")
var loader: ResourceInteractiveLoader = null
var current_scene = null
var load_next: String = ""
onready var root = get_tree().get_root()
onready var loading = $LoadingScreen
func _ready():
set_process(false)
func load_scene(path: String):
current_scene = root.get_child(root.get_child_count() -1)
loading.fade_in()
load_next = path
var delay := 0.1
var cur_delay := 0.0
func set_new_scene(scene):
current_scene.queue_free()
var instance = scene.instance()
instance.connect("loaded", self, "_fade_out")
root.add_child(instance)
root.move_child(instance, 0)
func _process(delta):
if loader == null:
# no need to process anymore
set_process(false)
return
cur_delay += delta
if cur_delay > delay:
cur_delay = 0
var err = loader.poll()
if err == ERR_FILE_EOF: # Finished loading.
var resource = loader.get_resource()
loader = null
set_new_scene(resource)
func _fade_in():
loader = ResourceLoader.load_interactive(load_next)
set_process(true)
func _fade_out():
loading.fade_out()