Skip to content

Declared, or in code ​

Anything a game can say as data, it can also decide in code. Start with the field; use code for the one case the field can't cover.

ts
skeleton: {
	height: 1.4,
	shapes: [/* … */],
	// a skeleton a cannon killed blows apart harder
	onDeath: (_skeleton, death) => (death.killerType === 'cannonTower' ? { debris: bones(1, 1.5), strength: 2.4 } : 'debris')
}

Code takes one of three shapes:

  • A function in the field's place, asked each time: onDeath next to debris, modelFor next to model.
  • A hook the engine runs while it decides: targetScore, canPlace, beforeDamage.
  • An API to change it while the game runs: world.sides.setTeam, world.areas.add, client.theme.set, client.looks.set.

In the rules ​

DeclaredIn code
an ability's range, castTimerangeOf, castTimeOf; the hooks canCast, canTarget
attack statsthe hooks canAttack, beforeAttack; RuleSettings.combat.splash
a buff's statschange(world, carrier, instance), the stats hook
teams on the mapworld.sides.setRelation, world.sides.setTeam, world.sides.shareVision
areasworld.areas.add, world.areas.remove
onError: 'end'onError: (error, where) => (where.mod ? 'disable' : 'end')
saves: { version, migrate }migrate: (save, from, to) => …, onMismatch: (save, hash) => …
log: { level: 'warn' }world.log.level = 'debug', world.log.to(sink)
ts
// no building on the road: a rule no field says
const keepTheRoadFree = defineHook({
	hook: 'canPlace',
	id: 'keepTheRoadFree',
	run(world, event) {
		if (world.map.path('road').contains(event.site)) event.cancelled = true
	}
})

Four ways to change any piece of the interface ​

From least work to most control:

  1. Declared: a field of the view: words, colours, what's hidden.
  2. In code: a function, hook or API.
  3. A component (Solid) in the client's place, given what the client's own gets, so it can wrap it.
  4. An element of your own: mount(element, …) with any HTML or framework, returning what cleans up. mount wins over component.
tsx
// 1. declared
outcome: { victory: 'The road holds', defeat: 'The gate has fallen' }

// 2. in code: buttons of the game's own, and the client's left out
outcome: { buttons: [{ label: 'Next mission', run: (client) => goNext(client), when: 'victory' }], standard: false }

// 3. a component, given what the client's own is given (`OutcomeScreen` to wrap)
outcome: { component: (props) => <MyOutcome outcome={props.outcome} restart={props.restart} /> }

// 4. an element of your own, with any framework
outcome: {
	mount(element, client, outcome) {
		element.innerHTML = `<h1>${outcome === 'victory' ? 'Won' : 'Lost'}</h1>`
		return () => (element.innerHTML = '')
	}
}
WayA HUD part
Declaredhud: { hide: ['minimap'] }
In codeclient.hud.hide('minimap'); back with client.hud.hide('minimap', false)
A componenthud: { widgets: [{ id: 'card', component: MyCard }] }, the client's card as children
An elementhud: { widgets: [{ id: 'card', mount: (element, client) => { /* draw */ return () => {} } }] }

The same four ways work for loading and error screens, tooltips, toasts, dialogs, menus, the lobby, the pause menu, keys and editor panels. Screens that do something (saves, a campaign map, the lobby, replays) split into a controller and a view: your view gets the same controller, so you never rewrite what the screen does. See The interface.