summaryrefslogtreecommitdiff
path: root/demos/misc/threads/thread.gd
blob: 1ef67e403c7eb388972168e9bd1d60e4f47313b4 (plain)
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

extends Node2D

# Member variables
var thread = Thread.new()


# This function runs in a thread!
# Threads always take one userdata argument
func _bg_load(path):
	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


func _bg_load_done():
	# Wait for the thread to complete, get the returned value
	var tex = thread.wait_to_finish()
	# Set to the sprite
	get_node("sprite").set_texture(tex)


func _on_load_pressed():
	if (thread.is_active()):
		# Already working
		return
	print("START THREAD!")
	thread.start(self, "_bg_load", "res://mona.png")