Timers and sequences
Run something later, or every so often (a timer), or script a series of steps that say, wait, then do (a sequence). Both count ticks, never the clock, and are saved with the world, so they survive a save and play the same in a replay.
Quick start
ts
import { defineTimer } from '@v12-engine/engine'
export const waveComes = defineTimer({
id: 'td.waveComes',
run: (world) => {
/* send the next wave */
}
})
// in the rules: timers: [waveComes]; then, in code: world.timers.after(30, waveComes)Common tasks
Do something later, or again and again
ts
const waveComes = defineTimer({ id: 'td.waveComes', run: (world) => { /* ... */ } })
const invaderComes = defineTimer<{ type: string }>({ id: 'td.invaderComes', run: (world, { type }, { left }) => { /* ... */ } })
world.timers.after(30, waveComes)
world.timers.every(1.2, invaderComes, { times: 20, first: 0, data: { type: 'skeleton' } })
world.timers.left(id) // seconds until it next goes off
world.timers.cancel(id)List timers in Rules.timers, so a loaded save finds their code.
Script a sequence: say, wait, then do
ts
const briefing = defineSequence({
id: 'td.briefing',
steps: [
(world) => world.message(DEFENDER, 'briefingStart', 'info'),
wait(8),
(world) => world.objectives.add(DEFENDER, frost),
waitUntil(hasFrostTower), // checked at the end of every tick
(world) => world.objectives.complete(DEFENDER, frost.id)
]
})
world.sequences.start(briefing)Steps: a function, wait(seconds), waitFor(event, test?), waitUntil(test). A sequence can be saved halfway through, which is why it is not an async function. List it in Rules.sequences. For missions built on sequences, see Make a mission.
Reference
A waiting timer or a running sequence is data in the world (which one, which tick or step, its data), found by id, so it saves and loads.
- Timers fire at the start of their tick, after commands, in the order they are due.
- The handler is told
left: how many more times it will run (0 on the last). world.sequences.start(sequence, data)returns an id forstopandrunning.- Mods add or replace timers and listeners by id (
game.timers,game.listeners).