Cooldowns
An entity keeps its cooldowns in the CooldownsTrait trait, by key. Each cooldown holds seconds, how long it runs, and endTick, the tick it ends on. A cooldown of N steps started on tick T runs on ticks T to T + N - 1, and the step on its end tick removes it. Nothing writes it between, so a room sends a cooldown as it starts and as it ends.
cooldowns() and the cooldown names on this page come from @spawnite/engine/cooldowns, a readable plugin a game can copy as a kit. globalCooldown and nameAbilityCooldown come from @spawnite/engine/abilities.
Use the following five functions:
defineCooldown(name)defines a key, once, at module scope.startCooldown(step, entity, { key, seconds })starts one. It takes the step first, aspushCharacterdoes: call it from a predicted system, so the player’s page starts it on the tick the player pressed and the room’s word corrects it. On an entity no player controls, such as a monster, a rule on the room calls it with the room’s step. On a player’s character, the prediction checks name a start from a system that is not predicted. A call from outside a step, such as a button’s handler, is a type error.isCoolingDown(entity, key)says whether one runs.findCooldown(entity, key)returns it, or undefined.readCooldownRemainingSeconds(entity, key)returns the seconds it has left, or 0 where none runs. It works them out from the tick the entity’s world stands on, so it moves each step with nothing written.
CooldownOptions and Cooldown hold the fields.
Choosing a key
Section titled “Choosing a key”defineCooldown(name) returns the key a cooldown runs under, a CooldownKey. A name is letters and digits starting with a lower case letter, such as "bolt". Whatever starts the same key on an entity shares that cooldown, and one name defined in two places is one key:
- One thing alone. A skill defines its own key, such as
defineCooldown("bolt"). - A group. Three potions each start
defineCooldown("drink"), so drinking one holds all three. - A global cooldown. Every ability starts
globalCooldownfor a second beside its own key, and checks both before it casts. - A buff or a ground effect. The system that lays a fire patch starts
defineCooldown("firePatch")on the caster. - An item. The inventory starts
item:and the item’s name, or the key the item’scooldownGroupnames, which a game’s own skill may start too.
A plugin defines its keys under a scope, defineCooldown(name, { scope }), which returns scope:name and takes any text as the name, so a plugin’s key never shares a cooldown with a game’s by accident. An ability runs its own under ability: and its name, which nameAbilityCooldown("bolt") returns, and every ability shares globalCooldown, abilityShared:global, so a game’s "bolt", an ability named "bolt" and one named "global" stay apart. A plain string is not a key: the type refuses it.
A cooldown started under a key that already runs keeps the longer of the two, so a short cooldown never cuts a long one.
A cooldown belongs to an entity. For a cooldown the whole world shares, such as a boss’s summon that any player triggers, keep it on the entity that stands for the thing: the boss, or the match.
Showing a cooldown
Section titled “Showing a cooldown”useCooldown(entity, key) reads a cooldown in a view. It returns { seconds, remainingSeconds } while one runs and undefined otherwise, and draws again after each step. remainingSeconds / seconds is the share left, which a button’s rim draws. On a page in a room it reads at the local page’s tick: right for the local player’s own cooldowns, and a little ahead for one the room starts.
What the other engines do
Section titled “What the other engines do”Unreal’s ability system keeps a cooldown as a timed effect that grants a tag, and an ability checks the tag: two abilities share a cooldown by sharing the tag. The effect replicates its start and its length once, as endTick does here, and Photon Fusion’s TickTimer keeps the tick it ends on. Minecraft’s use_cooldown component has a cooldown_group that items share. A Roblox game keeps a cooldown as a debounce, and a Godot game as a Timer node.
This engine takes Unreal’s and Minecraft’s shape, a key on the entity, because a timer by hand is what every game in this repository wrote for itself, each with its own name and its own way to end.
Sample
Section titled “Sample”A bolt that holds for two and a half seconds, and holds every skill for one:
import { type PredictedStep } from "@spawnite/engine";import { defineCooldown, isCoolingDown, startCooldown,} from "@spawnite/engine/cooldowns";import type { PredictedEntity } from "@spawnite/engine/core";import { globalCooldown } from "@spawnite/engine/abilities";
const boltCooldown = defineCooldown("bolt");
export function castBolt(step: PredictedStep, caster: PredictedEntity) { if ( isCoolingDown(caster, boltCooldown) || isCoolingDown(caster, globalCooldown) ) return; startCooldown(step, caster, { key: boltCooldown, seconds: 2.5 }); startCooldown(step, caster, { key: globalCooldown, seconds: 1 }); // The bolt itself.}Where it runs
Section titled “Where it runs”Both, for a player’s character. CooldownsTrait is a predicted trait: the player’s page counts the character’s cooldowns down on every step as the room does, so the player’s buttons drain smoothly between the room’s sends. The room’s word corrects the player’s page where the two differ, such as on a cooldown the room started. startCooldown takes the step, so a cooldown starts in a system: a predicted one for the player’s character, on the player’s page and on the room, or the room’s own for any other entity. Every other entity’s cooldowns count down on the room alone, and no page receives them. A page whose room is from before the predicted declaration counts nothing down and shows what the room sends.
Latency does not slow the player’s rate of fire. The room runs each predicted command on the tick the player’s page pressed it, so the cooldown ends on the same tick on both. On a link with a 150 ms round trip and 20 ms of jitter, a shot held down behind a 0.5 second cooldown ran on the room once every 30 ticks for 10 seconds, with no shot refused and no correction, as a test of a real room measures. Unreal’s ability system gates a predicted cooldown on the server’s timer instead, so a player there fires less often as latency grows.