|
@@ -1,28 +1,39 @@
|
|
|
extends Node2D
|
|
|
|
|
|
-var thread = Thread.new()
|
|
|
+var thread: Thread
|
|
|
|
|
|
-# This function runs in a thread!
|
|
|
-# Threads always take one userdata argument
|
|
|
-func _bg_load(path):
|
|
|
+func _on_load_pressed():
|
|
|
+ if is_instance_valid(thread) and thread.is_started():
|
|
|
+ # If a thread is already running, let it finish before we start another.
|
|
|
+ thread.wait_to_finish()
|
|
|
+ thread = Thread.new()
|
|
|
+ print("START THREAD!")
|
|
|
+ # Our method needs an argument, so we pass it using bind().
|
|
|
+ thread.start(_bg_load.bind("res://mona.png"))
|
|
|
+
|
|
|
+
|
|
|
+func _bg_load(path: String):
|
|
|
print("THREAD FUNC!")
|
|
|
- # Load the resource
|
|
|
- var tex = ResourceLoader.load(path)
|
|
|
- # Call _bg_load_done on main thread
|
|
|
- call_deferred("_bg_load_done")
|
|
|
- return tex # return it
|
|
|
+ var tex = load(path)
|
|
|
+ # call_deferred() tells the main thread to call a method during idle time.
|
|
|
+ # Our method operates on nodes currently in the tree, so it isn't safe to
|
|
|
+ # call directly from another thread.
|
|
|
+ _bg_load_done.call_deferred()
|
|
|
+ return tex
|
|
|
|
|
|
|
|
|
func _bg_load_done():
|
|
|
- # Wait for the thread to complete, get the returned value
|
|
|
+ # Wait for the thread to complete, and get the returned value.
|
|
|
var tex = thread.wait_to_finish()
|
|
|
- # Set to the sprite
|
|
|
- get_node(^"Sprite2D").set_texture(tex)
|
|
|
+ print("THREAD FINISHED!")
|
|
|
+ $Sprite2D.set_texture(tex)
|
|
|
+ # We're done with the thread now, so we can free it.
|
|
|
+ thread = null # Threads are reference counted, so this is how we free it.
|
|
|
|
|
|
|
|
|
-func _on_load_pressed():
|
|
|
- if thread.is_active():
|
|
|
- # Already working
|
|
|
- return
|
|
|
- print("START THREAD!")
|
|
|
- thread.start(self, "_bg_load", "res://mona.png")
|
|
|
+func _exit_tree():
|
|
|
+ # You should always wait for a thread to finish before letting it get freed!
|
|
|
+ # It might not clean up correctly if you don't.
|
|
|
+ if is_instance_valid(thread) and thread.is_started():
|
|
|
+ thread.wait_to_finish()
|
|
|
+ thread = null
|