Skip to content
Work in progress. These docs describe Spawnite at launch, and some parts are still being built.

Plugins

A plugin is one named part of the game’s step: its systems, the commands a player sends it, and what it registers on the world. A game lists the plugins it runs in src/game.ts, in the one defineGame call that names the game, the scene it starts in and what the room and the page must agree on. Game, the harness, the room and spawnite simulate read that one export, so a page and its room step the same systems in the same order. The smallest list, for a game whose players walk, holds one plugin, as this src/game.ts does:

packages/engine/test/outside/walkers.ts
import { defineGame } from "@spawnite/engine/core";
import { characters } from "@spawnite/engine/characters";
export const game = defineGame({
name: "walkers",
start: "meadow",
plugins: [characters()],
});

A game with health, weapons, abilities and a bag lists the engine’s plugin for each, and its own beside them, such as the regeneration plugin that A game’s own plugin builds:

src/game.ts
import { abilities } from "@spawnite/engine/abilities";
import { defineGame } from "@spawnite/engine/core";
import { cooldowns } from "@spawnite/engine/cooldowns";
import { resources } from "@spawnite/engine/resources";
import { stats } from "@spawnite/engine/stats";
import { weapons } from "@spawnite/engine/weapons";
import { behaviours } from "@spawnite/engine/behaviours";
import { characters } from "@spawnite/engine/characters";
import { inventory } from "@spawnite/engine/inventory";
import { regeneration } from "./regeneration.plugin";
export const game = defineGame({
name: "sled",
start: "lobby",
plugins: [
characters(),
stats(),
cooldowns(),
resources(),
behaviours(),
weapons(),
abilities(),
inventory(),
regeneration(),
],
});

defineGame composes the list as the module loads, so a requirement missing, a cycle, a bad anchor or two listings of one plugin with different options fail there, with the composer’s message. Game takes the game, <Game game={game}>, as Game shows.

A headless test passes the list beside the scene, here a spawn function such as the spawnCoins that Headless tests defines:

// Excerpt of test/coins.test.ts
const headless = await createHeadlessGame({ scene: spawnCoins, game });

A room loads a scene’s file and nothing else of the game, so each scene’s file re-exports the game, as The game in a scene’s file shows.

plugins holds plugins and nothing else: a kit that bundles several is a spread, ...kit, and so is a conditional one, ...(hardMode ? [rules()] : []). With no plugins, or an empty list, a world runs the core alone. Game makes its world from the game as it mounts, and a new world when the game holds other plugin objects, as a hot update to a plugin’s module makes. An engine plugin’s factory returns one object for each set of options, so a game defined again on each render keeps its world.

The engine’s own systems are plugins, built with the same definePlugin a game calls, and a game lists each one it runs. Under them is the core: the step’s own machinery, which every world runs and no game lists, omits or replaces. Its systems are core.copyInput, core.noteActivity, core.move, core.blendCorrections, core.physics, core.follow, core.restColliders and core.rebakeNavMesh, and a game’s system orders against them by their handles under core.systems.

Plugin What it adds It requires Its systems
characters() Each character’s movement on its input or along its path, and its pushes and speed modifiers. nothing characters.walk, characters.respawn, characters.aim, characters.npcMovement, characters.movement, characters.countDown
stats() One formula for every stat, and the fields stats set, such as a resource’s maximum. nothing stats.expireModifiers, stats.copyFields
cooldowns() One timer abilities, items and resources share. nothing cooldowns.countDown
resources() Health’s regeneration, and each resource a game declares: mana, rage, stamina. stats() resources.regenerate
machines() Each state machine’s wait and its when, as defineMachine() declares one. nothing machines.advance
behaviours() The engine’s behaviours, each its own system, and the Interact command. nothing behaviours.spin, bob, pickup, interact, trigger, capHealth, reapDead, respawnDowned, chase
weapons() The shots the room judges, and the projectiles in flight. behaviours() weapons.flyProjectiles
abilities() Casting: each cast a player asks for, its approach, its wind-up and its release. weapons(), cooldowns.systems.countDown, resources(), characters() abilities.approach, abilities.cast, abilities.keepRefusals
inventory() The bag, what an entity wears, the uses of its items, loot on the ground and the locks items open. stats(), cooldowns.systems.countDown, behaviours() inventory.useItems, inventory.loot
npcs() Each NPC’s routine, and the conversations a player opens. machines(), behaviours(), characters() npcs.converse, npcs.runRoutines
cameras() The follow aim and the followed body’s fade, once per drawn frame. nothing onCameraUpdate
tracks() Each mover’s ride along its track, and the triggers it reaches. characters() tracks.copyInput, tracks.move, tracks.markTriggers
matches() Matches: members, queues, score, clock, end and reset by step. machines() matches.takeRequests, matches.advance
parties() Room parties that queue and stay together. nothing parties.takeRequests
teams() Teams on the player: each joining player balanced onto a team, switching, and who is an ally. nothing teams.takeSwitches
building() Grid building: pieces placed, moved, stored and demolished by aim, and the areas that say who builds where. nothing building.takeCommands
carrying() Goods a character carries as a stack, the stations that move them, and custody in the world’s save. behaviours() carrying.settleCustody, carrying.overlapStations, carrying.interactStations
orders() Orders a player gives an NPC: walk to a station, work it a stage at a time, and come back. npcs() orders.answerGives, orders.runOrders
weather() The world’s weather: a replicated record blended from preset to preset, which the look draws as light, fog, rain and wet ground. nothing weather.open
controls() The engine’s own keys moved or dropped, so a game’s plugin can take them. nothing none
dragAndDrop() Drag and drop on the HUD: what a Draggable carries, onto a DropTarget. nothing none

