mlpcardgame/Scenes/Global/Loader.gd

51 lines
1.1 KiB
GDScript3
Raw Normal View History

2019-06-01 21:52:16 +00:00
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 = get_tree().current_scene
2019-06-01 21:52:16 +00:00
loading.fade_in()
load_next = path
var delay := 0.1
var cur_delay := 0.0
func set_new_scene(scene):
var instance = scene.instance()
instance.connect("loaded", self, "_fade_out")
root.add_child(instance)
root.move_child(instance, 0)
get_tree().current_scene = instance
current_scene.queue_free()
2019-06-01 21:52:16 +00:00
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()