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

@@ -1,35 +1,32 @@
extends Camera3D
const ROTATION_COEFF = 0.02
var _rotation_enabled = false
var _rotation_pivot
var _rotation_enabled := false
var _rotation_pivot: Node3D
func _ready():
func _ready() -> void:
_initialize_pivot.call_deferred()
func _unhandled_input(event):
var mouse_button_event = event as InputEventMouseButton
if mouse_button_event:
if mouse_button_event.button_index == MOUSE_BUTTON_RIGHT:
_rotation_enabled = mouse_button_event.pressed
func _unhandled_input(event: InputEvent) -> void:
if event is InputEventMouseButton:
if event.button_index == MOUSE_BUTTON_RIGHT:
_rotation_enabled = event.pressed
return
if not _rotation_enabled:
return
var mouse_motion_event = event as InputEventMouseMotion
if mouse_motion_event:
var rotation_delta = mouse_motion_event.relative.x
if event is InputEventMouseMotion:
var rotation_delta: float = event.relative.x
_rotation_pivot.rotate(Vector3.UP, -rotation_delta * ROTATION_COEFF)
func _initialize_pivot():
func _initialize_pivot() -> void:
_rotation_pivot = Node3D.new()
var camera_parent = get_parent()
var camera_parent := get_parent()
camera_parent.add_child(_rotation_pivot)
camera_parent.remove_child(self)
_rotation_pivot.add_child(self)

View File

@@ -1,12 +1,11 @@
extends CharacterBody3D
@export var _stop_on_slopes := false
@export var use_snap := false
@export var _stop_on_slopes = false
@export var use_snap = false
var _gravity := 20.0
var _gravity = 20.0
func _physics_process(delta):
func _physics_process(delta: float) -> void:
if is_on_floor():
floor_snap_length = 0.2
else:

View File

@@ -1,40 +1,39 @@
extends Control
const MAX_ENTRIES = 100
var _entry_template
var _entry_template: Label
func _enter_tree():
func _enter_tree() -> void:
Log.entry_logged.connect(_on_log_entry)
_entry_template = get_child(0) as Label
_entry_template = get_child(0)
remove_child(_entry_template)
func _exit_tree():
func _exit_tree() -> void:
_entry_template.free()
func clear():
func clear() -> void:
while get_child_count():
var entry = get_child(get_child_count() - 1)
var entry: Label = get_child(get_child_count() - 1)
remove_child(entry)
entry.queue_free()
func _on_log_entry(message, type):
var new_entry = _entry_template.duplicate() as Label
func _on_log_entry(message: String, type: Log.LogType) -> void:
var new_entry: Label = _entry_template.duplicate()
new_entry.set_text(message)
new_entry.text = message
if type == Log.LogType.ERROR:
new_entry.modulate = Color.RED
else:
new_entry.modulate = Color.WHITE
if get_child_count() >= MAX_ENTRIES:
var first_entry = get_child(0) as Label
var first_entry: Label = get_child(0)
remove_child(first_entry)
first_entry.queue_free()

View File

@@ -1,30 +1,28 @@
extends Control
@export var world_offset := Vector3.ZERO
@export var world_offset = Vector3.ZERO
var _pos_offset: Vector2
var _attachment: Node3D
var _pos_offset
var _attachment
func _ready():
func _ready() -> void:
_pos_offset = position
_attachment = get_parent() as Node3D
_attachment = get_parent()
func _process(_delta):
func _process(_delta: float) -> void:
if _attachment == null:
return
var viewport = get_viewport()
var viewport := get_viewport()
if viewport == null:
return
var camera = viewport.get_camera_3d()
var camera := viewport.get_camera_3d()
if camera == null:
return
var world_pos = world_offset + _attachment.global_transform.origin
var screen_pos = camera.unproject_position(world_pos)
var world_pos := world_offset + _attachment.global_transform.origin
var screen_pos := camera.unproject_position(world_pos)
position = _pos_offset + screen_pos - 0.5 * size

View File

