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

Write your own behaviour

A behaviour of your own has five parts: a trait that holds its state, a behaviour that names the trait, a component that adds the trait to an entity, a system that reads and writes the trait each step, and a plugin that carries the system. A view reads the trait to draw it. The following lamp has all of them: it lights while a character stands within 2 m of it, stays lit for 3 seconds after the last one leaves, then goes out.

spawnite add behaviour Glow --system writes the same parts as files of their own, to start from. The trait, the behaviour and its component go in src/behaviours/Glow.tsx, a system in src/behaviours/GlowSystem.ts, and the system’s entry in src/rules.plugin.ts, the game’s own plugin. The scaffold is a starting point. For state other players see, shape it as the lamp below is, with defineTrait, the behaviour’s plugin and runsOn, and updateEach.

packages/room/test/outside/Lamp.tsx
import {
defineBehaviour,
definePlugin,
defineTrait,
findNearestTarget,
RunContext,
updateEach,
useBehaviour,
useEntity,
useTrait,
Views,
type AuthoritativeStep,
defineSystem,
Phase,
} from "@spawnite/engine";
import { createQuery } from "@spawnite/engine/core";
/** A lamp's state: how near a character must stand, how long it stays lit
* after the last one leaves, whether it is lit, and the tick it goes out,
* -1 while a character stands near. */
export const LampTrait = defineTrait("lamp", {
radius: 2,
holdSeconds: 3,
isLit: false,
offTick: -1,
});
const lampEntities = createQuery(LampTrait);
/** Lights each lamp while a character stands within its radius, and puts
* it out `holdSeconds` after the last one leaves. Each field is written
* only when it changes, so a view that reads the lamp renders only then. */
function tendLamps(step: AuthoritativeStep) {
const world = step.world;
updateEach(step, lampEntities, ([lamp], entity) => {
// A character a player controls now, by default.
const nearest = findNearestTarget(world, entity);
if (nearest && nearest.distance <= lamp.radius) {
lamp.isLit = true;
lamp.offTick = -1;
} else if (lamp.isLit && lamp.offTick === -1) {
const holdTicks = Math.round(lamp.holdSeconds / step.deltaSeconds);
lamp.offTick = step.tick + holdTicks;
} else if (lamp.isLit && step.tick >= lamp.offTick) {
lamp.isLit = false;
lamp.offTick = -1;
}
});
}
/** The lamps: listed in the game's plugins, in src/game.ts. */
export const lamps = definePlugin(() => ({
name: "lamps",
description: "Lamps that light while a character stands near.",
systems: [
defineSystem({
name: "tendLamps",
phase: Phase.Rules,
run: tendLamps,
}),
],
}));
export const LampBehaviour = defineBehaviour({
name: "lamp",
trait: LampTrait,
// Its component throws where the game does not list the plugin.
plugin: lamps().name,
// The room decides it, so its component takes no callback.
runsOn: RunContext.Server,
description: "Lights while a character stands near, and for a while after.",
ranges: {
radius: { min: 0.5, max: 10, step: 0.5 },
holdSeconds: { min: 0, max: 30, step: 0.5 },
},
});
export interface LampProps {
/** Metres from the lamp a character lights it within.
* @defaultValue `2` */
radius?: number;
/** Seconds it stays lit after the last character leaves.
* @defaultValue `3` */
holdSeconds?: number;
}
/** A lamp on its entity: the behaviour, and the bulb it draws, a view the
* room skips, once its entity accepts it. */
export function Lamp({ radius = 2, holdSeconds = 3 }: LampProps) {
const isAccepted = useBehaviour(LampBehaviour, { radius, holdSeconds });
return isAccepted ? (
<Views>
<Bulb />
</Views>
) : null;
}
/** The bulb: warm while the lamp is lit, dark while it is out. */
function Bulb() {
const isLit = useTrait(useEntity(), LampTrait)?.isLit ?? false;
return (
<mesh position={[0, 2, 0]}>
<sphereGeometry args={[0.25, 16, 16]} />
<meshStandardMaterial
color={isLit ? "#ffd166" : "#333333"}
emissive={isLit ? "#ffb347" : "#000000"}
/>
</mesh>
);
}

