Trigger
Trigger is a sphere of radius about its entity that meets its targets: by default, a character a player controls now. A character its player let go of, an NPC and an empty kart are not met. Its targets prop widens or narrows who it meets, as Who it meets shows. A view reads the local player’s own character with useCharacter().
Its props are in TriggerProps.
The sphere meets a target’s body by its shape: a character’s capsule, a kart’s colliders, or the point of an entity with no body. Each step, Trigger marks inside while any target’s body overlaps the sphere. It emits TriggerEnteredEvent for each target that comes in, and TriggerExitedEvent for each that leaves, stops being a target, or is destroyed. Each event carries the target entity as other and that entity’s dump key as otherDumpKey, so a kart’s event names the kart rather than the player. findController(world, other) reads the player who controls it, as Roblox’s GetPlayerFromCharacter does. readTriggerOccupants(world, trigger) reads who is inside now.
onEnter fires as the first of this page’s own targets comes inside, and onExit as the last of them leaves. This page’s own targets are the entities its player controls that targets names, such as the player’s character, or the player’s kart where targets names it. In a game played alone, they fire as the view hears each event, so a character who passes straight through between two rendered frames still fires both. A room works differently, as Where it runs says. A system queries the same events; a view hears them with useEvent.
The sphere samples where each body stands at each step, as a trigger collider does. A body that crosses it within one step is not met. Unity’s Physics.OverlapSphere and Godot’s Area3D meet bodies the same way. A trigger of another shape is a collider with trigger: true, as Bodies and colliders says. Give each its own entity: one entity’s events read as one volume, and a development world warns of an entity that holds both.
Who it meets
Section titled “Who it meets”targets names who the trigger meets, as a collision mask names the layers a body meets. It takes one entry or a list:
TargetKind.ControlledCharacters, the default: a character a player controls now, a bot’s included.TargetKind.ControlledEntities: anything a player controls now, such as their character, a kart the player drives or the player’s drone.- A collision layer, such as
characters.collisionLayers.npcs: anything whose body stands in it, controlled or not. - A team, a handle that
defineTeamreturns, as Teams says: keeps the targets on that team. A kart counts on the team of the player who drives it. - A rule, which
defineTargetRuledeclares at module scope: keeps the targets its test accepts. TargetKind.None, alone: meets nobody. A list that holds it, and an empty list, throw in a development world and meet nobody in a deployed one.
Kinds and layers add who it meets: any one of them lets a candidate in. Teams and rules narrow them: a target stays on any listed team, kept by every rule. A list with no kind or layer narrows the default, so targets={[red]} meets the characters red’s players control. The same targets prop goes on Chase, Pickup, Interact and Loot, and this section is the one that lists it.
A rule’s test takes the world, the behaviour’s entity and the candidate, and returns whether it counts. It runs for each candidate on each step, so a trait the game changes this step counts at once. It runs on the room, and on the local player’s page too for Trigger’s callbacks and Interact’s prompt, where it reads what the local player’s page holds. So a rule that reads a trait the room never streams makes the page disagree with the room. A rule that reads other entities queries them as a system does, with readEach.
A system asks the same question with the same targets:
isTarget(world, entity, candidate, { targets })for one candidate.findNearestTarget(world, entity, { targets })for the nearest.findOverlappingTargets(step, entity, { radius, targets })for every target inside a sphere now, with no events.
A callback is allowed here because Trigger is client behaviour: it changes what the page shows, such as a banner or a sound, and never decides an outcome. A gate that wins the match, a door that takes a key or a trap that deals damage is a system, as the following section shows. A door the player opens with a key they carry is pressed rather than walked into: Locks and keys builds one with Interact.
Decide an outcome in a system
Section titled “Decide an outcome in a system”onEnter runs on a page. In a room it runs on the page of the player who crossed, and the room never hears it, so a callback that wins the match wins it on that page alone. An outcome belongs in a system, which runs on the room and in a game played alone.
A system reads TriggerEnteredEvent for each target that comes in. A rule in the trigger’s targets decides who counts, so the event fires for that target alone. A rule reads the trigger’s own entity, so one rule serves every trigger that carries its settings.
The following exit gate wins the match when a player walks in holding three keys. Its rule keeps a character whose bag holds the gate’s keys. countItem(character, item) counts what the character’s bag holds, as Inventory says, and endMatch ends the match in a victory, as Match says. The gate’s own trait carries the keys it wants, through a behaviour of the game’s own, as Behaviours describes. A player who picks up their last key inside becomes a target then, and enters the gate then. The room’s tests mount the gate on a room two players join, stand one player with two keys in it, then walk in a second with three:
import { defineBehaviour, definePlugin, defineTargetRule, defineTrait, Entity, readEvents, RunContext, Trigger, TriggerEnteredEvent, useBehaviour, Views, type AuthoritativeStep, type EntityProps, defineSystem, Phase,} from "@spawnite/engine";import { countItem, inventory } from "@spawnite/engine/inventory";import { requireMatch } from "@spawnite/engine/core";import { endMatch, Match, matches, MatchOutcome,} from "@spawnite/engine/matches";
/** The keys a player must hold as the character walks * into the gate. */export const GateTrait = defineTrait("exitGate", { keys: 3, item: "key" });
const GateBehaviour = defineBehaviour({ name: "exitGate", trait: GateTrait, runsOn: RunContext.Server, description: "Wins the match when a player walks in holding its keys.",});
/** Keeps a character who holds the gate's keys, by its own trait: as a * rule alone in a list, it narrows the default, a character a player * controls now. One who picks up its last key inside counts * from then, and enters the gate then. */const keyHolders = defineTargetRule("keyHolders", (_world, gate, candidate) => { const wanted = gate.get(GateTrait); return ( wanted !== undefined && countItem(candidate, wanted.item) >= wanted.keys );});
// On the room, which alone decides the match, from the bags it keeps. The// gate's Trigger meets key holders alone, and enters each as the// character comes in, so a player with the keys who joins one without// them still wins.function winMatchesAtGates(step: AuthoritativeStep) { for (const { entity: gate, record } of readEvents( step, TriggerEnteredEvent, )) { if (!gate?.has(GateTrait) || !record.other) continue; endMatch(step, requireMatch(step.world, record.other), { outcome: MatchOutcome.Victory, }); }}
export const exitGates = definePlugin(() => ({ name: "exit-gates", requires: [matches, inventory], systems: [ defineSystem({ name: "finish", phase: Phase.Rules, run: winMatchesAtGates, }), ],}));
interface ExitGateProps extends Pick<EntityProps, "position"> { /** The keys the * player must hold. */ keys: number;}
function GateLock({ keys }: Pick<ExitGateProps, "keys">) { useBehaviour(GateBehaviour, { keys, item: "key" }); return null;}
export function ExitGate({ position, keys }: ExitGateProps) { return ( <Entity position={position}> <Trigger radius={1.5} targets={[keyHolders]} /> <GateLock keys={keys} /> <Views> <mesh> <torusGeometry args={[1.2, 0.2, 16, 32]} /> <meshStandardMaterial color="#8a7cff" /> </mesh> </Views> </Entity> );}
/** The gate and the match it wins. The match has no score to win and no * time limit, so only the gate ends it. */export function ExitMatch() { return ( <> <Match /> <ExitGate position={[0, 0, -6]} keys={3} /> </> );}ExitMatch mounts the gate beside a bare <Match />, with no scoreToWin and no timeLimitSeconds, so the gate is the one way to win it. List exitGates in the game’s plugins after matches() and inventory(). Its system, winMatchesAtGates, runs on the room alone, since it names no runsOn, and in a game played alone. <Trigger> gives the gate its radius, and its onEnter can still show a banner on the page, as Show something on the page does.
Count a race’s laps
Section titled “Count a race’s laps”A <LapLine /> beside a trigger in one <Entity> makes it a race’s finish line, with no system of the game’s own. Each kart that passes through it forward counts a lap of the match for the player who drives it, and one driven back through it owes a lap. A kart race’s line is a box collider with trigger: true across the road, and a Trigger’s sphere meets karts with targets={TargetKind.ControlledEntities}. Count laps on the Match page says how a line counts, and shows a three-lap kart race in a room. Ordered checkpoints, where a lap counts only after each one in order, are a system of the game’s own that reads TriggerEnteredEvent, as the exit gate above does.
Where it runs
Section titled “Where it runs”Client. The callbacks only change what this client shows.
In a room, the room steps the entity and streams its trait, whose inside marks any target inside, and its TriggerEnteredEvent and TriggerExitedEvent, one of each per target. Each page’s Trigger ignores the events for its callbacks. Each frame, it tests its own entities that targets names against the sphere, the local character’s predicted capsule among them, so onEnter fires at once. Another player walking through fires nothing on this page. An entity that passes straight through between two frames on a page in a room fires neither callback. A system on the room reads the events, or readTriggerOccupants for who is inside. A view asks isTouching(world, character, { other: trigger }) for the local player’s own character.
Show something on the page
Section titled “Show something on the page”A callback changes what this page shows and nothing else. The following place name appears over the old mill while this page’s character stands within 4 metres of it, and goes as it leaves. No other page sees it, the room never hears it, and the match goes on. The engine’s tests step a character in and out of the mill and read the name off the page:
import { useState } from "react";import { Anchor, Entity, Panel, PanelVariant, Text, Trigger, Views,} from "@spawnite/engine";
export function OldMill() { const [isNear, setIsNear] = useState(false);
return ( <Entity position={[0, 0, -10]}> <Trigger radius={4} onEnter={() => setIsNear(true)} onExit={() => setIsNear(false)} /> {isNear && ( <Panel anchor={Anchor.Above} variant={PanelVariant.Glass}> <Text>The old mill</Text> </Panel> )} <Views> <mesh> <boxGeometry args={[3, 4, 3]} /> <meshStandardMaterial color="#b08860" /> </mesh> </Views> </Entity> );}