Use static typing in all demos (#1063)

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).
This commit is contained in:
Hugo Locurcio
2024-06-01 12:12:18 +02:00
committed by GitHub
parent 8e9c180278
commit bac1e69164
498 changed files with 5218 additions and 4776 deletions

View File

@@ -20,6 +20,10 @@ config/icon="res://icon.webp"
run/stretch/aspect="expand"
run/stretch/mode="canvas_items"
[debug]
gdscript/warnings/untyped_declaration=1
[display]
window/stretch/mode="canvas_items"

View File

@@ -1,22 +1,20 @@
extends Control
var thread: Thread
func _on_load_pressed():
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("START THREAD!")
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):
print("THREAD FUNC!")
var tex = load(path)
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.
@@ -24,16 +22,17 @@ func _bg_load(path: String):
return tex
func _bg_load_done():
func _bg_load_done() -> void:
# Wait for the thread to complete, and get the returned value.
var tex = thread.wait_to_finish()
print("THREAD FINISHED!")
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.
thread = null # Threads are reference counted, so this is how we free them.
# Threads are reference counted, so this is how we free them.
thread = null
func _exit_tree():
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():

View File

@@ -13,25 +13,35 @@ script = ExtResource("1")
[node name="Load" type="Button" parent="."]
layout_mode = 1
anchors_preset = 5
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -115.0
offset_top = 96.0
offset_top = -227.5
offset_right = 115.0
offset_bottom = 151.0
offset_bottom = -172.5
grow_horizontal = 2
grow_vertical = 2
size_flags_horizontal = 2
size_flags_vertical = 2
theme_override_font_sizes/font_size = 24
text = "Load in Thread"
[node name="ColorRect" type="Panel" parent="."]
layout_mode = 0
offset_left = 461.0
offset_top = 160.0
offset_right = 692.0
offset_bottom = 489.0
layout_mode = 1
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -115.5
offset_top = -164.5
offset_right = 115.5
offset_bottom = 164.5
grow_horizontal = 2
grow_vertical = 2
[node name="TextureRect" type="TextureRect" parent="."]
layout_mode = 1