godot/demos/misc/threads/thread.gd

32 lines
669 B
GDScript3
Raw Normal View History

2014-02-10 01:10:30 +00:00
extends Node2D
# Member variables
2014-02-10 01:10:30 +00:00
var thread = Thread.new()
# This function runs in a thread!
# Threads always take one userdata argument
2014-02-10 01:10:30 +00:00
func _bg_load(path):
print("THREAD FUNC!")
# Load the resource
2014-02-10 01:10:30 +00:00
var tex = ResourceLoader.load(path)
# Call _bg_load_done on main thread
2014-02-10 01:10:30 +00:00
call_deferred("_bg_load_done")
return tex # return it
2014-02-10 01:10:30 +00:00
func _bg_load_done():
# Wait for the thread to complete, get the returned value
2014-02-10 01:10:30 +00:00
var tex = thread.wait_to_finish()
# Set to the sprite
2014-02-10 01:10:30 +00:00
get_node("sprite").set_texture(tex)
2014-02-10 01:10:30 +00:00
func _on_load_pressed():
if (thread.is_active()):
# Already working
2014-02-10 01:10:30 +00:00
return
print("START THREAD!")
thread.start(self, "_bg_load", "res://mona.png")