The game lists the plugin beside the engine’s in src/game.ts. In the example template, the list becomes plugins: [characters(), stats(), cooldowns(), resources(), behaviours(), machines(), inventory(), matches(), kicks, runs, lamps]. A scene places a lamp as it places any behaviour: <Entity position={[0, 0, -1]}><Lamp /></Entity>. A game with a room re-exports plugins from the room’s scene file, as Run a room while you build says. A scene that places a lamp in a game that does not list lamps throws as it loads. The error reads 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.

Import components, hooks, traits, queries and types from @spawnite/engine. For simulation code without React, use @spawnite/engine/core. Import entries lists the other entries.

Entity is the engine’s read-only entity type, and a system reads a World. createQuery and useTrait also come from the engine, which supports predicted and core traits. A game imports nothing from koota or koota/react.

defineTrait(name, fields, options) makes a koota trait and gives it a name. The name is the trait’s key in the world’s dump, in the devtools and in a room’s stream. A trait from defineTrait reaches every client in a room unless its options say otherwise:

  • reach: Reach.Owner sends it to the player whose character holds it, and nobody else.
  • reach: Reach.Authority keeps it in the room.
  • reach: Reach.Local has the room and each client keep their own, and no stream carries it.
  • predicted: true has the player’s client predict it on their own character, as Networking describes.

Declare a game’s state with defineTrait, so its name and whether a room streams it are explicit. State a world keeps for itself, which no dump, stream or checkpoint names, uses trait. Choose who a trait reaches describes each option.

A trait of fields keeps each field in a column of its own. A field holds a number, a string, a boolean, or an object a function makes, such as () => new Vector3(). The function returns a new object for each entity, since a room’s stream, a save and a tool write into the one an entity holds. One that returns the same record or vector twice throws in development, naming the trait and the field; freeze a record every entity shares with Object.freeze.

defineBehaviour({ name, trait, ... }) runs once, when its file is imported, and returns the behaviour. It names the behaviour in the devtools’ Add behaviour and Wiki tab, puts its trait in the dump under the trait’s own name, and holds its props to ranges in the inspector. plugin names the plugin whose system runs it: its component throws as the scene loads where the game does not list that plugin, so a lamp never sits dark because a line was left out. description is the line the inspector shows under its name.

runsOn means one thing on a behaviour and another on a system:

Where it is written What it decides
On a behaviour, defineBehaviour Where its consequence is trusted, and so whether its component may take a callback
On a system, its plugin entry Where the system runs: in the room alone, on the clients too, or on the clients alone

A behaviour’s runsOn takes the following values:

  • RunContext.Server: the room decides it, so its component takes no callback prop. The lamp is one.
  • RunContext.Both: the room decides it, and the player’s client runs it too, to predict it.
  • RunContext.Client, the default when left out: it only changes what a client shows, such as a spin.

Use defineBehaviour for a behaviour whose component adds its one trait and nothing more. Add behaviour gives an entity the trait alone, so a behaviour that also needs a body or a second trait would run there without it.

useBehaviour(behaviour, value) is the one call a behaviour’s component makes. It adds the trait to the nearest <Entity> with value as it mounts, and removes it as it unmounts. Between renders, it compares the fields of the new value with those it last wrote, one by one, a shallow comparison. A new object with the same fields writes nothing, so a render with the same props keeps an edit made in the inspector, and a changed prop wins over it. It writes only the fields value holds, so the lamp’s isLit and offTick, which its component never passes, stay as the system left them. On a client in a room, it writes nothing: the room’s copy of the scene added the trait, and the stream carries it.

