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

@@ -17,6 +17,10 @@ run/main_scene="res://scene/combo.tscn"
config/features=PackedStringArray("4.2")
config/icon="res://icon.webp"
[debug]
gdscript/warnings/untyped_declaration=1
[display]
window/stretch/mode="canvas_items"

View File

@@ -1,50 +1,42 @@
[gd_scene load_steps=2 format=2]
[gd_scene load_steps=2 format=3 uid="uid://dqxum77awcw6u"]
[ext_resource path="res://script/game.gd" type="Script" id=1]
[ext_resource type="Script" path="res://script/game.gd" id="1"]
[node name="Game" type="Control"]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
mouse_filter = 1
size_flags_horizontal = 3
size_flags_vertical = 3
script = ExtResource( 1 )
__meta__ = {
"_edit_use_anchors_": false
}
mouse_filter = 1
script = ExtResource("1")
[node name="HBoxContainer" type="HBoxContainer" parent="."]
layout_mode = 0
anchor_right = 1.0
anchor_bottom = 1.0
[node name="RichTextLabel" type="RichTextLabel" parent="HBoxContainer"]
offset_right = 510.0
offset_bottom = 600.0
layout_mode = 2
size_flags_horizontal = 3
[node name="VBoxContainer" type="VBoxContainer" parent="HBoxContainer"]
offset_left = 514.0
offset_right = 1024.0
offset_bottom = 600.0
layout_mode = 2
size_flags_horizontal = 3
[node name="Label" type="Label" parent="HBoxContainer/VBoxContainer"]
offset_right = 510.0
offset_bottom = 14.0
layout_mode = 2
text = "Players:"
[node name="ItemList" type="ItemList" parent="HBoxContainer/VBoxContainer"]
offset_top = 18.0
offset_right = 510.0
offset_bottom = 576.0
layout_mode = 2
size_flags_horizontal = 3
size_flags_vertical = 3
same_column_width = true
[node name="Action" type="Button" parent="HBoxContainer/VBoxContainer"]
offset_top = 580.0
offset_right = 510.0
offset_bottom = 600.0
layout_mode = 2
disabled = true
text = "Do Action!"

View File

@@ -1,7 +1,7 @@
[gd_scene load_steps=3 format=3 uid="uid://c240icwf4uov8"]
[ext_resource type="Script" path="res://script/main.gd" id="1"]
[ext_resource type="PackedScene" path="res://scene/game.tscn" id="2"]
[ext_resource type="PackedScene" uid="uid://dqxum77awcw6u" path="res://scene/game.tscn" id="2"]
[node name="Main" type="Control"]
layout_mode = 3
@@ -15,14 +15,14 @@ size_flags_vertical = 3
script = ExtResource("1")
[node name="Panel" type="Panel" parent="."]
anchors_preset = 15
layout_mode = 0
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
[node name="VBoxContainer" type="VBoxContainer" parent="Panel"]
anchors_preset = 15
layout_mode = 0
anchor_right = 1.0
anchor_bottom = 1.0
offset_left = 20.0
@@ -33,84 +33,55 @@ grow_horizontal = 2
grow_vertical = 2
[node name="HBoxContainer" type="HBoxContainer" parent="Panel/VBoxContainer"]
offset_right = 1112.0
offset_bottom = 31.0
layout_mode = 2
[node name="Label" type="Label" parent="Panel/VBoxContainer/HBoxContainer"]
offset_top = 2.0
offset_right = 369.0
offset_bottom = 28.0
layout_mode = 2
size_flags_horizontal = 3
text = "Name"
[node name="NameEdit" type="LineEdit" parent="Panel/VBoxContainer/HBoxContainer"]
offset_left = 373.0
offset_right = 1112.0
offset_bottom = 31.0
layout_mode = 2
size_flags_horizontal = 3
size_flags_stretch_ratio = 2.0
text = "A Godot User"
[node name="HBoxContainer2" type="HBoxContainer" parent="Panel/VBoxContainer"]
offset_top = 35.0
offset_right = 1112.0
offset_bottom = 66.0
layout_mode = 2
[node name="HBoxContainer" type="HBoxContainer" parent="Panel/VBoxContainer/HBoxContainer2"]
offset_right = 369.0
offset_bottom = 31.0
layout_mode = 2
size_flags_horizontal = 3
[node name="Host" type="Button" parent="Panel/VBoxContainer/HBoxContainer2/HBoxContainer"]
offset_right = 44.0
offset_bottom = 31.0
layout_mode = 2
text = "Host"
[node name="Control" type="Control" parent="Panel/VBoxContainer/HBoxContainer2/HBoxContainer"]
layout_mode = 3
anchors_preset = 0
offset_left = 48.0
offset_right = 273.0
offset_bottom = 31.0
layout_mode = 2
size_flags_horizontal = 3
[node name="Connect" type="Button" parent="Panel/VBoxContainer/HBoxContainer2/HBoxContainer"]
offset_left = 277.0
offset_right = 369.0
offset_bottom = 31.0
layout_mode = 2
text = "Connect to"
[node name="Disconnect" type="Button" parent="Panel/VBoxContainer/HBoxContainer2/HBoxContainer"]
visible = false
offset_left = 68.0
offset_right = 152.0
offset_bottom = 24.0
layout_mode = 2
text = "Disconnect"
[node name="Hostname" type="LineEdit" parent="Panel/VBoxContainer/HBoxContainer2"]
offset_left = 373.0
offset_right = 1112.0
offset_bottom = 31.0
layout_mode = 2
size_flags_horizontal = 3
size_flags_stretch_ratio = 2.0
text = "localhost"
placeholder_text = "localhost"
[node name="Control" type="Control" parent="Panel/VBoxContainer"]
layout_mode = 3
anchors_preset = 0
offset_top = 70.0
offset_right = 1112.0
offset_bottom = 70.0
layout_mode = 2
[node name="Game" parent="Panel/VBoxContainer" instance=ExtResource("2")]
layout_mode = 3
anchors_preset = 0
anchor_right = 0.0
anchor_bottom = 0.0
offset_top = 74.0
offset_right = 1112.0
offset_bottom = 608.0
layout_mode = 2
[node name="AcceptDialog" type="AcceptDialog" parent="."]
dialog_text = "Connection closed"

