src / helpers / scene_helper.gd

Attached Scenes

Note
No attached scenes.

Code

extends Node
class_name SceneHelper

static var next_scene_path : String


static var change_finished: Signal = (func():
	(SceneHelper as Object).add_user_signal("change_finished")
	return Signal(SceneHelper, "change_finished")).call()


static func preload_scene(file_path: String) -> void:
	next_scene_path = file_path
	ResourceLoader.load_threaded_request(file_path)


# @see https://zenn.dev/tyto/articles/a0f245a7136cb0
static func transit(current: Node, file_path: String, transition: Array = []) -> void:
	if file_path != next_scene_path: preload_scene(file_path)
	if transition:
		var callable = Callable(current, transition[0])
		callable.callv(transition.slice(1, -1))

	var status := ResourceLoader.load_threaded_get_status(file_path)
	while status == ResourceLoader.THREAD_LOAD_IN_PROGRESS:
		await current.get_tree().process_frame
		status = ResourceLoader.load_threaded_get_status(file_path)

	current.get_tree().change_scene_to_packed(
		ResourceLoader.load_threaded_get(file_path))
	current.queue_free()
	change_finished.emit()