An entity holds one trait of each kind, so it takes one behaviour of each trait, whichever component sets it. A second <Spin> inside the same <Entity> throws in development, naming the behaviour and the trait. So does a game’s own <BigSpin> that calls useBehaviour with the same behaviour beside a <Spin>, and the error names both. Two of them would share one trait: the second’s props would overwrite the first’s, and either one’s unmount would remove the trait from the other. Put the second on an <Entity> of its own, such as a child. For several of one thing on one entity, such as several spin axes, give the behaviour a list in its props. A built game logs the refusal once, the second writes nothing, and the first behaviour keeps running.

An engine component that sets several traits is accepted on all of them or refused on all. <Chase> sets the character’s capsule, its controller and CharacterTrait beside its own trait. So where a game’s component on the same <Entity> already holds CharacterCapsuleTrait, the whole <Chase> is refused, as it is where the <Entity> lists one of them in its traits. Where the <Chase> came first, the game’s component is the second, and it is refused.

useBehaviour returns whether the entity accepted the behaviour. The first on an entity reads true from its first render, so its effects run in the same commit as its siblings’. A refused second reads false at every render. A component whose behaviour does more than set its trait does the rest only while accepted: it renders a child, adds a stat or starts a sound when the value is true. Otherwise a refused second in a built game still runs that part, such as drawing a second light or firing a second callback. The following torch draws its light only once its entity accepts it:

packages/engine/test/outside/Torch.tsx
import {
defineBehaviour,
defineTrait,
useBehaviour,
useEntity,
useTrait,
Views,
} from "@spawnite/engine";
/** A torch's state: how bright it burns. */
export const TorchTrait = defineTrait("torch", { intensity: 2 });
export const TorchBehaviour = defineBehaviour({
name: "torch",
trait: TorchTrait,
description: "A light that burns on its entity.",
ranges: { intensity: { min: 0, max: 10, step: 0.5 } },
});
export interface TorchProps {
/** How bright it burns.
* @defaultValue `2` */
intensity?: number;
}
/** A torch on its entity: the behaviour, and the light it draws once its
* entity accepts it. */
export function Torch({ intensity = 2 }: TorchProps) {
const isAccepted = useBehaviour(TorchBehaviour, { intensity });
// A second Torch on the entity is refused, and draws no second light.
return isAccepted ? (
<Views>
<Flame />
</Views>
) : null;
}
/** The light, as bright as the torch's trait says. */
function Flame() {
const intensity = useTrait(useEntity(), TorchTrait)?.intensity ?? 0;
return (
<pointLight
position={[0, 1, 0]}
color="#ffb347"
intensity={intensity}
/>
);
}

The component runs in the room too, so where the extra work goes depends on who needs it:

  • Work for the page alone, such as a light or a sound, goes in a child inside <Views>, which the room skips, rendered only while accepted, as the torch’s light is. useBehaviour itself stays outside <Views>, which refuses it.
  • Work the room needs too, such as adding a stat, goes in an effect that guards on the value: useEffect(() => { if (!isAccepted) return; ... }, [isAccepted]).

A behaviour that sets its trait and nothing more, such as <Spin>, ignores the value. The lamp at the top of this page guards its bulb the same way as the torch. Each end decides for itself: the room and each page in a room accept the first that mounts there, and of several that mount at once, the first in the tree. A page reads true for the one it accepts, though the room writes the trait. Where the two ends mount different components of one behaviour in a different order, each can accept a different one.

A component that replaces another by a key on the same entity reads false at its first render and true from its next. The old one still holds the trait as the new one renders, and no render can tell a replacement from a second added beside it. So its guarded children and effects start one commit after the mount effects of siblings that mount with it.

A behaviour whose component writes its trait through verbs rather than from a value, such as <Inventory>, calls useBehaviourClaim(behaviour) instead of useBehaviour. It returns whether the entity accepted the behaviour and refuses a second the same way, but never adds, sets or removes the trait. The component writes the trait through its verbs only while accepted, and undoes what it wrote as it unmounts. A claim is the entity’s record of which component holds a trait, for as long as that component is mounted.

