Skip to content

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 ​

ts
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 ​

ts
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 ​

ts
const lifeLost = defineHookType<{ amount: number }>({ id: 'td.lifeLost' })
const event = world.hooks.run(lifeLost, { amount: 1 }) // a mod's handler can change amount

Reference ​

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 ​

HookWhenA handler can
beforeDamageDamage is about to be dealt, before armourChange amount or type, cancelled
afterDamageDamage was dealtReact: life steal, on-hit effects
beforeHealHealth is about to be given backChange amount, cancelled
beforeDeathSomething is about to diecancel() and set its health
deathIt dies, still standing (world.life.isDying)Pay for the kill, drop loot, set how
afterDeathIt has left the worldReact
canCastWhether an ability can be used now (the HUD hears it)cancel(reason), or cancelled = false
canTargetWhether an ability may target somethingcancel(reason), or cancelled = false
beforeCast, afterCastAn ability is about to go off (before paying), or went offChange manaCost or cooldown, cancelled
beforeEffectAn effect is about to reach one target (missile for a missile hit)cancel() for that target: a spell shield
effectLandedAn effect reached a targetReact: an execute, a stack
visibleWhether a team sees an entity (asked very often)Change visible
requirementWhat making something requires (the HUD hears it)cancel(reason), or cancelled = false
availableWhether a player may make something at allChange available
beforeProduceSomething is about to be trained, researched, revived or builtChange cost or time, cancelled
producedSomething was madeReact: rally it
beforeDepositA worker hands resources inChange amount, cancelled
beforeBuyAn item is about to be boughtChange cost, cancelled with a reason
beforePickUp, beforeDropAn item is about to be picked up or put downcancelled with a reason
beforeXpExperience is about to go to a hero (from)Change amount
canLearnWhether a hero may learn an abilitycancelled
canAttackWhether an attacker may attack a target (asked very often)cancelled
beforeAttackAn attack goes off, damage, crit and miss rolledChange damage, type, splash, shot, tags, cancelled
targetScoreA unit sizes up a possible targetChange score; -Infinity skips it
beforeOrderAn order is about to be givenrejected
canPlaceA structure is about to be placed (and in the preview)cancel(reason)
beforeBuff, buffEndA buff is about to go on, or endedChange duration or level, cancelled
spawn, removeSomething came into, or left, the world for goodSet it up, clean up
supplyCapA player's supply cap, once buildings are addedChange cap
beforeSurrender, beforePauseA player gives up, or asks for a pause (left)cancelled; change seconds

Notes ​

  • targetScore and canAttack reuse 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); the spawn effect takes the same (owner: 'neutral', looksLike: 'target'). world.neutral is the neutral player.
  • A game's own effect that reaches a unit asks mayReach(context, effect, target); effectTargets asks beforeEffect for you. packages/engine/tests/effectHook.test.ts builds 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.