mirror of
https://github.com/godotengine/godot-demo-projects.git
synced 2025-12-16 05:20:06 +01:00
This leads to code that is easier to understand and runs faster thanks to GDScript's typed instructions. The untyped declaration warning is now enabled on all projects where type hints were added. All projects currently run without any untyped declration warnings. Dodge the Creeps and Squash the Creeps demos intentionally don't use type hints to match the documentation, where type hints haven't been adopted yet (given its beginner focus).
41 lines
1.3 KiB
GDScript
41 lines
1.3 KiB
GDScript
extends Control
|
|
|
|
var thread: Thread
|
|
|
|
func _on_load_pressed() -> void:
|
|
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_rich("[b]Starting 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) -> Texture2D:
|
|
print("Calling thread function.")
|
|
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() -> void:
|
|
# Wait for the thread to complete, and get the returned value.
|
|
var tex: Texture2D = thread.wait_to_finish()
|
|
print_rich("[b][i]Thread finished.\n")
|
|
$TextureRect.texture = tex
|
|
# We're done with the thread now, so we can free it.
|
|
# Threads are reference counted, so this is how we free them.
|
|
thread = null
|
|
|
|
|
|
func _exit_tree() -> void:
|
|
# 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
|