Style Guide
Usagi’s examples and this book (for the most part) uses the following style guide for Lua code:
- 2 spaces for indentation.
snake_casefor locals, function names, table fields, and helper module names (e.g.enemy.lua,local fresh_state). SCREAMING_SNAKE_CASEfor compile-time-ish constants (file-scopelocal TICK = 0.12,local MAX_BULLETS = 12, the engine’sgfx.COLOR_*table). Distinguishes “tunable knob” from “runtime variable.”- Engine API is lowercase (
gfx,input,sfx,music,usagi). It’s declared inmeta/usagi.luaso the LSP treats it as predefined; reads in user code don’t triplowercase-global. - Globals are
Capitalized. This includes the canonical game-state container (State = { ... }set inside_init) and module imports kept as globals (Player = require("player")). The capitalization signals “intentional global, lives across reloads”; anything lowercase at file scope is treated bylowercase-globalas an accident (forgot alocal). - Why
Stateis a global: live reload re-execs the chunk on every saved edit. Alocal Stateat module scope would get re-bound to a fresh table every save and obliterate the running game. SettingStatein_init(and only in_init, which only runs at startup and on F5) lets the table outlive reloads. - If you have a global need that isn’t
State, the convention scales: capitalize and document it. Module-bound require results (Enemy,Bullet) are the common second case.
This pattern (engine-API lowercase / game-state capitalized / locals snake_case)
is the same one shipped in .luarc.json and the usagi init template, and is
what every example under examples/ follows.