View File

@@ -1,8 +1,9 @@
extends Control
var paths := []
var paths: Array[NodePath] = []
func _enter_tree():
func _enter_tree() -> void:
for ch in $GridContainer.get_children():
paths.append(NodePath(str(get_path()) + "/GridContainer/" + str(ch.name)))
# Sets a dedicated Multiplayer API for each branch.
@@ -10,7 +11,7 @@ func _enter_tree():
get_tree().set_multiplayer(MultiplayerAPI.create_default_interface(), path)
func _exit_tree():
func _exit_tree() -> void:
# Clear the branch-specific Multiplayer API.
for path in paths:
get_tree().set_multiplayer(null, path)

View File

@@ -2,39 +2,40 @@ extends Control
const _crown = preload("res://img/crown.png")
@onready var _list = $HBoxContainer/VBoxContainer/ItemList
@onready var _action = $HBoxContainer/VBoxContainer/Action
@onready var _list: ItemList = $HBoxContainer/VBoxContainer/ItemList
@onready var _action: Button = $HBoxContainer/VBoxContainer/Action
const ACTIONS = ["roll", "pass"]
var _players = []
var _turn = -1
var _players: Array[int] = []
var _turn := -1
@rpc
func _log(what):
$HBoxContainer/RichTextLabel.add_text(what + "\n")
func _log(message: String) -> void:
$HBoxContainer/RichTextLabel.add_text(message + "\n")
@rpc("any_peer")
func set_player_name(p_name):
func set_player_name(p_name: String) -> void:
if not is_multiplayer_authority():
return
var sender = multiplayer.get_remote_sender_id()
var sender := multiplayer.get_remote_sender_id()
update_player_name.rpc(sender, p_name)
@rpc("call_local")
func update_player_name(player, p_name):
var pos = _players.find(player)
func update_player_name(player: int, p_name: String) -> void:
var pos := _players.find(player)
if pos != -1:
_list.set_item_text(pos, p_name)
@rpc("any_peer")
func request_action(action):
func request_action(action: String) -> void:
if not is_multiplayer_authority():
return
var sender = multiplayer.get_remote_sender_id()
var sender := multiplayer.get_remote_sender_id()
if _players[_turn] != sender:
_log.rpc("Someone is trying to cheat! %s" % str(sender))
return
@@ -46,88 +47,97 @@ func request_action(action):
next_turn()
func do_action(action):
var player_name = _list.get_item_text(_turn)
var val = randi() % 100
func do_action(action: String) -> void:
var player_name := _list.get_item_text(_turn)
var val := randi() % 100
_log.rpc("%s: %ss %d" % [player_name, action, val])
@rpc("call_local")
func set_turn(turn):
func set_turn(turn: int) -> void:
_turn = turn
if turn >= _players.size():
return
for i in range(0, _players.size()):
for i in _players.size():
if i == turn:
_list.set_item_icon(i, _crown)
else:
_list.set_item_icon(i, null)
_action.disabled = _players[turn] != multiplayer.get_unique_id()
@rpc("call_local")
func del_player(id):
var pos = _players.find(id)
func del_player(id: int) -> void:
var pos := _players.find(id)
if pos == -1:
return
_players.remove_at(pos)
_list.remove_item(pos)
if _turn > pos:
_turn -= 1
if multiplayer.is_server():
set_turn.rpc(_turn)
@rpc("call_local")
func add_player(id, pname=""):
func add_player(id: int, p_name: String = "") -> void:
_players.append(id)
if pname == "":
if p_name == "":
_list.add_item("... connecting ...", null, false)
else:
_list.add_item(pname, null, false)
_list.add_item(p_name, null, false)
func get_player_name(pos):
func get_player_name(pos: int) -> String:
if pos < _list.get_item_count():
return _list.get_item_text(pos)
else:
return "Error!"
func next_turn():
func next_turn() -> void:
_turn += 1
if _turn >= _players.size():
_turn = 0
set_turn.rpc(_turn)
func start():
func start() -> void:
set_turn(0)
func stop():
func stop() -> void:
_players.clear()
_list.clear()
_turn = 0
_action.disabled = true
func on_peer_add(id):
func on_peer_add(id: int) -> void:
if not multiplayer.is_server():
return
for i in range(0, _players.size()):
for i in _players.size():
add_player.rpc_id(id, _players[i], get_player_name(i))
add_player.rpc(id)
set_turn.rpc_id(id, _turn)
func on_peer_del(id):
func on_peer_del(id: int) -> void:
if not multiplayer.is_server():
return
del_player.rpc(id)
func _on_Action_pressed():
func _on_Action_pressed() -> void:
if multiplayer.is_server():
if _turn != 0:
return

