Skip to content

Commit

Permalink
Update loading in a thread demo
Browse files Browse the repository at this point in the history
  • Loading branch information
jtnicholl committed Jun 9, 2022
1 parent ae57eb2 commit a9e60f3
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 61 deletions.
2 changes: 0 additions & 2 deletions loading/threads/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ An example using a thread to load an image.

Language: GDScript

Renderer: GLES 2

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

## Screenshots
Expand Down
29 changes: 14 additions & 15 deletions loading/threads/mona.png.import
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
23 changes: 2 additions & 21 deletions loading/threads/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,11 @@
; [section] ; section goes between []
; param=value ; assign values to parameters

config_version=4
config_version=5

[application]

config/name="Loading in a Thread"
config/description="An example using a thread to load an image."
run/main_scene="res://thread.tscn"

[display]

window/dpi/allow_hidpi=true
window/stretch/mode="2d"
window/stretch/aspect="expand"

[gdnative]

singletons=[]

[memory]

multithread/thread_rid_pool_prealloc=60

[rendering]

quality/driver/driver_name="GLES2"
vram_compression/import_etc=true
vram_compression/import_etc2=false
config/features=PackedStringArray("4.0")
46 changes: 29 additions & 17 deletions loading/threads/thread.gd
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
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!")
thread.start(_bg_load, "res://mona.png")


# This is the function we'll run in a thread!
# Threads always expect the method they call to take one argument.
func _bg_load(path: String):
print("THREAD FUNC!")
# Load the resource
var tex = ResourceLoader.load(path)
# Call _bg_load_done on main thread
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.
call_deferred("_bg_load_done")
return tex # return it
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("TREAD 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
9 changes: 3 additions & 6 deletions loading/threads/thread.tscn
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[gd_scene load_steps=2 format=2]
[gd_scene load_steps=2 format=3 uid="uid://df1dmjx4ny0gd"]

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

[node name="Thread" type="Node2D"]
script = ExtResource( 1 )
script = ExtResource( "1" )

[node name="Load" type="Button" parent="."]
offset_left = 432.0
Expand All @@ -13,9 +13,6 @@ offset_bottom = 114.0
size_flags_horizontal = 2
size_flags_vertical = 2
text = "Load in Thread"
__meta__ = {
"_edit_use_anchors_": false
}

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

0 comments on commit a9e60f3

Please sign in to comment.