Skip to content

Commit

Permalink
Merge pull request #117 from nezvers/player_spawn_and_rooms
Browse files Browse the repository at this point in the history
ThreadUtility.load_resource()
  • Loading branch information
nezvers authored Nov 2, 2024
2 parents a89bb92 + 6f50ce1 commit 7c9a1c7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 21 deletions.
24 changes: 5 additions & 19 deletions addons/great_games_library/static/ThreadUtility/ThreadUtility.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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))
3 changes: 2 additions & 1 deletion addons/top_down/scripts/game/PlayerSpawner.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
3 changes: 2 additions & 1 deletion addons/top_down/scripts/ui/title_screen/ChangeSceneButton.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 7c9a1c7

Please sign in to comment.