View File

@@ -3,21 +3,21 @@ extends Control
const DEF_PORT = 8080
const PROTO_NAME = "ludus"
@onready var _host_btn = $Panel/VBoxContainer/HBoxContainer2/HBoxContainer/Host
@onready var _connect_btn = $Panel/VBoxContainer/HBoxContainer2/HBoxContainer/Connect
@onready var _disconnect_btn = $Panel/VBoxContainer/HBoxContainer2/HBoxContainer/Disconnect
@onready var _name_edit = $Panel/VBoxContainer/HBoxContainer/NameEdit
@onready var _host_edit = $Panel/VBoxContainer/HBoxContainer2/Hostname
@onready var _game = $Panel/VBoxContainer/Game
@onready var _host_btn: Button = $Panel/VBoxContainer/HBoxContainer2/HBoxContainer/Host
@onready var _connect_btn: Button = $Panel/VBoxContainer/HBoxContainer2/HBoxContainer/Connect
@onready var _disconnect_btn: Button = $Panel/VBoxContainer/HBoxContainer2/HBoxContainer/Disconnect
@onready var _name_edit: LineEdit = $Panel/VBoxContainer/HBoxContainer/NameEdit
@onready var _host_edit: LineEdit = $Panel/VBoxContainer/HBoxContainer2/Hostname
@onready var _game: Control = $Panel/VBoxContainer/Game
var peer = WebSocketMultiplayerPeer.new()
var peer := WebSocketMultiplayerPeer.new()
func _init():
func _init() -> void:
peer.supported_protocols = ["ludus"]
func _ready():
func _ready() -> void:
multiplayer.peer_connected.connect(_peer_connected)
multiplayer.peer_disconnected.connect(_peer_disconnected)
multiplayer.server_disconnected.connect(_close_network)
@@ -30,11 +30,11 @@ func _ready():
if OS.has_environment("USERNAME"):
_name_edit.text = OS.get_environment("USERNAME")
else:
var desktop_path = OS.get_system_dir(OS.SYSTEM_DIR_DESKTOP).replace("\\", "/").split("/")
var desktop_path := OS.get_system_dir(OS.SYSTEM_DIR_DESKTOP).replace("\\", "/").split("/")
_name_edit.text = desktop_path[desktop_path.size() - 2]
func start_game():
func start_game() -> void:
_host_btn.disabled = true
_name_edit.editable = false
_host_edit.editable = false
@@ -43,7 +43,7 @@ func start_game():
_game.start()
func stop_game():
func stop_game() -> void:
_host_btn.disabled = false
_name_edit.editable = true
_host_edit.editable = true
@@ -52,7 +52,7 @@ func stop_game():
_game.stop()
func _close_network():
func _close_network() -> void:
stop_game()
$AcceptDialog.popup_centered()
$AcceptDialog.get_ok_button().grab_focus()
@@ -60,20 +60,20 @@ func _close_network():
peer.close()
func _connected():
func _connected() -> void:
_game.set_player_name.rpc(_name_edit.text)
func _peer_connected(id):
func _peer_connected(id: int) -> void:
_game.on_peer_add(id)
func _peer_disconnected(id):
func _peer_disconnected(id: int) -> void:
print("Disconnected %d" % id)
_game.on_peer_del(id)
func _on_Host_pressed():
func _on_Host_pressed() -> void:
multiplayer.multiplayer_peer = null
peer.create_server(DEF_PORT)
multiplayer.multiplayer_peer = peer
@@ -81,11 +81,11 @@ func _on_Host_pressed():
start_game()
func _on_Disconnect_pressed():
func _on_Disconnect_pressed() -> void:
_close_network()
func _on_Connect_pressed():
func _on_Connect_pressed() -> void:
multiplayer.multiplayer_peer = null
peer.create_client("ws://" + _host_edit.text + ":" + str(DEF_PORT))
multiplayer.multiplayer_peer = peer