A system is a function of the world and the step. A plugin lists it under a phase: input, motion, rules or physics, which the step runs in that order. A behaviour’s rule goes in rules. The rules phase runs before physics moves the characters, so the lamp reads where a character stood before this step’s move, one step behind the physics.

readEach(world, query, callback) walks every entity the query matches. updateEach(step, query, callback) takes what may write first, and walks the entities that writer may write:

  • On the room and in a game played alone, every entity the query matches.
  • On a page in a room, for a query that hands a trait the room streams, the page’s own entities alone, such as one spawnLocal made. The room writes the rest. A system that runs on the page alone and leaves out an entity the room streams throws in development, since no end writes that entity through it.
  • For a query that hands only Reach.Local or Reach.Authority traits, every entity on any end.

A query that hands both a streamed trait and a predicted one takes the room’s step. The callback takes ([records], entity, index): one record for each trait of the query, in the query’s order, then the entity and its place in the walk.

  • readEach hands each record to read. For a trait of fields, the record is reused for the next entity, so keep a field, never the record, and a write to the record changes nothing.
  • updateEach hands the same records, and writes each one back once the callback returns. Write through the record, as the lamp writes lamp.isLit. A setTrait on a trait the walk holds is overwritten by that write-back; setTrait on any other trait, and reads of any trait, are safe.
  • An entity the callback destroys during an updateEach is skipped, and nothing is written back to it.
  • updateEach compares each written record with what it held. Where a field changed, it tells each view that reads the trait with useTrait once the walk ends. Where nothing changed, no view renders.

A system counts time from the step. step.deltaSeconds is one tick of the world: 1/30 s at 30 ticks a second, the default of a game with rooms, and 1/60 s at 60 ticks, the default of a game played alone. step.tick is the number of the step, which the room and every client share. Never read the wall clock, Date.now() or performance.now(), or draw from Math.random() in a system: the room, a client re-running a predicted step, and a replay’s rerun each run the step again, and must reach the same result. The game’s lint warns of Math.random in a system; random(step) draws from the world’s own stream on the room’s step.

findNearestTarget(world, entity, { targets, level, from, accepts }) returns the target nearest to entity, as { entity, distance }, or null where the world holds none. With no targets, a target is a character a player controls now; targets takes the kinds, layers, teams and rules Trigger lists. The distance is measured in 3D, from the entity’s position, or from, to the target’s feet. level: true leaves height out, and accepts skips each candidate it returns false for. findOverlappingTargets(step, entity, { radius, targets }) lists every target whose body overlaps a sphere about the entity, nearest first, as Pickup and Trigger meet them.

A plugin’s system with no runsOn runs in the room, and in the game itself when it plays alone, and never on a client in a room; its step is an AuthoritativeStep, which every function that decides an outcome takes. runsOn: RunContext.Both runs it on every client too, and RunContext.Client on the clients alone, for a system that only draws; such a step may be a client’s, so an outcome in it sits inside if (step.authoritative). A game played alone runs every system, whatever it declares.

The bulb reads the lamp with useTrait(useEntity(), LampTrait), and renders again each time a field of the lamp changes. The lamp writes each field only when its state turns, so the bulb renders a handful of times, never once a step. A field a system rewrites every step, such as a countdown in seconds, renders its view every step; keep such a field apart from what the view reads, or count in ticks, as offTick does.

The lamp’s system runs in the room, which decides when each lamp is lit, and LampTrait reaches every client, so every player sees the same lamp lit. The bulb is a view inside <Views>, which the room skips: each client draws it from the lamp it holds. Played alone, the game runs the system itself.

