-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathThreadUtility.gd
52 lines (42 loc) · 2.24 KB
/
ThreadUtility.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class_name ThreadUtility
extends Node
## Load a resource from path using a thread
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 _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
# Temporary thread that will shut down itself in the thread function
var _thread:Thread = Thread.new()
_thread.start(_tread_load.bind(path, receive_callback, _thread))
## Load a resource from data list {path:String, callback:Callable(resource)}
static func load_resource_list(data_list:Array[Dictionary], finish_callback:Callable = Callable())->void:
# create thread function to load resources and shut down the tread
var _tread_load:Callable = func (data_list:Array[Dictionary], finish_callback:Callable, thread:Thread)->PackedScene:
for _data:Dictionary in data_list:
var _resource:Resource = load(_data.path)
_data.callback.call_deferred(_resource)
if !finish_callback.is_null() && finish_callback.is_valid():
finish_callback.call_deferred()
# Thread will finnish itself on main thread using call_deffered.
thread.wait_to_finish.call_deferred()
return
# Temporary thread that will shut down itself in the thread function
var _thread:Thread = Thread.new()
_thread.start(_tread_load.bind(data_list, finish_callback, _thread))
static func callback_list(callback_list:Array[Callable], finish_callback:Callable)->void:
var _thread_func:Callable = func (callback_list:Array[Callable], finish_callback:Callable, thread:Thread)->void:
for _callback:Callable in callback_list:
assert(_callback.is_null() && _callback.is_valid())
_callback.call()
if !finish_callback.is_null() && finish_callback.is_valid():
finish_callback.call_deferred()
# Thread will finnish itself on main thread using call_deffered.
thread.wait_to_finish.call_deferred()
return
# Temporary thread that will shut down itself in the thread function
var _thread:Thread = Thread.new()
_thread.start(_thread_func.bind(callback_list, finish_callback, _thread))