A game whose players walk lists characters(), so the common game lists one plugin, plugins: [characters()], and a game with health adds behaviours() to it. A card game or a twin-stick ship lists no characters(): a player who joins its room holds a seat with no body, and nothing spawns a character. A plugin that needs another names it in requires, and the world throws as it is made where the list leaves it out, naming the line to add.

Each factory carries its plugin’s handles, characters.systems.movement or matches.systems.advance, for a game’s system to run before or after one, or to replace or omit it. core’s reference page tables every system by phase, in the order the step runs them with every engine plugin listed, with where it runs and what it does.

A game imports its plugin factories from @spawnite/engine/core, as src/game.ts above and the example template do, and a readable plugin’s from its own entry, such as matches from @spawnite/engine/matches. A factory is one object whichever entry exports it, so a list that takes weapons from @spawnite/engine/weapons beside the rest from core composes the same.

dragAndDrop() is all React, so it imports from @spawnite/engine or @spawnite/engine/dragAndDrop, never from core. Installing lists which entry each factory comes from. The root, @spawnite/engine, and @spawnite/engine/core export a closed plugin’s names too; a readable plugin’s names come from its own entry alone, such as import { matches, useMatch } from "@spawnite/engine/matches". The inventory’s and the abilities’ are the same: import { inventory, Loot } from "@spawnite/engine/inventory", and import { abilities, useCast } from "@spawnite/engine/abilities", with Targeting, SpellLook and useTarget beside them.

A call into a plugin the list leaves out throws and names the line to add, where it would otherwise do something no system ever reads. A game’s own plugin refuses the same way with requirePlugin(world, name), which it calls first in each function a game calls to change the plugin’s state, as startMatch does. A game lists its own plugin as the plugin itself or as a factory’s call, so for a game’s plugin the message names both forms. grantAbilities on a world listed without abilities() throws the second of these:

The plugin named "lamps" is off in this world: add it to plugins in src/game.ts, the plugin itself or a call to the factory that makes it.
abilities() is off in this world: add abilities() to plugins in src/game.ts.

The same holds for the following:

  • characters(): spawnCharacter, a scene’s <CharacterSpawn> as it mounts, and walkTo.
  • stats(): addStatModifier, removeStatModifiers, setStatBase and <Stats>.
  • cooldowns(): startCooldown.
  • resources(): changeResource, addResource, registerHealth and <Resource>.
  • machines(): a machine’s record added to an entity.
  • behaviours(): dealDamage, dealHealing and each of its components, such as <Spin>, <Health> and <Chase>.
  • inventory(): activateItem, <Inventory>, <Loot> and <Lock>.
  • weapons(), npcs(), tracks() and matches(): registerWeapon, registerShotSettings, spawnNpc, launchTrackMover, <TrackMover>, startMatch and endMatch.
  • A <CharacterSpawn> prop no listed plugin reads, such as weapons without weapons() or bagSlots without inventory().

walkTo and startCooldown also throw where the list omits the system they need: characters.movement for a player’s character, characters.npcMovement for a character no player controls, or cooldowns.countDown. isSystemEnabled(world, handle) reads whether the list holds a system, or a replacement in its place, whichever end runs it, without throwing.

The engine’s package ships nineteen plugins as readable source beside the closed, minified core. They are abilities(), behaviours(), building(), cameras(), carrying(), characters(), cooldowns(), inventory(), machines(), matches(), npcs(), orders(), parties(), resources(), stats(), teams(), tracks(), weapons() and weather(). Every other engine plugin is closed. A readable plugin sits in its own folder, node_modules/@spawnite/engine/readable/<plugin>/, so matches() is in readable/matches/, which holds the following:

  • The plugin’s TypeScript source, with its comments, each file headed by its license line, MIT-0.
  • dist/: one unminified script per source file, which is what a game runs, with a declaration and a map for each, so an editor’s go-to-definition opens the source.

matches() answers the core’s match question through its plugin’s matchRule, which the core’s physics, queries, damage, chase and respawn read. The rule names the traits whose add or remove moves its answer, scopeTraits, and where a member respawns, findRespawnPoints. A kit copied from it keeps each court’s scope.

A readable plugin imports the engine through its public entries alone, @spawnite/engine and @spawnite/engine/core, and no part of the core imports it. Every call it makes is one a game’s own plugin can make, so it doubles as a worked example of a game’s own plugin, and a game can copy one as the start of its own: its source is MIT-0, as the license says.

The rest of the engine stays closed: the core, which holds prediction, the room’s replication and the physics, and each plugin that still reaches into the core’s private modules. A plugin opens once it reaches the core through public entries alone; the others follow as each is moved onto them.

A plugin is installed and upgraded with the engine, listed by a game, and changed through its options. A kit is source copied into the game, which the game owns and edits, and an upgrade never touches it. The engine’s code splits into three layers, and these are their only names:

  • The core is the step’s own machinery, closed, which every game runs.
  • A plugin is listed by the game and changed through its options. A readable one, such as matches(), is built on the same public API a game uses and ships its source in the package.
  • A kit is a plugin or a part of one whose source the game holds.

