Skip to content

Write your own effect ​

When the engine's effects don't cover it, add a kind of effect of your own, or step in with a hook. Your effect works anywhere an engine effect does: in a missile, a zone, a trigger, a chain.

Quick start ​

ts
import { defineEffect, effectTargets, type Rules } from '@v12-engine/engine'

declare module '@v12-engine/engine' {
	interface EffectTypes {
		lifeTap: { share?: number }
	}
}

// hurts each target by a share of the caster's missing health, and heals the caster as much
export const lifeTap = defineEffect({
	type: 'lifeTap',
	run: (context, effect) => {
		const missing = context.caster.maxHealth - context.caster.health
		for (const target of effectTargets(context, effect, 'enemies')) {
			const amount = missing * (effect.share ?? 0.25)
			context.world.damage(target, amount, context.caster)
			context.world.heal(context.caster, amount, context.caster)
		}
	},
	fields: [{ path: 'share', kind: 'number', min: 0, max: 1 }]
})

export const rules: Partial<Rules> = { effects: [lifeTap] }

Now { type: 'lifeTap', share: 0.3, radius: 3 } works in any ability. A field the effect doesn't declare, or a type nobody declares, is a type error. fields tells the editor's Data panel what to offer.

Common tasks ​

Run a list of effects from your effect ​

runEffects runs a list the way the engine does (chains included). landEffects does the same where something arrived, and plays the strike on every screen.

ts
import { defineEffect, type EffectDef, landEffects } from '@v12-engine/engine'

declare module '@v12-engine/engine' {
	interface EffectTypes {
		atBall: { effects?: EffectDef[] }
	}
}

// Orianna's Dissonance, going off around her ball (kept in the ability's own state)
export const atBall = defineEffect({
	type: 'atBall',
	run: (context, effect) => {
		const id = context.caster.abilities.find((state) => state.id === 'command')?.data?.ball
		const ball = typeof id === 'number' ? context.world.get(id) : undefined
		if (ball) landEffects({ ...context, origin: ball, point: { x: ball.pos.x, z: ball.pos.z } }, effect.effects)
	}
})

Stop an effect reaching a target ​

The beforeEffect hook runs before any effect reaches a target. Call cancel() to skip that target.

ts
import { defineHook, hasBuff } from '@v12-engine/engine'

// a spell shield that eats one enemy effect
export const spellShield = defineHook({
	hook: 'beforeEffect',
	id: 'my-game.spellShield',
	run(world, event) {
		if (event.target.owner !== event.caster.owner && hasBuff(event.target, 'spellShield')) event.cancel()
	}
})

React after an effect lands ​

effectLanded runs after an effect reached a target: a stack for each hit, a reset on a kill.

ts
import { defineHook, setCooldown } from '@v12-engine/engine'

// the ability is ready again when its damage kills
export const resetOnKill = defineHook({
	hook: 'effectLanded',
	id: 'my-game.resetOnKill',
	run(world, { caster, ability, effect, target }) {
		if (effect.type === 'damage' && target.health <= 0) setCooldown(caster, ability.id, 0)
	}
})

beforeDamage changes any damage as it lands (true damage on a missile's way back, say).

Use a formula instead of a number ​

amount, duration, radius and other EffectValues take (context, target) => number:

ts
import type { EffectDef } from '@v12-engine/engine'

const bigger: EffectDef = { type: 'damage', amount: (context, target) => 50 + 10 * context.level + (target?.def.hero ? 100 : 0) }

In your own effect, effectAmount(context, effect, target) works out an amount the engine's way (scaling, target formulas), and valueOf(context, value, target) a time or size.

Keep state on an ability ​

AbilityState.data is your plain state of an ability on a unit (a zone it left, ammo). It is saved and sent to the owner's screen (AbilityInfo.data).

Change a cooldown from your effect ​

The cooldown starts before the effects run, so your effect may change it: setCooldown(entity, ability, seconds), reduceCooldown.

Reference ​

defineEffect ​

FieldWhat it does
typeIts name, declared in EffectTypes with the fields it reads
run(context, effect) => void, with effect typed as your fields plus EffectCommon
fieldsWhat the Data panel offers: names of EffectFields, or an EditableField in full

List it in Rules.effects. One of a type the engine has (damage…) replaces the engine's. lifeTap.of({ share: 0.3 }) builds one, typed. effectFields(game, type) lists what the panel offers.

EffectContext ​

FieldWhat it is
worldThe world
casterWhose effect it is: the damage, kills and scaling are its own
ability, levelThe ability and the level it was cast at
targetThe unit it was cast on, or that a trigger or missile reached
pointWhere it landed
originWhat it goes off from when not the caster: a proxy unit or a place
headingThe way it faces, when your code sets one
damageThe damage that set off a trigger
shareWhat's left after falloff (bounces, pierce)
heldHow full a hold was (0 to 1)
chargedWhether it went off charged
stacksStacks of the buff whose onEnd this is
distance, intervalHow far a missile flew; seconds between a channel's or zone's runs

The calls your effect stands on ​

CallWhat it does
effectTargets(context, effect, 'enemies' | 'allies')Who it reaches: its radius or shape, affects, on, tags, layers, beforeEffect
runEffects(context, effects)Runs a list as the engine does
landEffects(context, effects, impact?)The same where something arrived; screens draw the strike
launchMissile(context, effect)Sends a missile effect's missiles, and returns them
removeMissile(world, missile, { end? })Takes one out early, told to every screen
endZone(world, zone, { onEnd? })Ends a zone (world.zones) early
effectArea(context, effect), inArea(area, at, radius)An effect's area, and whether a point is in it
proxiesOf(context, origin)Which units an origin picks
effectAmount, valueOfAmounts and values the engine's way
setCooldown, reduceCooldownSet or cut a cooldown (by seconds or a share of what's left)
lowerChain(effects)Turns a when chain into nested lists
mayReach(context, effect, target)Whether beforeEffect lets it reach

Hooks ​

HookWhenCan
beforeEffectan effect is about to reach a targetcancel() it for that target
effectLandedan effect reached a target, after its handlerreact
beforeDamagedamage is about to landchange or stop it

See hooks for defineHook.

Notes ​

  • An effect of yours in a chain waits for a missile's hit as any does (when).
  • A computer player can't guess what your effect does: give the ability an ai.
  • packages/engine/tests/abilityHatches.test.ts builds Orianna's ball, Lux's Lucent Singularity and Yasuo's Wind Wall with these calls.