By Day 3 of my five-day strategy game sprint, adding a small gameplay tweak had started feeling strangely sluggish. Every ticket seemed to touch the same giant root simulation script—which had quietly blown past 2,700 lines inside a 27-script project.
When you ship features quickly with coding agents, the easiest thing for a model to do is tack one more method, one more dictionary, and one more flag onto the root node. Keeping everything in one file is simpler for the model to track while implementing a ticket, right up until the file gets so big that every change fights the weight of it.
Once I had cleaned up my test suite, I used improve-codebase-architecture (paired with Godot’s component-system and state-machine patterns from Outer Loop, Inner Loop) to break that monolith apart across two refactor passes.
Signs the Root Script Had Gone Too Far#
Three things made it obvious I had to stop adding features and split the script up:
- Pass-through wrappers everywhere: UI panels were calling methods on the root script that did nothing except forward the call to an internal array or helper.
- Duplicated UI state: Panels were keeping their own cached copies of simulation state, leading to annoying sync bugs whenever a mechanic changed.
- Copy-pasted type checks: Almost every logistics, combat, and power function branched on whether a target was a planet or a station, followed by defensive dynamic method checks.
Splitting the Refactor Into Two Passes#
Asking an AI to “refactor this 2,700-line script into clean modules” in one shot is a fast way to end up with broken scenes and half-wired signals. Splitting the work into two passes kept every step small, reviewable, and green under headless tests:

Round 1: Pull Subsystems Out#
In Round 1, I kept the existing methods on the root script as thin wrappers so callers wouldn’t all break at once, and pulled subsystems out one ticket at a time. Across both rounds, 15 focused modules (roughly 85 to 900 lines each) took ownership of their own domains:
- Simulation & Domain Modules: Logistics, Hazard Defence, System Generation, Combat, Power Network, Shared Node Base Class, Typed Slot State Machine, Camera Control, Research, and Window Prediction.
- Rendering & UI Modules: World Map Renderer, Target Panel, Slot Panel, Stream Renderer, and UI Style Helper.
Sure enough, the very next day, new power-network and rival-AI features pushed the root script back up by ~60 lines. That’s why Round 2 mattered.
Round 2: Delete the Wrappers & Dead Prototypes#
Once UI panels and subsystems talked directly to each other through typed references, the old root wrappers had no reason to exist:
- Deleted 110 pass-through wrapper functions from the root script in a single cleanup pass (
0lines added), bringing it cleanly under 1,000 lines. - Deleted 9 dead prototype files (prototype scenes, scripts, tests, and metadata) so abandoned experiments wouldn’t pollute future AI searches.
Collapsing Repeated Branching with One Base Class#
One of the cleanest wins in Round 2 was adding a small shared base class (NetworkNode, ~220 lines) for both planets and stations. Before that, every coordinator repeated the same branching and dynamic fallback:
# BEFORE: Every coordinator branched on concrete node types + dynamic .get()/.call()
func can_dispatch_supply(target: Node2D, faction_id: int) -> bool:
var is_active: bool = false
if target is PlanetNode:
var p := target as PlanetNode
is_active = p != central_star and (p.is_colonized or p.owner_faction == faction_id)
elif target is StationNode:
var s := target as StationNode
is_active = s.has_station and s.owner_faction == faction_id
if not is_active:
return false
var eligible: bool = not target.has_method("is_belt_eligible") or bool(target.call("is_belt_eligible"))
return eligible
# AFTER: Polymorphic base contract (NetworkNode) shared by PlanetNode & StationNode
func can_dispatch_supply(target: Node2D, faction_id: int) -> bool:
var host := target as NetworkNode
if host == null or host == central_star:
return false
return host.can_receive_supply(faction_id) and host.is_belt_eligible()Replacing repeated type-checks with one small base class shrinks both the code and the context the AI has to read next time I add another node type.
By the Numbers#
-65.0%
Root Script Shrink
15
Focused Modules
110
Wrappers Deleted
Next Up#
With the root script cut by 65%, the next glaring mess was the hundreds of untyped dictionaries and dynamic .get() / .call() lookups still scattered across those 40 scripts. That’s what I tackled next.