Where games want different settings, a piece is a plugin; where they want different code, it is a kit. For example, a game that wants a longer match sets timeLimitSeconds on <Match>, a setting of matches(); a game that wants a match with lives and no clock copies the readable matches() into a kit and changes its code. A piece is also a plugin when other plugins or the platform build on its types, as abilities and weapons build on cooldowns, or when a fix must reach every game by an upgrade. A genre’s movement feel or its rules is a kit.

A readable plugin can be copied out into a kit: the copy keeps the plugin’s name and its place in the step, and the game lists the copy in the engine plugin’s place. A closed plugin ships no source to copy, so a game that wants different code from one replaces or omits the system it differs on, as Change an engine system shows. Unity ships packages beside samples a project imports and edits, and Unreal ships plugins beside starter projects a team copies; a plugin and a kit are the same split.

spawnite add kit follow-camera copies the aim from cameras(); list followCamera() in its place. spawnite add kit chase copies the chase from behaviours(), replacing behaviours.systems.chase. spawnite add kit npc-routines copies the routine interpreter from npcs(), replacing npcs.systems.runRoutines. spawnite add kit stat-expiry copies the timed modifier expiry from stats(), replacing stats.systems.expireModifiers. spawnite add kit machine-advance copies the pass from machines(), replacing machines.systems.advance.

Scaffold files lists these kits beside character-movement and platformer-movement.

A game is not stuck behind the engine’s plugins: it takes out any of their systems that no other listed plugin requires or anchors to, or puts its own in a system’s place, copies a readable plugin’s source, or writes a plugin of its own. The following show each way:

  • Take an engine system’s place. A chase that walks a route the navmesh finds takes the place of the engine’s chase with replaces: behaviours.systems.chase, as the tested sample in Change an engine system shows.
  • Copy a plugin and change it. A game may copy any readable plugin into a kit and change its code, such as matches() for a match with lives and no clock, as Plugins and kits says.
  • Write a plugin of its own. The example game’s kick is a plugin of the game’s own, as the tour shows, and so is Holdfast’s whole siege, in its src/siege/siege.plugin.ts.

A game takes one system of a listed plugin out with omit on defineGame, or puts its own in a system’s place with replaces, each by the system’s handle.

A game that keeps its dead as corpses writes omit: [behaviours.systems.reapDead] beside its plugins, and a plugin of its own that reads health at zero. A game with its own chase lists a plugin whose chase entry says replaces: behaviours.systems.chase, such as the one below.

characters() splits its work so each part goes alone. characters.systems.movement moves each player’s character, and a game with its own movement, such as a platformer’s, replaces it and keeps the rest. characters.systems.npcMovement still moves every character no player controls, characters.systems.aim still writes the character’s aim into its pose, and the walk command still sets the character’s path. The NPCs move before the players, so a player standing on an NPC is carried by its move. A game’s system that moves a base, such as a ferry, or steers or pushes an NPC, anchors before: [characters.systems.npcMovement], which places it before both movements.

A roguelike that ends the run on a death writes omit: [characters.systems.respawn], which keeps each player’s character down. behaviours.systems.respawnDowned still stands every other entity with a respawn, such as a crate, an NPC or a kart a player drives. A world that lists behaviours() and no characters() has no player’s character to respawn, and the behaviours stand everything else.

  • omit: [handle] on defineGame removes one system of a listed plugin from every world the game makes. A whole plugin is left out by not listing it. An omit of a system that another listed plugin requires, or anchors to without anchorIfPresent, fails as the game is defined, naming both.
  • replaces: handle on a system’s entry hands the replaced system’s place to it: its slot in the step, its anchors, and every anchor and requirement another plugin writes to the handle. The replacement keeps its own name, nav-chase.chase, and the dump names what it replaced. A predicted system is replaced only by an entry marked predicted: true, and an unpredicted one only by an unpredicted entry with the same runsOn, as the types require; for a system of another kind, omit the engine’s and add the game’s beside it. A replacement may itself be replaced, as a game replaces a kit’s replacement, and two plugins that replace one handle throw, naming both.
  • The core is neither omitted nor replaced: a game anchors to core.systems and no more.

The chase below takes the engine’s place, from the engine’s public entry alone. The engine’s chase points a chaser straight at the nearest player and moves it on the navmesh, sliding along whatever it meets, so a wall square across that line holds it. This one asks the navmesh for a route and walks its corners. Against a real room, it walks a wolf round a wall the engine’s chase stops at:

packages/room/test/outside/navChase.ts
import {
addTrait,
ChaseTrait,
createQuery,
definePlugin,
DownedTrait,
editTrait,
findNearestTarget,
findPath,
NavMeshTrait,
Not,
readEach,
TransformTrait,
VelocityTrait,
type AuthoritativeStep,
defineSystem,
Phase,
} from "@spawnite/engine/core";
import { behaviours } from "@spawnite/engine/behaviours";
import { Vector3 } from "three";
// A chase that walks the navmesh, built as a creator's plugin is, from
// the engine's public entry alone, in place of the engine's straight
// chase: it takes behaviours.chase's handle, so its slot in the step and
// every anchor to that handle are its own. Each chaser walks toward the
// nearest of its targets along the corners the navmesh routes it by,
// round what blocks the straight line.
const chasers = createQuery(ChaseTrait, TransformTrait, Not(DownedTrait));
const unmoved = createQuery(ChaseTrait, Not(VelocityTrait));
// Written in place per chaser.
const heading = new Vector3();
const pathStart = new Vector3();
const pathEnd = new Vector3();
/** How close a corner may be before the chaser heads for the next one. */
const cornerReach = 0.4;
function chaseAlongNavMesh(step: AuthoritativeStep) {
const world = step.world;
readEach(world, unmoved, (_traits, chaser) =>
addTrait(step, chaser, VelocityTrait),
);
const handle = world.queryFirst(NavMeshTrait)?.get(NavMeshTrait)?.handle;
readEach(world, chasers, (_traits, chaser) => {
const chase = chaser.get(ChaseTrait);
const position = chaser.get(TransformTrait)?.position;
if (!chase || !position || !chaser.has(VelocityTrait)) return;
const velocity = editTrait(step, chaser, VelocityTrait);
// Along the ground, as the engine's chase measures.
const nearest = findNearestTarget(world, chaser, {
targets: chase.targets,
level: true,
});
const target = nearest?.entity.get(TransformTrait)?.position;
if (!handle || !nearest || !target || nearest.distance <= chase.reach) {
velocity.set(0, 0, 0);
return;
}
const route = findPath(handle, {
from: pathStart.copy(position),
to: pathEnd.copy(target),
});
const corner = route.corners?.find(
(point) =>
Math.hypot(point.x - position.x, point.z - position.z) >
cornerReach,
);
heading.subVectors(corner ?? target, position);
heading.y = 0;
velocity.copy(heading.normalize().multiplyScalar(chase.speed));
});
}
export const navChase = definePlugin(() => ({
name: "nav-chase",
description: "Walks each chaser along the navmesh to the nearest player.",
systems: [
defineSystem({
name: "chase",
phase: Phase.Rules,
replaces: behaviours.systems.chase,
description:
"Points each chaser along the navmesh's route to the nearest of its targets, round what blocks the straight line.",
run: chaseAlongNavMesh,
}),
],
}));

Freezing a character. A countdown, a meeting or a stun holds a character still with a system of the game’s own: a predicted system in the physics phase, before characters.systems.npcMovement, which runs there ahead of characters.systems.movement. It adds a speed modifier of more: -1 while the game’s own predicted stun trait lasts, and the room grants that trait with grantPredicted.

The player’s page counts the stun down as the room does, so the grant reaches it as one correction and the hold makes none; gravity and a push still move the character. A game that never walks its characters omits both characters.systems.npcMovement and characters.systems.movement: omitting movement alone stops only players’ characters. One whose characters move by rules of its own replaces it with a predicted entry.

definePlugin makes a plugin’s factory from a function of its options that returns the plugin’s parts, so the engine’s plugins and a game’s own are written the same way. The following plugin adds one system to the rules of the step:

src/regeneration.plugin.ts
import {
createQuery,
definePlugin,
defineSystem,
HealthTrait,
Phase,
updateEach,
} from "@spawnite/engine/core";
import { behaviours } from "@spawnite/engine/behaviours";
const healthy = createQuery(HealthTrait);
const heal = defineSystem({
name: "heal",
phase: Phase.Rules,
after: [behaviours.systems.capHealth],
description: "Heals everything with health up to its maximum.",
run: (step) => {
updateEach(step, healthy, ([health]) => {
health.current = Math.min(
health.maximum,
health.current + step.deltaSeconds,
);
});
},
});
/** Health that comes back a point a second. */
export const regeneration = definePlugin(() => ({
name: "regeneration",
description: "Health that comes back over time.",
systems: [heal],
}));

