Skip to content

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.

BlockWhat it is forDefined withListed in
Querying the worldFind units and everything else by place and conditionworld.query, an effect's where–
SystemsCode that runs every tick, or every fewSystemRules.systems
HooksChange or cancel a rule as it happensdefineHookRules.hooks
Events and listenersReact after something happened; areasdefineEvent, defineListener, defineAreaRules.events, Rules.listeners, Rules.areas
Timers and sequencesDo something later, again and again, or step by stepdefineTimer, defineSequenceRules.timers, Rules.sequences
Components and world stateState of your own on an entity, a player or the gamedefineComponentRules.components
Orders and commandsWhat units do, and what players may tell themOrderBehavior, defineCommandRules.orders
Checks, logs and errorsTypos caught, rules checked, logs, a policy for code that throwsdefineIds, checkRulesRules.onError, Rules.log
Saving and loadingSaves, versions, and what a save keeps
Mods and content packsChanging a game from outside it
Score and statsKills, losses and what a game counts
Testing your rulesRunning 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.

PartWhat it isRunsImports
RulesUnits, abilities, buffs, items, upgrades, and code (effects, orders, systems, victory)Wherever the simulation runs: page, worker, server@v12-engine/engine
GameViewModels, effect colours, sounds and icons, keyed by the rules' idsOnly on a player's screen@v12-engine/engine, @v12-engine/client

How a world runs ​

  • Fixed ticks: 30 a second (world.dt each). Every random number comes from the seeded world.rng; never use Math.random or the clock. Use exactMath (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 (OrderBehavior in Rules.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 in Rules.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, Set or 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/server and @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 lint checks 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.