Mods and content packs
Change a game without touching its code: a mod in code (a new hero, other numbers, a rule), a content pack as JSON, the editor's data file, definitions changed while the game runs, and hot reload while you develop.
Quick start
export const goldRush: ModDef = {
id: 'goldRush',
version: '1.0.0',
requires: ['melee@^1'],
apply(game) {
game.settings({ supply: { max: 150 } })
game.units.override('peasant', (def) => ({ ...def, cost: { gold: 50 } }))
game.hooks.add(doubleBounty) // the same id as the engine's bounty: in its place
game.addSystem(goldTrickle)
}
}
const game = composeGame(melee, [goldRush]) // a Rules like any other, with the id melee+goldRushMelee is now played with cheaper peasants, a supply cap of 150, double bounties and a gold trickle. The example's Gold rush menu entry is this mod (apps/example/src/games/goldRush, tested by tests/mods.test.ts).
Common tasks
Add, override or remove things
Units, abilities, buffs, items, upgrades, stats, resources, effects, orders, commands, hooks, races, listeners, timers and components can each be added, overridden or taken out (game.units.add, game.units.override, game.races.add, game.components.add…). Engine hooks and systems can be removed, and single settings (day, currency, victory…) set.
Ship content as JSON: a content pack
{
"$schema": "../../node_modules/@v12-engine/engine/schemas/pack.schema.json",
"id": "gnomes",
"version": "1.0.0",
"requires": ["melee"],
"races": [{ "id": "gnome", "name": "Gnome", "start": { "hall": "townhall", "workers": { "type": "peasant", "count": 4 } } }],
"units": [{ "id": "tinker", "name": "Tinker", "kind": "unit", "radius": 0.4, "stats": { "health": 150 }, "sight": 9, "abilities": ["zap"] }],
"abilities": [{ "id": "zap", "name": "Zap", "target": "unit", "manaCost": 10, "range": 5, "effects": [{ "type": "damage", "amount": 40 }] }]
}createGame({ ...rules, contentPacks: ['/packs/gnomes/pack.json'] }) // in the page
mergePackFiles(game, ['packs/gnomes/pack.json']) // on a server (@v12-engine/engine/server)Change a definition while the game runs
world.redefine('units', 'footman', { stats: { health: 500 } }) // existing footmen take it at once; saved
world.redefine('units', 'champion', { copyOf: 'footman', name: 'Champion' }) // a new type copied from anotherEvery player's screen is told (a redefined event), and definitionChanged fires. The world gets a copy; the module and other worlds are unchanged.
Lay the editor's data file over your code
export const melee: Rules = layData({ id: 'melee', /* … */ editable: { units: 'all', races: 'all' } }, dataFile)The Data panel writes data.json; layData applies only the fields your game lets be changed (editable). An entry with copyOf is a new definition based on another (basedOn); the client draws it as the original where the view has no look of its own.
Hot reload a game while you develop
// games/mygame/index.ts
if (import.meta.hot) import.meta.hot.accept((module) => hotRules(import.meta.hot, myGame, module?.myGame))
// games/mygame/view/index.ts
if (import.meta.hot) import.meta.hot.accept((module) => hotView(import.meta.hot, 'mygame', myView, module?.myView))
// mods/goldRush.ts
if (import.meta.hot) import.meta.hot.accept((module) => hotMod(import.meta.hot, goldRush, module?.goldRush))Save a unit's numbers, an ability's code, data.json or looks.json, and every game and editor in the page takes it without a reload. Models and textures in public reload by themselves.
Hot reload a game in a worker
// game.worker.ts
serveWorld(myGame, {
onStart: (world) => startMyGame(world),
reload: (version) => import(/* @vite-ignore */ `./index.ts?t=${version}`).then((module) => module.myGame)
})Rebuild your own caches after a reload
export const myGame: Rules = { ...game, onReload: (world) => rebuildCache(world) }
world.events.on(gameReloaded, () => rebuildCache(world))Editor
- Open the Data panel and change a unit, ability, item or race; Keep writes
data.json. - Copy as new makes a new definition based on another (
copyOf). - To keep changes in a content pack instead, set
editor.project.dataPack = 'gnomes': the panel writespacks/gnomes/pack.json. - The debug command
debug.redefine(redefineCommand) changes one editable field in a running game, for players allowed the debug tools.
Reference
Mods (ModDef)
requiresnames the game and other mods:id,id@1.2.3orid@^1.2. Mods apply after what they require.- Two mods that change the same thing, neither requiring the other, are a conflict:
composeGamethrows, listing each, before anyone plays. registry.hashcovers the game, its mods and their code. A game that declaressavesloads saves made with a newer mod version (Saving and loading).
Content packs (JSON)
A pack (packs/<id>/pack.json, PackFile) has id, version, requires, then units, abilities, buffs, items, upgrades, races (data form: startingBase: { hall, workers }), data (laid over like data.json), settings (RuleSettingsInit) and looks (as looks.json). A definition of an existing id replaces it. A pack that still says rules for its knobs is refused with a message to say settings. Which data layer wins has the order packs are laid in.
- In the page:
prepareGamefetches the packs (the loading screen counts them), checks them, makes them mods (packFromJson) and composes them in order (composeGame,mergePacks): the game's id becomesmelee+gnomes. Looks are laid over the view. - On a server:
mergePackFiles(orreadPacksfor the mods alone). A player whose packs differ is told which the host has as they join. - Same bytes, same game: each pack's hash is part of the game's hash, so saves and lobbies tell versions apart (
SaveRules.onMismatch). Packs that change the same thing conflict, as mods do. - Schema:
@v12-engine/engine/schemas/pack.schema.json(made bynode scripts/packSchema.mjs) gives an editor completion, and packs are checked against it on load (checkPackSchema). Code (a unit'sonDeath) cannot be in a pack; that stays a mod's.
Data and redefinitions
| Call | What it does |
|---|---|
layData(game, dataFile) | The game with the file over its definitions, editable fields only; the rest is logged |
editableFields(game, kind, def) | Which fields may be changed |
world.redefine(kind, id, values) | Change or copy a definition live; saved; sent to every screen |
copyDefinition(registry, kind, from, id) | Copy one in code |
On a server's game, each screen keeps its own copy of definitions as they were (a tooltip's cost).
What hot reload takes
hotRules makes the module the new one in place; reloadGame(world, game) puts its unit types, abilities, buffs, items, upgrades, races, effect and order code, and rules into each world (what world.redefine changed stays on top; units keep their lost health), then fires core.gameReloaded. hotView updates looks, pictures and words. If systems, hooks, components or resources changed, the page reloads (hot.invalidate) and says which. Also hotReload(game, { rules, view }), hotTarget. Rules.onReload(world) and GameView.on (gameReloaded) let you rebuild what you derived. What reloads has a table.
- A game composed with mods is recomposed (
recompose) when either is saved. - A worker game reloads through
WorkerSession.reloadGameand itsreloadoption; without it, it keeps its first version. A game over a wire is not reached. - Melee and the tower defense reload; the lanes and campaign keep their version until the page reloads.
- Models and textures saved in
public(models/…,textures/…) reload through the editor's plugin (v12:asset-files,hotFiles): a.glbis loaded beside the old and swapped in; textures get a?v=(Assets.changed). Not reloaded: particle and screen textures already made, a clip source file alone, and the shared art package innode_modules. Without the dev server:hotFiles({ models: ['characters/knight.glb'], textures: [] }).