Lesson 2 of 9 · 22m
GDScript Day-One Discipline
Use typed GDScript, explicit node access, exports, and small functions to make scripts readable under pressure instead of merely executable.
GDScript is easy to start and just as easy to write badly. Because it looks forgiving, beginners rely on dynamic typing, stringly node paths, and five-hundred-line scripts. Six months later every refactor is an archaeological dig. The language can be strict enough for production if you choose that discipline on day one.
Types are documentation the compiler reads
Untyped GDScript works until a resource returns null or the wrong node type silently stores a value. Prefer explicit types:
extends CharacterBody2D
@export var speed: float = 220.0
@export var max_health: int = 5
var health: int = max_health
func damage(amount: int) -> void:
health = maxi(health - amount, 0)The annotation is not ceremony. It enables autocomplete, catches invalid calls, and tells the next reader whether a function returns state or mutates it.
Resolve local dependencies at ready time
@onready initializes after the node is ready, when child nodes can safely be resolved:
@onready var sprite: Sprite2D = $Sprite2D
@onready var collision_shape: CollisionShape2D = $CollisionShape2D
@onready var animation_player: AnimationPlayer = $AnimationPlayerThis is appropriate for nodes owned by the same scene. It is not a license to path across the game. If a script needs ../../UI/HUD/HealthLabel, the scene boundary is already leaking.
Export tuning values, not architecture
@export exposes data in the inspector so designers can tune behavior without editing code:
@export var speed: float = 220.0
@export_range(0.0, 1.0) var acceleration_smoothing: float = 0.2
@export var dash_sound: AudioStream
@export var loot_table: ResourceExport assets and tuning values. Do not export references to runtime nodes unless manual wiring is truly the design; those references break when scenes are duplicated, spawned, or test-driven in isolation.
Prefer plain functions and loud failures
A new hire does not need a five-level inheritance tree. They need small functions whose names describe game rules:
func _physics_process(delta: float) -> void:
update_movement(delta)
move_and_slide()
func update_movement(delta: float) -> void:
var direction := Input.get_vector("move_left", "move_right", "move_up", "move_down")
velocity = direction * speedUse push_error and early returns for broken preconditions:
if item == null:
push_error("equip called without an item")
returnField notes
:=infers a static type from the assigned expression; use it where the type is obvious.nullis not a safe default for a required dependency; fail early or design the dependency as optional.preloadresolves at compile time;loadresolves at runtime and can fail.- Typed arrays such as
Array[Enemy]are safer than passing heterogeneous arrays through gameplay code. - GDScript normally communicates errors through return values, warnings, and engine errors, not exception-first design.
Was this lesson helpful?