Hooks
A hook runs at a named moment of a rule, before it is settled, and can change or cancel what happens: damage about to land, an ability about to go off, a unit about to die. The engine's own rules are hooks too, so a game can replace one. To react after something happened instead, use a listener.
Quick start
import { defineHook, earn } from '@v12-engine/engine'
// a kill pays lumber instead of gold
export const lumberBounty = defineHook({
hook: 'death',
id: 'bounty',
run(world, { entity, killer }) {
if (killer && world.areEnemies(killer.owner, entity.owner)) earn(world.player(killer.owner).resources, 'lumber', 5)
}
})Put it in Rules.hooks. It has the id of the engine's bounty handler, so it replaces the gold bounty.
Common tasks
Change damage before it lands
import { defineHook } from '@v12-engine/engine'
export const fireResist = defineHook({
hook: 'beforeDamage',
id: 'mygame.fireResist',
run(_world, event) {
if (event.target.def.id === 'salamander' && event.tags?.includes('fire')) event.amount *= 0.5
}
})Tag damage with world.damage(target, amount, source, { type, ability, tags: ['fire'] }). A death handler can set how ('exploded') for the screen.
Run before or after other handlers
priority orders handlers of the same hook: lower runs first, and the engine's own are at 0. Handlers with the same priority run in the order they were added.
Take out one of the engine's hooks
Rules.withoutHooks: ['bounty'] drops it. Give yours the same id to replace it instead (Turn an engine rule off).
Let mods hook into your own rules
const lifeLost = defineHookType<{ amount: number }>({ id: 'td.lifeLost' })
const event = world.hooks.run(lifeLost, { amount: 1 }) // a mod's handler can change amountReference
How hooks run
Handlers run in order on one event object. before… hooks can change it or cancel it. A handler with an engine id replaces that handler (coreHooks: undying, experience, loot, bounty, onHit, onCast, onKill).
The engine's hooks
| Hook | When | A handler can |
|---|---|---|
beforeDamage | Damage is about to be dealt, before armour | Change amount or type, cancelled |
afterDamage | Damage was dealt | React: life steal, on-hit effects |
beforeHeal | Health is about to be given back | Change amount, cancelled |
beforeDeath | Something is about to die | cancel() and set its health |
death | It dies, still standing (world.life.isDying) | Pay for the kill, drop loot, set how |
afterDeath | It has left the world | React |
canCast | Whether an ability can be used now (the HUD hears it) | cancel(reason), or cancelled = false |
canTarget | Whether an ability may target something | cancel(reason), or cancelled = false |
beforeCast, afterCast | An ability is about to go off (before paying), or went off | Change manaCost or cooldown, cancelled |
beforeEffect | An effect is about to reach one target (missile for a missile hit) | cancel() for that target: a spell shield |
effectLanded | An effect reached a target | React: an execute, a stack |
visible | Whether a team sees an entity (asked very often) | Change visible |
requirement | What making something requires (the HUD hears it) | cancel(reason), or cancelled = false |
available | Whether a player may make something at all | Change available |
beforeProduce | Something is about to be trained, researched, revived or built | Change cost or time, cancelled |
produced | Something was made | React: rally it |
beforeDeposit | A worker hands resources in | Change amount, cancelled |
beforeBuy | An item is about to be bought | Change cost, cancelled with a reason |
beforePickUp, beforeDrop | An item is about to be picked up or put down | cancelled with a reason |
beforeXp | Experience is about to go to a hero (from) | Change amount |
canLearn | Whether a hero may learn an ability | cancelled |
canAttack | Whether an attacker may attack a target (asked very often) | cancelled |
beforeAttack | An attack goes off, damage, crit and miss rolled | Change damage, type, splash, shot, tags, cancelled |
targetScore | A unit sizes up a possible target | Change score; -Infinity skips it |
beforeOrder | An order is about to be given | rejected |
canPlace | A structure is about to be placed (and in the preview) | cancel(reason) |
beforeBuff, buffEnd | A buff is about to go on, or ended | Change duration or level, cancelled |
spawn, remove | Something came into, or left, the world for good | Set it up, clean up |
supplyCap | A player's supply cap, once buildings are added | Change cap |
beforeSurrender, beforePause | A player gives up, or asks for a pause (left) | cancelled; change seconds |
Notes
targetScoreandcanAttackreuse one event object for every question: don't keep anything from it.- Data, not hooks:
UnitTypeDef.targetableBy: ['possess']makes a unit targetable by those abilities only.world.spawn(type, owner, at, { data, looksLike })gives a unit components by id and another's looks (Entity.likeness); thespawneffect takes the same (owner: 'neutral',looksLike: 'target').world.neutralis the neutral player. - A game's own effect that reaches a unit asks
mayReach(context, effect, target);effectTargetsasksbeforeEffectfor you.packages/engine/tests/effectHook.test.tsbuilds a spell shield, a mist and an execute with it. - Not hooks yet: attacks and projectiles, gathering and income, experience, visibility.
- Hooks or events? See Hooks vs events.