@@ -1,12 +1,12 @@
extends Label
func _process(_delta):
var engine_name = ""
func _ready() -> void:
var engine_name := ""
match System.get_physics_engine():
System.PhysicsEngine.GODOT_PHYSICS:
engine_name = "GodotPhysics 3D"
System.PhysicsEngine.OTHER:
var engine_setting = ProjectSettings.get_setting("physics/3d/physics_engine")
var engine_setting := str(ProjectSettings.get_setting("physics/3d/physics_engine"))
engine_name = "Other (%s)" % engine_setting
set_text("Physics engine: %s" % engine_name)
text = "Physics engine: %s" % engine_name

View File

@@ -1,5 +1,4 @@
extends Label
func _process(_delta):
set_text("FPS: %d" % Engine.get_frames_per_second())
func _process(_delta: float) -> void:
text = "%d FPS (%.2f mspf)" % [Engine.get_frames_per_second(), 1000.0 / Engine.get_frames_per_second()]

View File

@@ -1,5 +1,4 @@
extends Label
func _process(_delta):
func _process(_delta: float) -> void:
visible = get_tree().paused

View File

@@ -1,13 +1,12 @@
extends Label
var test_name = "":
var test_name := "":
set(value):
if (test_name != value):
return
test_name = value
set_text("Test: %s" % test_name)
text = "Test: %s" % test_name
func _ready():
set_text("Select a test from the menu to start it")
func _ready() -> void:
text = "Select a test from the menu to start it"

View File

@@ -1,5 +1,4 @@
extends Label
func _process(_delta):
func _process(_delta: float) -> void:
set_text("Godot Version: %s" % Engine.get_version_info().string)

View File

@@ -1,24 +1,22 @@
class_name OptionMenu
extends MenuButton
signal option_selected(item_path: String)
signal option_changed(item_path: String, checked: bool)
signal option_selected(item_path)
signal option_changed(item_path, checked)
func add_menu_item(item_path, checkbox = false, checked = false):
var path_elements = item_path.split("/", false)
var path_element_count = path_elements.size()
func add_menu_item(item_path: String, checkbox: bool = false, checked: bool = false) -> void:
var path_elements := item_path.split("/", false)
var path_element_count := path_elements.size()
assert(path_element_count > 0)
var path = ""
var popup = get_popup()
for element_index in range(path_element_count - 1):
var popup_label = path_elements[element_index]
var path := ""
var popup := get_popup()
for element_index in path_element_count - 1:
var popup_label := path_elements[element_index]
path += popup_label + "/"
popup = _add_popup(popup, path, popup_label)
var label = path_elements[path_element_count - 1]
var label := path_elements[path_element_count - 1]
if checkbox:
popup.add_check_item(label)
popup.set_item_checked(popup.get_item_count() - 1, checked)
@@ -26,18 +24,18 @@ func add_menu_item(item_path, checkbox = false, checked = false):
popup.add_item(label)
func _add_item(parent_popup, label):
func _add_item(parent_popup: PopupMenu, label: String) -> void:
parent_popup.add_item(label)
func _add_popup(parent_popup, path, label):
func _add_popup(parent_popup: PopupMenu, path: String, label: String) -> PopupMenu:
if parent_popup.has_node(label):
var popup_node = parent_popup.get_node(label)
var popup_menu = popup_node as PopupMenu
assert(popup_menu)
return popup_menu
var popup_node := parent_popup.get_node(label)
var new_popup_menu: PopupMenu = popup_node
assert(new_popup_menu)
return new_popup_menu
var popup_menu = PopupMenu.new()
var popup_menu := PopupMenu.new()
popup_menu.name = label
popup_menu.hide_on_checkable_item_selection = false
@@ -49,11 +47,11 @@ func _add_popup(parent_popup, path, label):
return popup_menu
func _on_item_pressed(item_index, popup_menu, path):
var item_path = path + popup_menu.get_item_text(item_index)
func _on_item_pressed(item_index: int, popup_menu: PopupMenu, path: String) -> void:
var item_path := path + popup_menu.get_item_text(item_index)
if popup_menu.is_item_checkable(item_index):
var checked = not popup_menu.is_item_checked(item_index)
var checked := not popup_menu.is_item_checked(item_index)
popup_menu.set_item_checked(item_index, checked)
option_changed.emit(item_path, checked)
else:

View File

@@ -1,33 +1,31 @@
extends RigidBody3D
var _dir := 1.0
var _distance := 10.0
var _walk_spd := 100.0
var _acceleration := 22.0
var _is_on_floor := false
@onready var _forward = -transform.basis.z
@onready var _collision_shape = $CollisionShape
@onready var _material = $CollisionShape/MeshInstance3D.get_active_material(0)
@onready var _forward := -transform.basis.z
@onready var _collision_shape := $CollisionShape
@onready var _material: StandardMaterial3D = $CollisionShape/MeshInstance3D.get_active_material(0)
var _dir = 1.0
var _distance = 10.0
var _walk_spd = 100.0
var _acceleration = 22.0
var _is_on_floor = false
func _ready():
func _ready() -> void:
if not _material:
_material = StandardMaterial3D.new()
$CollisionShape/MeshInstance3D.set_surface_override_material(0, _material)
func _process(_delta):
func _process(_delta: float) -> void:
if _is_on_floor:
_material.albedo_color = Color.WHITE
else:
_material.albedo_color = Color.RED
func _integrate_forces(state):
var delta = state.step
var velocity = (_forward * _dir * _walk_spd * delta) + (state.linear_velocity * Vector3.UP)
func _integrate_forces(state: PhysicsDirectBodyState3D) -> void:
var delta := state.step
var velocity := (_forward * _dir * _walk_spd * delta) + (state.linear_velocity * Vector3.UP)
state.linear_velocity = state.linear_velocity.move_toward(velocity, _acceleration * delta)
if state.transform.origin.z < -_distance:
@@ -38,13 +36,13 @@ func _integrate_forces(state):
ground_check()
func ground_check():
var space_state = get_world_3d().direct_space_state
var shape = PhysicsShapeQueryParameters3D.new()
func ground_check() -> void:
var space_state := get_world_3d().direct_space_state
var shape := PhysicsShapeQueryParameters3D.new()
shape.transform = _collision_shape.global_transform
shape.shape_rid = _collision_shape.shape.get_rid()
shape.collision_mask = 2
var result = space_state.get_rest_info(shape)
var result := space_state.get_rest_info(shape)
if result:
_is_on_floor = true
else:

View File

@@ -1,53 +1,49 @@
extends RigidBody3D
const MOUSE_DELTA_COEFFICIENT = 0.01
const CAMERA_DISTANCE_COEFFICIENT = 0.2
var _picked = false
var _last_mouse_pos = Vector2.ZERO
var _mouse_pos = Vector2.ZERO
var _picked := false
var _last_mouse_pos := Vector2.ZERO
var _mouse_pos := Vector2.ZERO
func _ready():
func _ready() -> void:
input_ray_pickable = true
func _input(event):
var mouse_event = event as InputEventMouseButton
if mouse_event and not mouse_event.pressed:
if mouse_event.button_index == MOUSE_BUTTON_LEFT:
func _input(event: InputEvent) -> void:
if event is InputEventMouseButton:
if not event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
_picked = false
var mouse_motion = event as InputEventMouseMotion
if mouse_motion:
_mouse_pos = mouse_motion.position
if event is InputEventMouseMotion:
_mouse_pos = event.position
func _input_event(_viewport, event, _click_pos, _click_normal, _shape_idx):
var mouse_event = event as InputEventMouseButton
if mouse_event and mouse_event.pressed:
if mouse_event.button_index == MOUSE_BUTTON_LEFT:
func _input_event(_camera: Camera3D, event: InputEvent, _position: Vector3, _normal: Vector3, _shape_idx: int) -> void:
if event is InputEventMouseButton:
if event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
_picked = true
_mouse_pos = mouse_event.position
_mouse_pos = event.position
_last_mouse_pos = _mouse_pos
func _physics_process(delta):
func _physics_process(delta: float) -> void:
if _picked:
var mouse_delta = _mouse_pos - _last_mouse_pos
var mouse_delta := _mouse_pos - _last_mouse_pos
var world_delta := Vector3.ZERO
world_delta.x = mouse_delta.x * MOUSE_DELTA_COEFFICIENT
world_delta.y = -mouse_delta.y * MOUSE_DELTA_COEFFICIENT
var camera = get_viewport().get_camera_3d()
var camera := get_viewport().get_camera_3d()
if camera:
var camera_basis = camera.global_transform.basis
var camera_basis := camera.global_transform.basis
world_delta = camera_basis * world_delta
var camera_dist = camera.global_transform.origin.distance_to(global_transform.origin)
var fov_coefficient = camera.fov / 70.0
var camera_dist := camera.global_transform.origin.distance_to(global_transform.origin)
const DEFAULT_CAMERA_FOV = 75.0
var fov_coefficient := camera.fov / DEFAULT_CAMERA_FOV
world_delta *= CAMERA_DISTANCE_COEFFICIENT * camera_dist * fov_coefficient
if freeze:

