-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #747 from jtnicholl/loading_threads
Update loading in a thread demo for 4.0
- Loading branch information
Showing
5 changed files
with
50 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,34 @@ | ||
[remap] | ||
|
||
importer="texture" | ||
type="StreamTexture2D" | ||
path="res://.godot/imported/mona.png-a5ce9963ac8c7ef765aeb0f5428366a9.stex" | ||
type="CompressedTexture2D" | ||
uid="uid://bcy2b4hw0bvj2" | ||
path="res://.godot/imported/mona.png-a5ce9963ac8c7ef765aeb0f5428366a9.ctex" | ||
metadata={ | ||
"vram_texture": false | ||
} | ||
|
||
[deps] | ||
|
||
source_file="res://mona.png" | ||
dest_files=["res://.godot/imported/mona.png-a5ce9963ac8c7ef765aeb0f5428366a9.stex"] | ||
dest_files=["res://.godot/imported/mona.png-a5ce9963ac8c7ef765aeb0f5428366a9.ctex"] | ||
|
||
[params] | ||
|
||
compress/mode=0 | ||
compress/lossy_quality=0.7 | ||
compress/hdr_mode=0 | ||
compress/hdr_compression=1 | ||
compress/bptc_ldr=0 | ||
compress/normal_map=0 | ||
flags/repeat=0 | ||
flags/filter=true | ||
flags/mipmaps=false | ||
flags/anisotropic=false | ||
flags/srgb=2 | ||
compress/channel_pack=0 | ||
mipmaps/generate=false | ||
mipmaps/limit=-1 | ||
roughness/mode=0 | ||
roughness/src_normal="" | ||
process/fix_alpha_border=true | ||
process/premult_alpha=false | ||
process/HDR_as_SRGB=false | ||
process/invert_color=false | ||
process/normal_map_invert_y=false | ||
stream=false | ||
size_limit=0 | ||
detect_3d=true | ||
svg/scale=1.0 | ||
process/hdr_as_srgb=false | ||
process/hdr_clamp_exposure=false | ||
process/size_limit=0 | ||
detect_3d/compress_to=1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters