↓ Skip to main content

Teaching AI Agents to Respect Godot's Type System

Author
David Watson
Terraforming enterprise clouds by day, terraforming worlds by night. With an unhealthy urge to over-engineer both.

Right after I finished breaking up my root simulation script, a tiny crash inside _draw() annoyed me enough to audit the rest of the codebase: a dynamic .get() call had returned null instead of a boolean, and GDScript only complained halfway through a playtest.

If you let an LLM write GDScript without explicit rules, it almost always falls back on two habits: untyped Dictionary blobs for state, and dynamic .get() / .call() lookups between nodes. While you’re moving fast, that feels convenient. A few days later, typos in string keys fail silently and rename refactors miss dynamic calls completely.

During Round 2 of my cleanup, I cracked down on both—enforcing static typing and explicit enum state machines across the project.


Replacing Dictionary Blobs with Typed Classes and State Machines
#

The worst offender was the building slot model. Every slot lived as an untyped Dictionary mixing string keys ("status", "stalled_reason", "building", "build_progress", "owner") with stream-reservation keys. One typo in "STALLED" or "COMPUTE" and a build would silently hang with no parse error.

Swapping that dictionary out for a small statically typed RefCounted class (StructureSlot, ~170 lines) with an explicit enum state machine wiped out that whole class of bugs immediately:

# BEFORE: Untyped Dictionary + stringly-typed status/reason keys
var slot: Dictionary = {
	"building": "REACTOR",
	"status": "STALLED",
	"stalled_reason": "COMPUTE",
	"build_progress": 0.45,
	"powered": true,
}
if String(slot.get("status", "")) == "STALLED" and String(slot.get("stalled_reason", "")) == "COMPUTE":
	...

# AFTER: Typed RefCounted model with explicit FSM
class_name StructureSlot
extends RefCounted

enum State { EMPTY, AWAITING_SUPPLY, CONSTRUCTING, STALLED_COMPUTE, ACTIVE, RECYCLING }
enum Category { SURFACE, ORBITAL }

var index: int = 0
var category: Category = Category.SURFACE
var structure_id: String = ""
var state: State = State.EMPTY
var build_progress: float = 0.0
var powered: bool = true

Once every valid state is an enum, impossible flag combinations (like is_building = true and is_active = true) simply can’t happen. I rolled the same pattern out across building slots, remote hacking/capture, territory control, research, incoming hazards, and UI modes:

Explicit finite state machine transition diagrams for the StructureSlot lifecycle and Remote Hacking FSM

Ripping Out Dynamic Lookups Between UI and Simulation
#

Early on, my UI panels held untyped Node references to the simulation and poked at it with dynamic .get(), .set(), and .call() lookups.

Counting those calls across scripts/ turned into a handy health check: they dropped from 459 before the refactor, down to 113 after the domain ownership pass, and finally to 3 after Round 2 cleanup (-99.3%). Now UI panels hold statically typed references and call real methods, so renamed properties fail at parse time instead of mid-match.


Keeping the AI Sticking to Static Types
#

I didn’t even need to change Godot’s default [debug] warning settings in project.godot. Three simple workflow habits did the work:

  • One clear rule in AGENTS.md: “Explicitly type all variables, function arguments, and return types; avoid untyped Dictionaries for domain state.”
  • Headless compile gate: Running godot --headless --quit --check-only on every .tscn before committing catches parse and type errors in a couple of seconds.
  • Local standards review: My local bin/ollama-gen --review-standards pass flags untyped declarations, dynamic .call() fallbacks, and raw state dictionaries before a PR is opened—using zero cloud tokens (covered in Beyond Basic MCP).

By the Numbers
#

99.3%

Fewer Dynamic Lookups

Root sim.get() / sim.set() / sim.call() calls dropped from 459 → 113 → 3.

166 LOC

Typed Slot Model

Replaced untyped 10-key Dictionary blobs with a single RefCounted FSM class.

5+

Explicit Enum FSMs

Eliminated scattered boolean/string flags across slots, combat, research, hazards, and UI.

Next Up
#

Once the codebase was split into 40 cleanly typed scripts, I wanted a way to actually see the structure—and verify the refactor visually. That led me to build a zero-dependency interactive architecture map in Python.