Skip to content

Lesson 9 of 9 · 24m

Testing, Debugging, and Operating Embedded Lua

How to build a feedback loop for scripts: isolated tests, useful errors, tracing hooks, versioning, hot reload, and production visibility.

Embedded Lua often fails operationally because it lives between two owners. Engine developers call it content. Content developers call it engine behavior. Production calls it a crash at 03:00. The fix is to treat scripts as software with their own test and observability loop.

Test scripts against a fake host

Do not boot the entire game, database, or proxy for every test. Implement the host API against a small fixture:

local world = fake_world()
world:spawn("rat", { x = 0, y = 0 })
local enemy = load_enemy_brain("scripts/rat.lua")
enemy:on_tick(0.016)
assert(world.calls[1].name == "move_toward")

This forces the API boundary to become explicit. If tests need a live renderer, the script is probably reaching too deeply into engine internals. Busted and plain assertion harnesses both work; the harness matters less than isolation.

Make errors attributable

When a script fails, capture:

script id/path, host version, event or entry point
sandbox owner/mod name, entity or request id, Lua stack traceback

debug.traceback is the baseline:

local ok, err = xpcall(chunk, function(msg) return debug.traceback(msg, 2) end)

Without that context, 'attempt to index nil' tells you nothing in a system running thousands of content items.

Hot reload is a migration problem

Reloading source is easy; reconciling live state is hard. If a script stores cooldowns, targets, or progress in Lua tables, replacing the function does not migrate those tables. Choose one model:

Choose one model: stateless scripts can reload freely; host-owned state makes scripts replaceable; Lua-owned state needs on_reload(old, new) or explicit persistence.

Implicit state transfer is how rare production-only corruption is born.

Instrument the boundary

script executions/errors by entry point and script id; execution time p50/p95/p99
instruction quota terminations; Lua memory; host API calls per script tick/request

Averages hide the frame spike. In games, p99 per frame matters. In proxies, tail latency per request matters. In batch jobs, total runtime may be acceptable until memory blows up.

Debugging hooks are powerful and expensive

Lua's debug library can trace calls, lines, and returns. It is useful in development and risky in production because hooks alter performance and can expose internals. Build a mode for it, not an always-on side effect.

Field notes

  • Keep a replayable minimal input that triggers a script bug; embedded failures otherwise evaporate into host state.
  • Version scripts and host APIs together in artifact metadata. 'Latest' is not a rollback plan.
  • Never hot reload arbitrary customer scripts without revalidating sandbox constraints.
  • The operational goal is not zero script errors; it is errors that are isolated, attributed, and understandable without a core dump.

Was this lesson helpful?

Sign in to rate