Core engine
The building blocks of a game's code: what runs each tick, what changes a rule, what reacts, what waits, where state lives and how to find things on the map. Data (units, abilities, items) says what a game has. These say what data cannot.
| Block | What it is for | Defined with | Listed in |
|---|---|---|---|
| Querying the world | Find units and everything else by place and condition | world.query, an effect's where | – |
| Systems | Code that runs every tick, or every few | System | Rules.systems |
| Hooks | Change or cancel a rule as it happens | defineHook | Rules.hooks |
| Events and listeners | React after something happened; areas | defineEvent, defineListener, defineArea | Rules.events, Rules.listeners, Rules.areas |
| Timers and sequences | Do something later, again and again, or step by step | defineTimer, defineSequence | Rules.timers, Rules.sequences |
| Components and world state | State of your own on an entity, a player or the game | defineComponent | Rules.components |
| Orders and commands | What units do, and what players may tell them | OrderBehavior, defineCommand | Rules.orders |
| Checks, logs and errors | Typos caught, rules checked, logs, a policy for code that throws | defineIds, checkRules | Rules.onError, Rules.log |
| Saving and loading | Saves, versions, and what a save keeps | ||
| Mods and content packs | Changing a game from outside it | ||
| Score and stats | Kills, losses and what a game counts | ||
| Testing your rules | Running a world in a test, balance runs, determinism |
Rules and their look
A game mode has two parts: the Rules (the data, and the code on these pages) and the GameView (how it looks). For a walk through a whole game, see Making a game.
| Part | What it is | Runs | Imports |
|---|---|---|---|
Rules | Units, abilities, buffs, items, upgrades, and code (effects, orders, systems, victory) | Wherever the simulation runs: page, worker, server | @v12-engine/engine |
GameView | Models, effect colours, sounds and icons, keyed by the rules' ids | Only on a player's screen | @v12-engine/engine, @v12-engine/client |
How a world runs
- Fixed ticks: 30 a second (
world.dteach). Every random number comes from the seededworld.rng; never useMath.randomor the clock. UseexactMath(sin,cos,atan,atan2,hypot) for maths that decides anything. - Commands (
world.issue) are the only way players change the world. They apply at the start of the next tick. - Orders (
OrderBehaviorinRules.orders) are the only way units act. - Systems run in phases:
commands,decide,move,resolve,cleanup(PHASES). Timers fire at the start of their tick. - Hooks run in the middle of a rule, as it is decided.
- Effects are data (
EffectDef); types of your own go inRules.effects. - Events go out at the end of the tick, to listeners and to the screens.
Rules of thumb
- Keep state in the world: components (
world.state(key),player.state(key),entity.get(key)), so it saves and works with many games on one server. See Components. - The view has no say in the rules: anything a player does is a command.
- Keep state plain data: numbers, text, lists, plain objects, entities by id. A
Map,Setor entity makes the save throw, naming the field. - Look at it running: the debug panel's Entities, Inspect and Event log.
- Run the debug checks in tests:
setDebugChecks(true)(@v12-engine/engine/debug).
The library
@v12-engine/engine: the world, definitions, rule helpers, maps, sessions, computer players. Also@v12-engine/engine/serverand@v12-engine/engine/testing.@v12-engine/client:launch,loadGame, the client,GameView. Also/app(scenes),/kit(UI components),/render,/vite;@v12-engine/editor.- Anything not exported may change.
npm run lintchecks that rules import nothing that draws, and nothing reaches into a package by path.
Examples
The campaign starter (npm create @v12-engine my-game -- --starter campaign) is a whole mode on the public API: its own resource, units, hero, abilities, items, missions, a wave system of timers and events, objectiveVictory, and a view. The melee starter is the example's whole RTS. Melee lives in apps/example/src/games/melee (rules) and .../melee/view (view). packages/engine/tests/hello-mode.test.ts is the smallest mode there is.