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

Interact

Interact shows a prompt above an entity while one of this page’s own targets is near it, and fires a callback on a key press or a click. Its targets are, by default, a character a player controls now: the player’s character, not one the player let go of. Near means the target’s body overlaps a sphere of radius about the entity, the character’s capsule rather than its feet. Its targets prop widens or narrows who may press it, with the kinds, layers, teams and rules Trigger lists. A view reads the local player’s own character with useCharacter().

Its props are in InteractProps.

The prompt shows while the local player’s target is near, with the key that presses it, and the E key fires onInteract, except an E typed into a text field. One press fires one Interact, as Several in reach says. Where the camera walks the player’s character to a click, as the classic camera does, a click or a tap on the entity walks the character to where it stood. It fires onInteract once it comes within radius, stopping it there, as World of Warcraft’s click-to-move does with an NPC. A click on the ground, a steer, or a walk that ends short of the entity, because it walked on, cancels the press; a second click follows it. A callback is allowed here because Interact is client behaviour. Each press also adds the InteractedEvent event to the entity, which a system or an NPC’s routine reads. A door that opens when the player uses a key on it is an Interact whose press uses the key, as Locks and keys builds.

E is the engine action engineActions.interact, in the game’s key table like every other key. controls({ keys: { interact: ["KeyF"] } }) moves it, and a chord with Ctrl, Alt or ⌘ presses nothing, except under a Ctrl or an Alt the game binds itself.

Each press of E fires onInteract once, however long the player holds the key, so a game needs no guard of its own against a held key. A game that wants the key while it is held reads useActionHeld(engineActions.interact), or hears each of the keyboard’s repeats with useAction(engineActions.interact, { repeat: true, onPress }), as a page action does.

The prompt is white text with a dark shadow and the key in a box. A game draws it in its own look with className, which merges over those classes, as className does on Panel and Button. Juice Island’s prompt is a green speech bubble: <Interact prompt="Pick" className="island-prompt" />.

Where several Interacts are in reach, such as an orchard of close sockets or a dock beside a counter, the prompt shows over one of them, and the E key fires that one alone, as Roblox’s ProximityPrompt shows one prompt per key. The step that runs the rules chooses it for each player, and the player’s page shows that choice:

  1. Of every Interact the player’s page holds that one of the player’s entities stands near, the character or a kart the player drives, the nearest wins, measured from that entity’s feet. A bot has no page, so any Interact near it competes.
  2. An Interact within its own distanceTolerance of the nearest, 5 cm by default, still competes, and of those the one the player’s entity faces most wins.
  3. Of those the entity faces alike, the one the world holds first wins.

Raise distanceTolerance on an Interact that should win when the player’s entity faces it, such as a counter beside a row of crates: <Interact prompt="Serve" distanceTolerance={0.5} /> wins over a crate up to half a metre nearer behind the entity. A large tolerance on many close Interacts makes facing decide among all of them.

An Interact with exclusive={false} takes no part: it shows its prompt and fires on the key whenever the target is near, beside the chosen one, as Roblox’s AlwaysShow does. A click on an entity names it, so the walk the click starts fires that entity, chosen or not.

A game reads the choice from InteractChoiceTrait on the player entity: interactable, the Interact the player’s press fires, and presser, the player’s entity it goes through, each null where none is near. Only the player’s own page holds that choice.

Client. The prompt only changes what this client shows.

The room steps the entity and streams its trait, whose near says whether any target stands near. The room’s behaviours.choose writes each player’s InteractChoiceTrait each step, and the stream carries it to the player’s page alone. The player’s page shows the prompt over the Interact the room chose, or over an Interact that is not exclusive while one of the player’s own targets is near it, and the E key fires the onInteract its own scene passed. The choice changes only in a step, so a callback that moves or removes an Interact cannot make one press fire two. A press on one page fires nothing on another. In a room, the prompt moves to a new Interact once the room has the player’s move and its choice has come back, about one round trip after the character moves.

The press also reaches the room as the engine’s behaviours.interact command, which behaviours() declares, through the player’s entity the room chose, and clicked where a click sent it. The room adds InteractedEvent to its own copy of the entity when that entity is one of its targets and its body stands within radius plus 1 m, the metre covering the player’s page walking ahead of the room. It refuses a press from an entity that is no target not-a-target, one past that reach out-of-reach, and one on an entity of a match the player is outside of other-match. A key press on an exclusive Interact the room has not chosen for the player within the last second, which covers a press on its way as the choice changed, it refuses not-chosen. The room keeps the player’s choice before its last change, and the tick it changed, in PreviousInteractChoiceTrait, which a checkpoint keeps.

InteractedEvent carries by, the entities whose presses reached the entity in that step, so two players who press together each start their own conversation. A game played alone takes the same command, so its InteractedEvent names the player’s entity too. So an NPC’s talk routine runs on the room, and every page sees it through the stream. A system that needs to know who is near reads findOverlappingTargets(step, entity, { radius, targets }), the read Interact itself uses.

A game’s own system asks whether an entity stands within that reach with isWithinInteractReach(world, target, entity), as the NPCs’ conversations do.

import { Entity, Interact, Views, type EntityProps } from "@spawnite/engine";
export function Chest({
position,
onInteract,
}: Pick<EntityProps, "position"> & { onInteract: () => void }) {
return (
<Entity position={position}>
<Interact prompt="Open" onInteract={onInteract} />
<Views>
<mesh position={[0, 0.4, 0]}>
<boxGeometry args={[1, 0.8, 0.6]} />
<meshStandardMaterial color="saddlebrown" />
</mesh>
</Views>
</Entity>
);
}

An Interact whose targets name every controlled entity shows its prompt while the kart the player drives is near, and the press goes through the kart. The engine’s tests drive a kart to this dock and press it:

packages/engine/test/outside/LoadingDock.tsx
import {
Entity,
Interact,
TargetKind,
Views,
type EntityProps,
} from "@spawnite/engine";
/** A dock a player loads from the player's kart: its prompt shows while
* any entity the player controls is near, the kart the player drives as
* well as the player's character, and the press goes to the room
* through that entity. */
export function LoadingDock({
position,
onLoad,
}: Pick<EntityProps, "position"> & { onLoad: () => void }) {
return (
<Entity position={position}>
<Interact
prompt="Load"
radius={3}
targets={TargetKind.ControlledEntities}
onInteract={onLoad}
/>
<Views>
<mesh position={[0, 0.25, 0]}>
<boxGeometry args={[4, 0.5, 2]} />
<meshStandardMaterial color="slategrey" />
</mesh>
</Views>
</Entity>
);
}