View File

@@ -1,14 +1,12 @@
extends ScrollContainer
@export var auto_scroll := false
@export var auto_scroll: bool = false
func _process(_delta):
func _process(_delta: float) -> void:
if auto_scroll:
var scrollbar = get_v_scroll_bar()
var scrollbar := get_v_scroll_bar()
scrollbar.value = scrollbar.max_value
func _on_check_box_scroll_toggled(button_pressed):
func _on_check_box_scroll_toggled(button_pressed: bool) -> void:
auto_scroll = button_pressed

View File

@@ -1,20 +1,20 @@
extends Node
enum PhysicsEngine {
GODOT_PHYSICS,
OTHER,
}
var _engine = PhysicsEngine.OTHER
var _engine := PhysicsEngine.OTHER
func _enter_tree():
func _enter_tree() -> void:
process_mode = Node.PROCESS_MODE_ALWAYS
# Always enable visible collision shapes on startup
# (same as the Debug > Visible Collision Shapes option).
get_tree().debug_collisions_hint = true
var engine_string = ProjectSettings.get_setting("physics/3d/physics_engine")
var engine_string: String = ProjectSettings.get_setting("physics/3d/physics_engine")
match engine_string:
"DEFAULT":
_engine = PhysicsEngine.GODOT_PHYSICS
@@ -24,7 +24,7 @@ func _enter_tree():
_engine = PhysicsEngine.OTHER
func _process(_delta):
func _process(_delta: float) -> void:
if Input.is_action_just_pressed(&"toggle_full_screen"):
if DisplayServer.window_get_mode() == DisplayServer.WINDOW_MODE_FULLSCREEN:
DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED)
@@ -32,7 +32,7 @@ func _process(_delta):
DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN)
if Input.is_action_just_pressed(&"toggle_debug_collision"):
var debug_collision_enabled = not _is_debug_collision_enabled()
var debug_collision_enabled := not _is_debug_collision_enabled()
_set_debug_collision_enabled(debug_collision_enabled)
if debug_collision_enabled:
Log.print_log("Debug Collision ON")
@@ -46,13 +46,13 @@ func _process(_delta):
get_tree().quit()
func get_physics_engine():
func get_physics_engine() -> PhysicsEngine:
return _engine
func _set_debug_collision_enabled(enabled):
func _set_debug_collision_enabled(enabled: bool) -> void:
get_tree().debug_collisions_hint = enabled
func _is_debug_collision_enabled():
func _is_debug_collision_enabled() -> bool:
return get_tree().debug_collisions_hint

View File

@@ -1,20 +1,18 @@
extends Node
enum LogType {
LOG,
ERROR,
}
signal entry_logged(message, type)
signal entry_logged(message: String, type: LogType)
func print_log(message):
func print_log(message: String) -> void:
print(message)
entry_logged.emit(message, LogType.LOG)
func print_error(message):
func print_error(message: String) -> void:
push_error(message)
printerr(message)
entry_logged.emit(message, LogType.ERROR)