The function returns the plugin’s fields. name is always required, and a function that takes options also records options:

  • name: lower case letters, digits and dashes, starting with a letter, as frost-nova is. The dump and the devtools list the plugin under it, and each of its systems is named plugin.system after it: regeneration.heal above.

  • description: one line on what the plugin does, which spawnite simulate, spawnite play state and the devtools’ Wiki tab print beside its name.

  • systems: the plugin’s systems, each made with defineSystem({ name, phase, run }). The step runs four phases in order: input reads the player, motion turns intent into movement, rules judges the result, and physics moves the characters and settles the bodies. The Phase enum holds the four, and each definition names its phase. name is the system’s name after the plugin’s, so a definition named tendWardens runs as siege.tendWardens; it is written out, never read off the function, because a build renames a function, and the page and its room must name each system alike. run takes the step alone, and step.world is the world. A definition may add description, before, after, predicted, runsOn, replaces and answers. The step runs a phase’s systems in the order of the list.

  • before and after: the systems of the same phase that this one runs before or after. Name another plugin’s system by its handle: behaviours.systems.capHealth for the engine’s, as above, core.systems.physics for the core’s, or siege.systems.spawn for one of a plugin of your own. The factory carries a handle per system under systems, beside its commands. Name a sibling by its definition, as after: [heal], which the plugin that lists both resolves. A handle carries its phase, so an anchor across phases, a sibling the list does not hold, or a name such as "behaviours.chase" fails to typecheck.

    Wrap a handle in anchorIfPresent where the system orders against another only when that one runs: it skips the anchor where the game lists no such plugin or omits the system. Within a phase the step runs the core’s and the engine plugins’ systems first, in the engine’s order wherever the game lists them, then each of the game’s plugins’ systems in the order of the list. An anchor moves a system no further than it must. Unity orders a system with [UpdateAfter(typeof(X))] and Bevy with .after(x), each naming the system by its type or its function rather than by a string, and the handle does the same here.

  • predicted: true for a predicted system, one that steps a character on the character’s own clock, such as a dash.

  • runsOn: where the system runs in a game with a room, as Where a system runs shows. Left out, the room alone.

  • requires: what this one needs on the same world, by handle: a plugin definePlugin made, a plugin’s factory such as weapons or rounds, or a system’s handle such as behaviours.systems.capHealth. A plugin it requires orders the plugins’ setup calls, each after the ones it requires, and leaves their systems in the order of the list: a system that must follow another plugin’s says so with after. A system it requires must run: an omit of it throws, and a replacement meets it.

  • replaces: on a definition, the handle of another plugin’s system whose place it takes, as Change an engine system shows.

  • commands: the commands a page may send the plugin, each made with defineCommand, and answered by the one system that names it in answers, as Send the room a command shows. A command declared predicted: true runs on the player’s page on the tick the player sent it and on the room on the same tick, and its answering system is a predicted system, as Build a predicted mechanic shows.

  • inputs: the held inputs a page sends with every tick, each buttonInput(), axisInput(min, max), vector2Input() or angleInput(), with keys and touch where a key or a touch button holds it. A predicted system reads one with readInput(character, plugin.inputs.name), as Hold a key describes. buttonInput({ edge: true }) reads true on the tick its key went down alone, as characters.inputs.jump does, and readInputChanges(step, character, handle) lists a button’s changes within the tick in order. characters() declares the character’s own five, move, heading, pitch, strafe and jump, which its systems read. A tick of a game’s inputs packs into at most 96 bytes across its plugins, maximumInputBytes, with at most 32 buttons, and the welcome compares them, so a page and a room that lay them out apart refuse each other.

  • collisionLayers: the names of the collision layers its bodies stand in, each a handle plugin.collisionLayers.name that a body’s collisionLayer names and a collisionMask lists, as Layers describes. The core reserves two and characters() declares two, so a game’s plugins share the other 12, numbered in list order, and the welcome compares the numbering.

  • actions: the keys the page acts on and the room never hears, each pageAction({ keys }), such as a HUD’s window or a scoreboard held open. A component runs what it does with useAction(plugin.actions.name, { onPress, onRelease, enabled }), and its touch button shows only while one listens and is enabled. As the world is made, every key the game’s inputs, commands and actions bind is checked against the others and the engine’s, and a clash throws with the fix.

  • setup: (world, writer) => undo, for the plugin’s registrations, such as registerAbility(writer, handle). It runs on the room and on every page, as the world is created and again after each reset. The world is read-only, and the setup writer writes any trait on it, addTrait(writer, world, Trait, value), until the setup returns; it spawns nothing, since a page would make a second copy of what the room streams. The function it returns undoes it, with the writer open again. A logic component that registers as it mounts, as <Weapon> does, uses useSetupEffect((writer) => undo, dependencies) instead.

    onTraitAdd(writer, Trait, (entity, authority) => …) in a setup calls the listener as each entity gains the trait, with the world’s authority, or null on a page in a room. The setup returns the call that stops it. An entity that gains the trait while a player’s join runs, such as one an onPlayerJoin hook spawns, reaches the listener once the player’s join ends, so the listener writes with the world’s authority. A refused join’s entities never reach it. As Bevy’s OnAdd observer.

  • onCharacterSpawn: a function of one context, { writer, character, player, props }: the authority a character was spawned under, the character as a PredictedEntity whose predicted traits it may add, its player and the scene’s <CharacterSpawn> props. It is for what the plugin adds to each character, as What a plugin adds to a character shows.

  • onCharacterRespawn: a function of { writer, character, player }, run as a downed character stands again, for what a plugin resets each life.

  • onPlayerJoin, onPlayerReady, onPlayerDisconnect, onPlayerReconnect and onPlayerLeave: functions of one context, { step, player }, with returning on the join and reason on the leave. They are for what the plugin does as a player joins, drops, comes back and leaves, as What a plugin adds to a player shows.

  • onCameraUpdate: a function of { writer, camera, orbit, deltaSeconds }, run while a camera with an orbit is mounted. It runs after transforms, animation and the event drain, before shake, on frames the loop steps or holds under the devtools pause. It skips frames held only for a modal, a waiting world or loading physics. Hooks run in dependency then list order; deltaSeconds is the frame’s seconds, scaled by the devtools speed. Rooms and headless mounts run none. The slot follows Roblox’s RenderPriority.Camera, Unreal’s UpdateCamera and Cinemachine’s camera stages. A following rig lists cameras(), or another plugin that declares this hook.

  • onEntityDestroy: a function of { world, entity, reason, writer }, run inline on every world, a page’s too, as an entity leaves it. The entity still holds its traits, and every reference to it still names it, so a hook clears what it keeps about the entity, as the chase emits its exit on a chaser whose target goes. reason is an EntityDestroyReason:

    • Destroyed, by destroy or the engine, a body fallen below the world included.
    • LevelChanged, as the scene starts again.
    • Unmounted, as the component that placed it unmounts.
    • StreamRemoved, as the stream takes it off a page: the room destroyed it, or it left what the page sees.
    • PlayerLeft, for the player entity and the player’s saved entities.

    The hook writes another entity through findAuthority(writer, entity): any entity on a world that decides outcomes, on a page the page’s own alone, and none inside a player’s join, whose refusal could not take the write back. Each authority it gives ends with the hook. A refused join’s take-back, a checkpoint’s restore and a world’s teardown run no hook. A throw is reported in a built game and the hooks after it run. As Unity’s OnDestroy and Unreal’s EndPlay with its reason.

  • options: the options the factory was called with, as JSON, which the function records. A plugin with options writes export const karts = definePlugin(({ laps = 3 }: KartsOptions = {}) => ({ name: "karts", options: { laps }, ... })). The parameter’s annotation types the options a game passes. The dump shows them, and a page and its room compare them. The factory makes one plugin for each distinct options, so a list written again on each render, and a kit that lists it with the game’s own options, name one plugin. The factory carries the plugin’s handles, karts.systems.drive, read from a call with no options, and its name, so another plugin writes requires: [karts].

    Two calls name one plugin when their recorded options are the same JSON and each function and handle they were given is the same object. So a function that takes options records them, which definePlugin’s type requires where the parameter’s annotation types the options. A call that passes options to a plugin that records none throws, naming the record to add. A record may leave out each setting at its default, as characters() records {}. A handle such as a Team compares by identity, so two calls with different teams of one key are two plugins. A plugin whose options are required, as teams()’s are, passes the options its handles are read from as a second argument, definePlugin((options: TeamsOptions) => ..., { handlesFrom: { freeForAll: true } }). A game’s call must then pass options of its own.

  • component: a React component Game mounts around its children, for the plugin’s own views or providers. The components nest in the order of the list, the first outermost, and each renders its children. It is a view: a room and a headless mount never mount it, so a scene’s logic must not read a context it provides. A room still loads its file, with each view package the file imports as a stub, as What a room loads says.

  • saves: what the plugin keeps in the player’s save, by name, each made with defineSave: a version, a schema, read, restore, migrations, scopes and requires. Each is kept under kits.<name>.<save> in the saved record, on once the game lists the plugin, until the game’s saves option on the call turns it off or moves it. definePlugin throws for a save defineSave did not make, and for the older save field.

