Skip to content

Make a mod ​

A mod is a change on top of a game, with an id and a version. It adds, overrides and removes definitions; composeGame makes the game with its mods applied.

ts
// in src/mods.ts
import { composeGame, defineIds, type ModDef, type Rules } from '@v12-engine/engine'

export const UNIT = defineIds('scout')

export const rules: Rules = {
	id: 'my-game',
	name: 'My game',
	units: [{ id: UNIT.scout, name: 'Scout', kind: 'unit', radius: 0.4, sight: 10, stats: { health: 100, moveSpeed: 5 } }],
	victory: null
}

/** Faster scouts, and a heavy one beside them */
export const swiftScouts: ModDef = {
	id: 'swift-scouts',
	version: '1.0.0',
	name: 'Swift scouts',
	apply(game) {
		// a field changed on what the game has
		game.units.override(UNIT.scout, (def) => ({ ...def, stats: { ...def.stats, moveSpeed: (def.stats.moveSpeed ?? 0) * 1.5 } }))
		// a type added, from the scout
		game.units.add({
			id: 'heavy-scout',
			name: 'Heavy scout',
			basedOn: UNIT.scout,
			kind: 'unit',
			radius: 0.5,
			sight: 10,
			stats: { health: 220, moveSpeed: 4 }
		})
		// a knob of the rules
		game.settings({ fog: { nightSight: 0.7 } })
	}
}

/** The game as it is played with the mod on */
export const modded = composeGame(rules, [swiftScouts])

Done: modded plays with scouts 50% faster and a heavy scout beside them. Saves record which mods made the game.

Reference ​

The builder apply gets has a collection for everything a game lists: units, abilities, buffs, items, upgrades, hooks, listeners, systems, orders, effects, timers, sequences, resources, races, packs, cellLayers (a layer of grass of the mod's own, with foliagePack)…

CallWhat it does
add(def)Adds a definition.
override(id, (def) => …)Changes one.
remove(id)Removes one.
get(id), has(id)Reads one.
game.settings({ … })Changes knobs of the rules.
addSystem, withoutSystem, withoutHook, setSystems, hooks and plain fields.

A mod that needs another names it in requires. A save opened without its mod is refused or loaded, as saves.onMismatch says. See Saves, mods, data and components.