Checks, logs and errors
Catch mistyped ids at compile time, check the rules for mistakes before a game starts, log from the rules, and decide what happens when your code throws mid-game.
Quick start
export const UNIT = defineIds('skeleton', 'runner', 'brute')
export type TdUnit = IdOf<typeof UNIT>
world.spawn(UNIT.skeletn, owner, at) // does not compile
const errors = checkRules(rules).filter((problem) => problem.level === 'error')Common tasks
Catch typos in ids
export const UNIT = defineIds('skeleton', 'runner', 'brute')
export type TdUnit = IdOf<typeof UNIT>
const view: GameView<TdUnit, TdResource, TdMessage> = { /* a look for every unit, words for every message */ }Log from the rules
import type { World } from '@v12-engine/engine'
export const logWave = (world: World, wave: number, unit: { id: number }): void => {
world.log.warn('waves', 'nothing left to send', { wave })
world.log.debug('ai', 'retreating', { entity: unit.id })
}Decide what happens when your code throws
export const rules: Rules = { ...game, onError: (error, where) => (where.mod ? 'disable' : 'end') }Reference
Ids and checks
defineIdslets the compiler check ids; plain text still works.- A fetched view is checked against the rules (
loadGame): a warning for each unknown id, with the likely meaning.createGame({ …, checkView: 'throw' }),checkView: false,checkGame({ rules, view }),await game.check(). checkRules(rules)checks every id the rules name, that every effect has a handler, that chained effects have something before them, and that requirements don't loop: those are errors. Warnings: costed things nothing makes, upgrades researched nowhere, abilities on nothing, numbers that do nothing. Each problem hasat:{ kind, id, path }.
The log
An entry has a level (debug, info, warn, error), a category, words, fields, the game and the tick. Logging changes nothing in the world and is not saved.
| Declared | In code |
|---|---|
Rules.log: { level: 'warn' } | world.log.level = 'debug', world.log.says('debug') |
createWorld({ log }), launch({ log }), startServer({ log }) | world.log.to(sink) (returns a stop function) |
The console from info up (rootLogger) | rootConsole.stop(), rootLogger.to(sink) |
Loggers pass entries up (world, server, root). createLogRing(), logLine(entry), errorFields(error). On a screen: client.log, client.logs, the debug panel's Console; LogConsole (@v12-engine/client/hud), foldRepeats, filterLog, stackFrames, sourcesOf, LogFilter.source.
Errors (Rules.onError)
| Answer | What happens |
|---|---|
throw | The error goes to whoever stepped the world (what tests want) |
end | The game ends; world.failure says why; players see it (session.failure) |
disable | That handler, listener, system or timer is switched off (world.disabled, saved); an order is dropped, a command refused |
where says the kind of part, its id, the hook, unit, player and mod. Also world.errorPolicy, createWorld({ onError }). Defaults: createWorld throws; a server and a page end the game. On a server, one failed game is one game (GameResult.failed).