Skip to content

Commit 02c6a26

Browse files
committed
Update loading in a thread demo
1 parent ae57eb2 commit 02c6a26

File tree

5 files changed

+50
-58
lines changed

5 files changed

+50
-58
lines changed

loading/threads/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ An example using a thread to load an image.
44

55
Language: GDScript
66

7-
Renderer: GLES 2
7+
Renderer: Vulkan Mobile
88

99
Check out this demo on the asset library: https://godotengine.org/asset-library/asset/144
1010

loading/threads/mona.png.import

+14-15
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,34 @@
11
[remap]
22

33
importer="texture"
4-
type="StreamTexture2D"
5-
path="res://.godot/imported/mona.png-a5ce9963ac8c7ef765aeb0f5428366a9.stex"
4+
type="CompressedTexture2D"
5+
uid="uid://bcy2b4hw0bvj2"
6+
path="res://.godot/imported/mona.png-a5ce9963ac8c7ef765aeb0f5428366a9.ctex"
67
metadata={
78
"vram_texture": false
89
}
910

1011
[deps]
1112

1213
source_file="res://mona.png"
13-
dest_files=["res://.godot/imported/mona.png-a5ce9963ac8c7ef765aeb0f5428366a9.stex"]
14+
dest_files=["res://.godot/imported/mona.png-a5ce9963ac8c7ef765aeb0f5428366a9.ctex"]
1415

1516
[params]
1617

1718
compress/mode=0
1819
compress/lossy_quality=0.7
19-
compress/hdr_mode=0
20+
compress/hdr_compression=1
2021
compress/bptc_ldr=0
2122
compress/normal_map=0
22-
flags/repeat=0
23-
flags/filter=true
24-
flags/mipmaps=false
25-
flags/anisotropic=false
26-
flags/srgb=2
23+
compress/channel_pack=0
24+
mipmaps/generate=false
25+
mipmaps/limit=-1
26+
roughness/mode=0
27+
roughness/src_normal=""
2728
process/fix_alpha_border=true
2829
process/premult_alpha=false
29-
process/HDR_as_SRGB=false
30-
process/invert_color=false
3130
process/normal_map_invert_y=false
32-
stream=false
33-
size_limit=0
34-
detect_3d=true
35-
svg/scale=1.0
31+
process/hdr_as_srgb=false
32+
process/hdr_clamp_exposure=false
33+
process/size_limit=0
34+
detect_3d/compress_to=1

loading/threads/project.godot

+3-18
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,15 @@
66
; [section] ; section goes between []
77
; param=value ; assign values to parameters
88

9-
config_version=4
9+
config_version=5
1010

1111
[application]
1212

1313
config/name="Loading in a Thread"
1414
config/description="An example using a thread to load an image."
1515
run/main_scene="res://thread.tscn"
16-
17-
[display]
18-
19-
window/dpi/allow_hidpi=true
20-
window/stretch/mode="2d"
21-
window/stretch/aspect="expand"
22-
23-
[gdnative]
24-
25-
singletons=[]
26-
27-
[memory]
28-
29-
multithread/thread_rid_pool_prealloc=60
16+
config/features=PackedStringArray("4.0")
3017

3118
[rendering]
3219

33-
quality/driver/driver_name="GLES2"
34-
vram_compression/import_etc=true
35-
vram_compression/import_etc2=false
20+
vulkan/rendering/back_end=1

loading/threads/thread.gd

+29-18
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,39 @@
11
extends Node2D
22

3-
var thread = Thread.new()
3+
var thread: Thread
44

5-
# This function runs in a thread!
6-
# Threads always take one userdata argument
7-
func _bg_load(path):
5+
func _on_load_pressed():
6+
if is_instance_valid(thread) and thread.is_started():
7+
# If a thread is already running, let it finish before we start another.
8+
thread.wait_to_finish()
9+
thread = Thread.new()
10+
print("START THREAD!")
11+
# Our method needs an argument, so we pass it using bind().
12+
thread.start(_bg_load.bind("res://mona.png"))
13+
14+
15+
func _bg_load(path: String):
816
print("THREAD FUNC!")
9-
# Load the resource
10-
var tex = ResourceLoader.load(path)
11-
# Call _bg_load_done on main thread
12-
call_deferred("_bg_load_done")
13-
return tex # return it
17+
var tex = load(path)
18+
# call_deferred() tells the main thread to call a method during idle time.
19+
# Our method operates on nodes currently in the tree, so it isn't safe to
20+
# call directly from another thread.
21+
_bg_load_done.call_deferred()
22+
return tex
1423

1524

1625
func _bg_load_done():
17-
# Wait for the thread to complete, get the returned value
26+
# Wait for the thread to complete, and get the returned value.
1827
var tex = thread.wait_to_finish()
19-
# Set to the sprite
20-
get_node(^"Sprite2D").set_texture(tex)
28+
print("THREAD FINISHED!")
29+
$Sprite2D.set_texture(tex)
30+
# We're done with the thread now, so we can free it.
31+
thread = null # Threads are reference counted, so this is how we free it.
2132

2233

23-
func _on_load_pressed():
24-
if thread.is_active():
25-
# Already working
26-
return
27-
print("START THREAD!")
28-
thread.start(self, "_bg_load", "res://mona.png")
34+
func _exit_tree():
35+
# You should always wait for a thread to finish before letting it get freed!
36+
# It might not clean up correctly if you don't.
37+
if is_instance_valid(thread) and thread.is_started():
38+
thread.wait_to_finish()
39+
thread = null

loading/threads/thread.tscn

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
[gd_scene load_steps=2 format=2]
1+
[gd_scene load_steps=2 format=3 uid="uid://df1dmjx4ny0gd"]
22

3-
[ext_resource path="res://thread.gd" type="Script" id=1]
3+
[ext_resource type="Script" path="res://thread.gd" id="1"]
44

55
[node name="Thread" type="Node2D"]
6-
script = ExtResource( 1 )
6+
script = ExtResource("1")
77

88
[node name="Load" type="Button" parent="."]
99
offset_left = 432.0
@@ -13,9 +13,6 @@ offset_bottom = 114.0
1313
size_flags_horizontal = 2
1414
size_flags_vertical = 2
1515
text = "Load in Thread"
16-
__meta__ = {
17-
"_edit_use_anchors_": false
18-
}
1916

2017
[node name="Sprite2D" type="Sprite2D" parent="."]
2118
position = Vector2(494, 336)

0 commit comments

Comments
 (0)