Each name is checked as it is made, and a name that breaks its rule throws, naming the rule:

  • A plugin’s name is lower-case letters, digits and dashes, and starts with a letter: "lamps" and "frost-nova" pass, and "Lamps" and "frost_nova" throw.
  • Every other name is letters and digits, with no dash, no underscore and no space. A trait’s, an event’s, a behaviour’s, a prefab’s and a collision layer’s start with a lower-case letter: "lamp" and "kartBlue" pass, and definePrefab("returning-coin", ...) throws on its dash. A system’s key may start with either case.
  • A trait and its behaviour may share a name, as LampTrait, defined as "lamp", and LampBehaviour, named "lamp", do. The two names live apart: the trait’s is its key in the dump and the stream, and the behaviour’s is its row in Add behaviour.
  • Two traits, two behaviours or two events may not share a name. A second definition under a name another of its kind holds throws, naming both. The exception is a game’s own module that runs again and defines it anew, as a hot reload or a watched room’s restart does.

spawnite simulate runs a scene in Node and stops when a condition over its entities holds. --scene street loads src/scenes/Street.tsx, the name in PascalCase, and mounts the component that file exports as Street. With the lamp in such a scene, 4 m ahead of the character’s spawn, the following command holds W until any lamp is lit and prints the lamps:

Terminal window
spawnite simulate --scene street --keys w --until "Object.values(entities).some((entity) => entity.lamp?.isLit)" --fields lamp

The following output is an example: no game in this repository holds a street scene, so your hash and your listing differ. It comes from the example game on 2026-10-06, with the lamp listed in its plugins and a street scene that places one lamp 4 m ahead of the character’s spawn. It printed The condition held on step 28; it returned true. and Hash 2323499623, stepped in 152 ms., then the plugins and the systems by phase, the lamps’ lamps.tendLamps last among the rules, and the lamp with "isLit": true.

The whole output
The condition held on step 28; it returned true.
Hash 2323499623, stepped in 152 ms.
Plugins, the engine's first, then the game's in the order it lists them:
stats (engine): One formula for every stat, and the fields stats set.
cooldowns (engine): One timer abilities, items and resources share.
behaviours (engine): The engine's behaviours: spin, bob, pickup, interact, trigger, health and chase.
characters (game): The characters: their movement, their pushes and speed modifiers, and the character each player gets as the player joins.
inventory (engine): The bag, what an entity wears, the uses of its items, loot, and locks.
resources (engine): Health, and each resource a game declares: mana, rage, stamina.
machines (engine): The state machines: the match, the NPC runner, a game's own.
matches (game): The match: its score, its clock, and how it ends.
kicks (game): Kicks a ball within reach on F.
runs (game): Counts each run the player wins, which their save keeps.
lamps (game): Lamps that light while a character stands near.
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.teamChange, characters.respawn, behaviours.chase, resources.regenerate, matches.takeRequests, matches.advance, machines.advance, kicks.kick, runs.countRunsWon, lamps.tendLamps
physics: characters.aim, characters.npcMovement, characters.movement, characters.countDown, core.physics, core.follow, core.restColliders, core.rebakeNavMesh
{
"5": {
"lamp": {
"holdSeconds": 3,
"isLit": true,
"offTick": -1,
"radius": 2
}
}
}

The lamp’s test files run it alone and in a room. Alone, a character walks to the lamp, then away, and the lamp stays lit for exactly its 180-step hold, plus the one step the rules phase reads behind the physics. A view beside the bulb renders on each change of the lamp and on nothing else. A scene in a game that does not list lamps throws as it loads. In a room, the room lights the lamp while a player’s character stands near it, and the player’s client reads it lit, then out after its hold. The files printed the following on 2026-10-05:

✓ |@spawnite/room| test/outsideLamp.test.ts > lights while a character stands near, stays lit for its hold after it leaves, then goes out 519ms
✓ |@spawnite/room| test/outsideLamp.test.ts > throws as the scene loads where the game does not list the lamps plugin 12ms
✓ |@spawnite/room| test/outsideLampRoom.test.ts > lights the room's lamp while a player's character stands near, and every client draws it lit, then out after its hold 636ms