mirror of
https://github.com/godotengine/godot-demo-projects.git
synced 2026-01-05 07:20:07 +01:00
Update loading in a thread demo
This commit is contained in:
@@ -4,7 +4,7 @@ An example using a thread to load an image.
|
|||||||
|
|
||||||
Language: GDScript
|
Language: GDScript
|
||||||
|
|
||||||
Renderer: GLES 2
|
Renderer: Vulkan Mobile
|
||||||
|
|
||||||
Check out this demo on the asset library: https://godotengine.org/asset-library/asset/144
|
Check out this demo on the asset library: https://godotengine.org/asset-library/asset/144
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
[remap]
|
[remap]
|
||||||
|
|
||||||
importer="texture"
|
importer="texture"
|
||||||
type="StreamTexture2D"
|
type="CompressedTexture2D"
|
||||||
path="res://.godot/imported/mona.png-a5ce9963ac8c7ef765aeb0f5428366a9.stex"
|
uid="uid://bcy2b4hw0bvj2"
|
||||||
|
path="res://.godot/imported/mona.png-a5ce9963ac8c7ef765aeb0f5428366a9.ctex"
|
||||||
metadata={
|
metadata={
|
||||||
"vram_texture": false
|
"vram_texture": false
|
||||||
}
|
}
|
||||||
@@ -10,26 +11,24 @@ metadata={
|
|||||||
[deps]
|
[deps]
|
||||||
|
|
||||||
source_file="res://mona.png"
|
source_file="res://mona.png"
|
||||||
dest_files=["res://.godot/imported/mona.png-a5ce9963ac8c7ef765aeb0f5428366a9.stex"]
|
dest_files=["res://.godot/imported/mona.png-a5ce9963ac8c7ef765aeb0f5428366a9.ctex"]
|
||||||
|
|
||||||
[params]
|
[params]
|
||||||
|
|
||||||
compress/mode=0
|
compress/mode=0
|
||||||
compress/lossy_quality=0.7
|
compress/lossy_quality=0.7
|
||||||
compress/hdr_mode=0
|
compress/hdr_compression=1
|
||||||
compress/bptc_ldr=0
|
compress/bptc_ldr=0
|
||||||
compress/normal_map=0
|
compress/normal_map=0
|
||||||
flags/repeat=0
|
compress/channel_pack=0
|
||||||
flags/filter=true
|
mipmaps/generate=false
|
||||||
flags/mipmaps=false
|
mipmaps/limit=-1
|
||||||
flags/anisotropic=false
|
roughness/mode=0
|
||||||
flags/srgb=2
|
roughness/src_normal=""
|
||||||
process/fix_alpha_border=true
|
process/fix_alpha_border=true
|
||||||
process/premult_alpha=false
|
process/premult_alpha=false
|
||||||
process/HDR_as_SRGB=false
|
|
||||||
process/invert_color=false
|
|
||||||
process/normal_map_invert_y=false
|
process/normal_map_invert_y=false
|
||||||
stream=false
|
process/hdr_as_srgb=false
|
||||||
size_limit=0
|
process/hdr_clamp_exposure=false
|
||||||
detect_3d=true
|
process/size_limit=0
|
||||||
svg/scale=1.0
|
detect_3d/compress_to=1
|
||||||
|
|||||||
@@ -6,30 +6,15 @@
|
|||||||
; [section] ; section goes between []
|
; [section] ; section goes between []
|
||||||
; param=value ; assign values to parameters
|
; param=value ; assign values to parameters
|
||||||
|
|
||||||
config_version=4
|
config_version=5
|
||||||
|
|
||||||
[application]
|
[application]
|
||||||
|
|
||||||
config/name="Loading in a Thread"
|
config/name="Loading in a Thread"
|
||||||
config/description="An example using a thread to load an image."
|
config/description="An example using a thread to load an image."
|
||||||
run/main_scene="res://thread.tscn"
|
run/main_scene="res://thread.tscn"
|
||||||
|
config/features=PackedStringArray("4.0")
|
||||||
[display]
|
|
||||||
|
|
||||||
window/dpi/allow_hidpi=true
|
|
||||||
window/stretch/mode="2d"
|
|
||||||
window/stretch/aspect="expand"
|
|
||||||
|
|
||||||
[gdnative]
|
|
||||||
|
|
||||||
singletons=[]
|
|
||||||
|
|
||||||
[memory]
|
|
||||||
|
|
||||||
multithread/thread_rid_pool_prealloc=60
|
|
||||||
|
|
||||||
[rendering]
|
[rendering]
|
||||||
|
|
||||||
quality/driver/driver_name="GLES2"
|
vulkan/rendering/back_end=1
|
||||||
vram_compression/import_etc=true
|
|
||||||
vram_compression/import_etc2=false
|
|
||||||
|
|||||||
@@ -1,28 +1,39 @@
|
|||||||
extends Node2D
|
extends Node2D
|
||||||
|
|
||||||
var thread = Thread.new()
|
var thread: Thread
|
||||||
|
|
||||||
# This function runs in a thread!
|
func _on_load_pressed():
|
||||||
# Threads always take one userdata argument
|
if is_instance_valid(thread) and thread.is_started():
|
||||||
func _bg_load(path):
|
# 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!")
|
print("THREAD FUNC!")
|
||||||
# Load the resource
|
var tex = load(path)
|
||||||
var tex = ResourceLoader.load(path)
|
# call_deferred() tells the main thread to call a method during idle time.
|
||||||
# Call _bg_load_done on main thread
|
# Our method operates on nodes currently in the tree, so it isn't safe to
|
||||||
call_deferred("_bg_load_done")
|
# call directly from another thread.
|
||||||
return tex # return it
|
_bg_load_done.call_deferred()
|
||||||
|
return tex
|
||||||
|
|
||||||
|
|
||||||
func _bg_load_done():
|
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()
|
var tex = thread.wait_to_finish()
|
||||||
# Set to the sprite
|
print("THREAD FINISHED!")
|
||||||
get_node(^"Sprite2D").set_texture(tex)
|
$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():
|
func _exit_tree():
|
||||||
if thread.is_active():
|
# You should always wait for a thread to finish before letting it get freed!
|
||||||
# Already working
|
# It might not clean up correctly if you don't.
|
||||||
return
|
if is_instance_valid(thread) and thread.is_started():
|
||||||
print("START THREAD!")
|
thread.wait_to_finish()
|
||||||
thread.start(self, "_bg_load", "res://mona.png")
|
thread = null
|
||||||
|
|||||||
@@ -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"]
|
[node name="Thread" type="Node2D"]
|
||||||
script = ExtResource( 1 )
|
script = ExtResource("1")
|
||||||
|
|
||||||
[node name="Load" type="Button" parent="."]
|
[node name="Load" type="Button" parent="."]
|
||||||
offset_left = 432.0
|
offset_left = 432.0
|
||||||
@@ -13,9 +13,6 @@ offset_bottom = 114.0
|
|||||||
size_flags_horizontal = 2
|
size_flags_horizontal = 2
|
||||||
size_flags_vertical = 2
|
size_flags_vertical = 2
|
||||||
text = "Load in Thread"
|
text = "Load in Thread"
|
||||||
__meta__ = {
|
|
||||||
"_edit_use_anchors_": false
|
|
||||||
}
|
|
||||||
|
|
||||||
[node name="Sprite2D" type="Sprite2D" parent="."]
|
[node name="Sprite2D" type="Sprite2D" parent="."]
|
||||||
position = Vector2(494, 336)
|
position = Vector2(494, 336)
|
||||||
|
|||||||
Reference in New Issue
Block a user