From 6f50ce143eae6dfbba952e39de68a3bef91fd967 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Agnis=20Aldi=C5=86=C5=A1=20=22NeZv=C4=93rs?= Date: Sat, 2 Nov 2024 09:44:14 +0200 Subject: [PATCH] ThreadUtility.load_resource() --- .../static/ThreadUtility/ThreadUtility.gd | 24 ++++--------------- addons/top_down/scripts/game/PlayerSpawner.gd | 3 ++- .../ui/title_screen/ChangeSceneButton.gd | 3 ++- 3 files changed, 9 insertions(+), 21 deletions(-) diff --git a/addons/great_games_library/static/ThreadUtility/ThreadUtility.gd b/addons/great_games_library/static/ThreadUtility/ThreadUtility.gd index aa010dd9..b58a8e36 100644 --- a/addons/great_games_library/static/ThreadUtility/ThreadUtility.gd +++ b/addons/great_games_library/static/ThreadUtility/ThreadUtility.gd @@ -2,30 +2,16 @@ class_name ThreadUtility extends Node ## Load next scene from path using a thread -static func change_scene(path:String, scene_tree:SceneTree)->void: - # create thread function to load and instantiate a scene, then pass it for scene swapping +static func load_resource(path:String, receive_callback:Callable)->void: + # create thread function to load resource and shut down the tread var _tread_load:Callable = func (path:String, callback:Callable, thread:Thread)->PackedScene: - var _next_scene:Resource = load(path) - assert(_next_scene != null) - callback.call_deferred(_next_scene) + var _resource:Resource = load(path) + callback.call_deferred(_resource) # Thread will finnish itself on main thread using call_deffered. thread.wait_to_finish.call_deferred() return - # Callback function for main thread to swap scenes - var _change_scene:Callable = func (next_scene:PackedScene, scene_tree:SceneTree)->void: - if scene_tree.current_scene != null: - scene_tree.set_meta("current_scene", scene_tree.current_scene) - var _current_scene:Node = scene_tree.get_meta("current_scene") - - scene_tree.root.remove_child(_current_scene) - _current_scene.queue_free() - - var _scene:Node = next_scene.instantiate() - scene_tree.root.add_child.call_deferred(_scene) - scene_tree.set_meta("current_scene", _scene) - # Temporary thread that will shut down itself in the thread function var _thread:Thread = Thread.new() - _thread.start(_tread_load.bind(path, _change_scene.bind(scene_tree), _thread)) + _thread.start(_tread_load.bind(path, receive_callback, _thread)) diff --git a/addons/top_down/scripts/game/PlayerSpawner.gd b/addons/top_down/scripts/game/PlayerSpawner.gd index 4e1b1707..2783daa5 100644 --- a/addons/top_down/scripts/game/PlayerSpawner.gd +++ b/addons/top_down/scripts/game/PlayerSpawner.gd @@ -32,4 +32,5 @@ func on_player_scene_entry()->void: func on_scene_transition()->void: player_instance_resource.parent_reference_resource.node.remove_child(player_reference.node) - get_tree().change_scene_to_file(scene_transition_resource.next_scene_path) + #get_tree().change_scene_to_file(scene_transition_resource.next_scene_path) + ThreadUtility.load_resource(scene_transition_resource.next_scene_path, get_tree().change_scene_to_packed) diff --git a/addons/top_down/scripts/ui/title_screen/ChangeSceneButton.gd b/addons/top_down/scripts/ui/title_screen/ChangeSceneButton.gd index 96380c7d..3de9c900 100644 --- a/addons/top_down/scripts/ui/title_screen/ChangeSceneButton.gd +++ b/addons/top_down/scripts/ui/title_screen/ChangeSceneButton.gd @@ -11,4 +11,5 @@ func pressed()->void: # Avoid triggering multiple times # Deffered because this function is called by that signal button.pressed.disconnect.call_deferred(pressed) - get_tree().change_scene_to_file(scene_path) + #get_tree().change_scene_to_file(scene_path) + ThreadUtility.load_resource(scene_path, get_tree().change_scene_to_packed)