src/quests.plugin.ts
import { defineTrait } from "@spawnite/engine/core";
import { addOrSetTrait, definePlugin, defineSave } from "@spawnite/engine/core";
import { z } from "@spawnite/schema";
/** The quests a character has finished, by id. */
export const QuestsTrait = defineTrait("quests", () => ({
done: new Set<string>(),
}));
export const quests = definePlugin(() => ({
name: "quests",
saves: {
done: defineSave({
version: 1,
// Kept on each saved entity, the character under `main`.
scopes: ["entity"],
schema: z.object({ done: z.array(z.string()) }),
// An array, since JSON keeps no Set.
read: ({ entity }) => ({
done: [...(entity.get(QuestsTrait)?.done ?? [])],
}),
restore: ({ entity, writer }, saved) => {
const done = new Set(saved.done);
addOrSetTrait(writer, entity, QuestsTrait, { done });
},
}),
},
}));

The world’s creation checks the list and throws on each of the following, with a message that names the fix:

  • Two plugins with one name, or a plugin named core. The same object listed twice counts once, so a plugin that a kit requires and the game also lists is fine. So is an engine plugin’s factory called twice with the same options; with different options it throws. A plugin of the game’s own may take an engine plugin’s name, as a kit copied from that plugin’s source does, and then stands in for it.

  • A plugin whose requires names one the list lacks, such as abilities() without weapons(), and plugins that require each other in a cycle.

  • An anchor to a system that does not run, of a plugin the game does not list or one the list omits. The message names the plugin and the game’s list:

    spell.tick runs before weapons.flyProjectiles, but the game lists no plugin named weapons: it lists siege, spell. Add weapons() to plugins, or anchor with anchorIfPresent(weapons.systems.flyProjectiles) where spell.tick orders against it only when it runs.

    A plugin that runs with or without another, and must sit on one side of it when both run, wraps the handle in anchorIfPresent, as matches() scores before npcs()’s conversations with before: [anchorIfPresent(npcs.systems.converse)].

  • An anchor to a system the listed plugin of that name has not got, as when a factory’s options left the system out. The message lists the systems the plugin has.

  • An omit or a replacement that cannot hold. That is one of a plugin the game does not list, of a system the plugin has not got, or of the core’s. So is one system both omitted and replaced, two replacements of one system, a replacement of another kind, and replacements that loop.

  • An anchor to a system of another phase, and systems that anchor each other in a cycle. A plugin written in TypeScript meets the phase as a type error first. In a plugin written in JavaScript, defineSystem throws for it and for a name in place of a handle or a definition, and the factory’s call throws for a sibling its list does not hold.

A system reads the step’s own input with readLocalInput(step, handle), or gets null where the step carries no input. Read it before core.systems.copyInput, which takes an edge button off; on a re-run it reads the input the re-run carries.

A plugin’s onCharacterSpawn runs each time a character spawns: when the characters plugin spawns a player’s, when a game calls spawnCharacter, and when a headless game places one. It runs in a room and in a game played alone alike. A page in a room runs none: it spawns no character, and the room’s stream brings the player’s character with what the room’s hooks added. A hook run there again would write its defaults over what the room sent, such as a resource the character has spent. The hook takes one context: writer, the authority the character was spawned under, character, the character entity as a PredictedEntity, and player, null for a character no player plays. Its props are the scene’s <CharacterSpawn> props with what spawnCharacter laid over them. So one function gives the character the same traits wherever it spawns:

src/siege/siege.plugin.ts
import { addTrait, definePlugin, defineTrait } from "@spawnite/engine/core";
import { weapons } from "@spawnite/engine/weapons";
export const WardenTrait = defineTrait("warden", { class: "scout" });
declare module "@spawnite/engine" {
interface CharacterSpawnProps {
/** The class each warden starts as. */
wardenClass?: "scout" | "sapper";
}
}
export const siegePlugin = definePlugin(() => ({
name: "siege",
requires: [weapons],
onCharacterSpawn: ({
writer,
character,
props: { wardenClass = "scout" },
}) => {
addTrait(writer, character, WardenTrait, { class: wardenClass });
},
}));

The scene then writes <CharacterSpawn weapons={["rail"]} wardenClass="sapper" />. The declare module block types the plugin’s own prop, on the Player and in the hook alike.

The hook runs once per spawned entity, once the character holds its base traits: its transform, health, stats and respawn. It runs before the player’s save restores onto the character, so a save’s value wins over what the hook set. A respawn stands the same entity again and runs onCharacterRespawn({ writer, character, player }) instead. The hooks run in the order of requires, each after the plugins it names, and otherwise in the order of the list. In a room, the room keeps the props the scene’s Player was given and hands them to the hooks for every player who joins. The hooks get every prop a room’s checkpoint can keep: each but a function, such as children. Keep a plugin’s own prop to data, a number, a string, a list or a record, so a room’s checkpoint can keep it.

The engine’s own plugins give a character its kit the same way: weapons() reads weapons, abilities() reads abilities, and inventory() reads bagSlots. A game that leaves a plugin out of its list gets none of what that plugin adds: with no inventory(), a character has no bag. Unreal’s Game Features add components to an actor class as a feature activates, and Roblox raises CharacterAdded for a script to add to each character.

A player is an entity the core makes as the player joins: a room makes one for each connection, and a game played alone makes one for its player once the player’s save has loaded. It holds who the player is and whether the player’s connection holds, in traits a game reads and never writes. PlayerTrait holds the player’s account, name, seat, role and avatar, and ConnectionTrait says connected, or held with the tick the player’s hold ends on. Read them with readTrait(player, PlayerTrait); koota’s own set, add and remove refuse them by type, and a development world throws on a write that slips past the type. A game adds whatever belongs to the person rather than to the player’s character: a score, a team, a hand of cards. readPlayers(world) lists every player, as Roblox’s Players:GetPlayers() does.

Each hook runs on the room’s tick, or as a game played alone starts, in the order of requires and otherwise of the list. Each takes one context, whose step is a JoinStep for the join and ready hooks and the room’s step for the others:

  • onPlayerJoin({ step, player, returning }) runs once the player entity is made and before the player’s save restores, so what it adds is a default the player’s save overwrites. returning says the player’s account held a seat in this room earlier.
  • onPlayerReady({ step, player }) runs once the player’s save has restored, so it reads what the player saved. The engine’s characters plugin spawns the player’s character here, so a saved choice can pick what the character spawns as.
  • onPlayerDisconnect({ step, player }) runs as the player’s connection drops and the room starts holding the player’s seat; onPlayerReconnect({ step, player }) runs as the player takes it back within the hold.
  • onPlayerLeave({ step, player, reason }) runs before the core destroys the player entity, in the reverse order. A destroy inside a leave hook waits until every leave hook has run and the player’s save has read the player’s record, so a game’s hook reads the character though the characters plugin asked to destroy it. What a hook hands on as the player goes, as a survival game drops the character’s bag as loot, is not in the player’s save. Its reason is LeaveReason.Left for a player who quit, HoldEnded for one whose connection did not come back, Kicked, or RoomClosing.

A JoinStep reaches the joining player and what the player’s join spawns, and nothing else. spawn(step, prefab) takes it, and so does every call that takes an EntityAuthority, such as destroy, attach and emitEvent for an event on one entity, for the player or an entity the player’s join spawned. A call that takes an Authority, such as kick or dealDamage, fails to typecheck, and a call on any other entity throws. While the player’s join runs, every other authority over the world is refused where it is used, one a logic component’s useAuthorityEffect holds or one kept from before among them.

A join hook adds the player’s own traits with player.add. A join hook that throws refuses the player’s join. A development room throws at once. A room the platform hosts takes out every entity the player’s hooks spawned and every event they emitted, frees the player’s seat and closes the player’s page naming the plugin, so the world is as it was before the player joined. Change the rest of the world from a system, whose step reaches it, once the player’s join has run. A drop, rejoin or leave hook that throws is logged, and the room goes on. kick(step, player, { message }) removes a player as the step ends, the player’s save stored first, and the player’s page shows the message:

packages/engine/test/outside/scoreboard.ts
import {
defineTrait,
definePlugin,
kick,
LeaveReason,
readPlayers,
readTrait,
PlayerTrait,
type AuthoritativeSystem,
addTrait,
defineSystem,
Phase,
} from "@spawnite/engine";
/** A player's points this match: on the player entity, so a respawn or a
* card game with no character keeps them. */
export const ScoreTrait = defineTrait("score", { points: 0 });
/** Points a player reaches to win the match, which ends it for everyone. */
const winningPoints = 10;
/** Ends the match once a player reaches `winningPoints`: every player is
* kicked with the winner's name. */
const endRound: AuthoritativeSystem = (step) => {
const world = step.world;
const players = readPlayers(world);
const winner = players.find(
(player) => (player.get(ScoreTrait)?.points ?? 0) >= winningPoints,
);
if (!winner) return;
const name = readTrait(winner, PlayerTrait)?.name ?? "Someone";
for (const player of players)
kick(step, player, { message: `${name} won the match.` });
};
/** The names of the players who quit, rather than lost their connection. */
export const quitters: string[] = [];
export const scoreboard = definePlugin(() => ({
name: "scoreboard",
description: "A score on each player, and the round's end at 10 points.",
onPlayerJoin: ({ step, player }) => addTrait(step, player, ScoreTrait),
onPlayerLeave: ({ player, reason }) => {
if (reason === LeaveReason.Left)
quitters.push(readTrait(player, PlayerTrait)?.name ?? "");
},
systems: [
defineSystem({
name: "endRound",
phase: Phase.Rules,
run: endRound,
}),
],
}));

A game whose players control something other than a character, or nothing, such as a card game, lists no characters(): a room then spawns no character for a player who joins, and the player entity is all the player has. A game that spawns its players’ characters itself, later, lists characters({ autoSpawn: false }), so its characters still move. A game played alone spawns its character from its <CharacterSpawn> either way. A test adds a player with addPlayer(world, { name }), and drops, takes back and removes the player with dropPlayer, rejoinPlayer and removePlayer, each running the same hooks. Roblox raises PlayerAdded and PlayerRemoving for a script, and Unreal’s game mode calls PostLogin and Logout; here the player is an entity, so a game adds traits to the player entity rather than subclassing, and a scoreboard is a query.

A room loads a scene’s file and nothing else of the game, so each scene’s file re-exports the game: export { game } from "../game";. The game carries its room, defineGame({ room: defineRoom({ ... }) }), as Set the room’s players and rates says. A game that changes its physics scene declares it there too, physics: definePhysics({ gravity: [0, -20, 0], solverIterations: 8, killBelowHeight: -100 }) on defineGame, so the page and the room read one setting.

Each setting left out takes its default: gravity 9.8 m/s² down, Rapier’s 4 solver passes a tick, and a killBelowHeight of -500 m, null for none. A page and its room compare the settings at the welcome and refuse each other when they differ. A dynamic body or a living character below killBelowHeight emits FellOutOfWorldEvent, and the next physics step destroys it unless a system has put it back above the height with teleport. A player’s character is not destroyed: Health says what a fall does to the character. spawnite simulate reads that export first, then src/game.ts itself. A room and spawnite simulate refuse a scene’s file that exports systems, with an error that says how to move those systems into a plugin.

The plugins a world was made from are data an agent reads, the engine’s first, each with its name, description, requires, options, systems, isEngine and omitted, the systems the list omits. So is every system of its step, the core’s among them, in the order the step runs them, each with its name, plugin, phase, runsOn, predicted, isEngine, and replaces, the system whose place it took, where it took one:

  • spawnite simulate and the MCP’s simulate print the plugins under the hash, each marked as the engine’s or the game’s with what the list omits, then the systems, one line per phase, a replacement as nav-chase.chase (replaces behaviours.chase). The dump holds them as plugins and systems. The hash covers the entities alone. A plugin is marked the engine’s, isEngine, when it is one of the engine’s closed plugins. The readable plugins use the public API. Each prints as the game’s, such as matches (game), as a kit copied from one would.
  • spawnite play state and the MCP’s read_engine_state print both for a running page, and describe_entity returns the plugins beside an entity’s traits.
  • The devtools’ Wiki tab lists each system under its plugin, with the phase it runs in, the system it replaces, and each plugin’s omitted systems.

The example game lists characters(), stats(), cooldowns(), resources(), behaviours(), machines(), inventory(), matches() and its own kicks, lanterns, gathering and runs, and spawnite simulate --scene run --seconds 1 prints its step as follows:

Systems by phase, in the order the step runs them:
input: core.copyInput, core.noteActivity, characters.walk
motion: stats.expireModifiers, stats.copyFields, core.move, core.blendCorrections
rules: cooldowns.countDown, inventory.takeItemCommands, inventory.useItems, behaviours.spin, behaviours.bob, behaviours.pickup, inventory.loot, behaviours.interact, behaviours.choose, behaviours.takeInteracts, behaviours.trigger, behaviours.capHealth, behaviours.reapDead, behaviours.respawnDowned, characters.respawn, behaviours.chase, resources.regenerate, matches.takeRequests, matches.advance, machines.advance, kicks.kick, lanterns.takePlants, lanterns.returnLand, lanterns.takeCrafts, gathering.renew, gathering.presses, gathering.swings, gathering.stash, gathering.reapStashes, runs.countRunsWon
physics: characters.aim, characters.npcMovement, characters.movement, characters.countDown, core.physics, core.follow, core.restColliders, core.rebakeNavMesh

A page that joins the room of a game on plugins checks its list against the room’s. The room’s welcome names the plugins it lists, the systems its step runs, in order, and each plugin’s options, so an omit or a replacement on one side alone changes the names it compares. A page whose own step differs throws an error that prints both lists, since a page and a room that run different systems part on the first step either runs alone.

Bevy groups an app’s systems into a Plugin the app adds, and Unreal’s game feature plugins add abilities and components to the game that turns them on. Here the list is one array in one file, because a room, a headless run and an agent each read it without the page. The packages design page holds the reasoning and what the other engines do.