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

Pickup

Pickup turns an entity into a coin: the nearest of its targets whose body reaches it takes its reward, and the coin is removed for good in the same step. Its targets are, by default, a character a player controls now: not one the player let go of, not an NPC and not an empty kart. Its targets prop widens or narrows who may take it, as Who takes a pickup shows. The reward goes into the wallet of the player behind the taker, the player who controls it. A game that keeps the wallet on the entity, as Keep coins across scenes and visits shows, pays the character itself. Nothing brings a taken pickup back while its scene runs; a thing that comes back after it is taken is a behaviour of your own, as Write your own behaviour shows how to build. Put <Pickup> inside an <Entity>. The example template’s coin:

games/example/src/components/Coin.tsx
import {
Bob,
box,
Entity,
Pickup,
type Position,
RigidBodyType,
sounds,
Spin,
} from "@spawnite/engine";
/** One coin: it turns, bobs, and goes into the wallet of whoever reaches it,
* with a chime.
* Its body follows the bob, so the ball bounces off it where it is drawn. */
export interface CoinProps {
position: Position;
}
export function Coin({ position }: CoinProps) {
return (
<Entity
position={position}
body={{ bodyType: RigidBodyType.Kinematic }}
collider={{ shape: box({ size: [0.8, 0.1, 0.8] }) }}
view={CoinView}
>
<Spin speed={2} />
<Bob height={0.2} />
<Pickup reward={1} sound={sounds.pickup} />
</Entity>
);
}
/** How a coin is drawn on a page: a gold disc. A room never mounts it. */
function CoinView() {
return (
<mesh>
<cylinderGeometry args={[0.4, 0.4, 0.1, 24]} />
<meshStandardMaterial color="gold" />
</mesh>
);
}

Pickup needs no body and no collider of its own. This coin has a kinematic body and a box collider only so that the example’s ball bounces off it, and Pickup never meets its own entity’s body.

Pickup takes four props, listed in PickupProps:

  • reward: the coins it pays. A fraction is allowed. A pickup with a reward of 0 is taken and pays nothing, and a reward below 0 throws in the step that would pay it, as addCoins does.
  • radius: how far from the entity a target’s body must come, in metres. 1 when left out.
  • targets: who may take it, as Trigger lists them. A character a player controls now when left out.
  • sound: a file, or a ?url import, played once as the pickup is taken, as Sound plays one. It plays nothing before the player’s first input, and a headless run, such as spawnite simulate, never loads it. The engine ships a chime as sounds.pickup.
  • The behaviours() plugin in the game’s plugins list in src/game.ts. The example template lists it. A scene that places a Pickup in a game without it throws as it loads, with behaviours() is off in this world: add behaviours() to plugins in src/game.ts.
  • A target to take it: by default, a character a player controls now. A view reads the player’s own character with useCharacter().

Each step, Pickup meets every target whose body overlaps the sphere of radius about its entity, as Trigger does, and gives the pickup to the nearest:

  • A target is met by its shape: a character’s capsule, from its feet to its head, a kart’s colliders, or the point of an entity with no body. A coin 1.5 m above the ground is in reach of a character who walks under it, since the character’s capsule reaches up to it.
  • Where several targets are in reach, the one whose feet stand nearest takes the whole reward. A tie goes to the entity made first.
  • Pickup meets only its targets. A downed character takes one, as does any target. For a coin only some characters may take, give it targets: a team, such as targets={[red]}, or a rule defineTargetRule declares.
  • A pickup in a Match is taken by its members alone.

Use Pickup for coins. For an item that goes into the bag, use Loot, which measures reach the same way. For something that happens when a character reaches a place, use Trigger.

A coin on a kart track meets every entity a player controls now, so the kart the player drives takes it and the player’s wallet gains the coin. An empty kart rolling through takes none. The engine’s tests drive one kart through it and roll an empty one through another:

packages/engine/test/outside/KartCoin.tsx
import {
Entity,
Pickup,
Spin,
TargetKind,
Views,
type EntityProps,
} from "@spawnite/engine";
/** A coin on a kart track: a kart the player drives takes it as well
* as the player's character, and the coins go to the player's
* wallet. An empty kart rolling through takes none. */
export function KartCoin({ position }: Pick<EntityProps, "position">) {
return (
<Entity position={position}>
<Pickup
reward={1}
radius={1.5}
targets={TargetKind.ControlledEntities}
/>
<Spin speed={3} />
<Views>
<mesh>
<cylinderGeometry args={[0.6, 0.6, 0.1, 24]} />
<meshStandardMaterial color="gold" />
</mesh>
</Views>
</Entity>
);
}

In the step a character takes it, Pickup does the following, in order:

  1. Pays reward with addCoins into the wallet of the player behind the taker, the player who controls it, such as their character or a kart the player drives, unless the game keeps the wallet on the entity, and then into the taker’s own. A taker no player stands behind, such as a character no player plays, takes the coins into a wallet of its own, which addCoins gives it where it has none. While a Match plays, the coin counts toward the round’s score.
  2. Emits the TakenEvent event on the pickup entity. A pickup the scene removes untaken emits none, so it plays no sound.
  3. Destroys the entity.

The sound plays as the game hears TakenEvent, at the effects volume and as loud wherever the player stands: it is not positional. In a room, every player hears every pickup, whichever player took it.

Coins and the wallet covers reading the coins, spending them and keeping them in the save.

Pickup changes a wallet, so it runs in the room, or in the game itself when it plays alone. A client, the game running in one player’s browser, never runs it in a room. The client draws the coin until the room’s update removes it, and the same update carries TakenEvent, which the client hears before it removes the coin.

The room takes the coin when its own copy of the character reaches it. A client draws the player’s character ahead of the room, so on the player’s screen the coin goes about one round trip to the room after the character touched it, plus a few ticks (an estimate from how prediction works).

A “+1” or a burst of sparks where a coin is taken plays in a view that hears the coin’s TakenEvent. The scene must mount <FloatingText /> for the text, and <Particles effects> holding the sparks effect:

packages/engine/test/outside/Coin.tsx
import {
Entity,
Pickup,
TakenEvent,
useEntity,
useEvent,
useFloatingText,
useParticles,
Views,
type ParticleEffect,
type Position,
} from "@spawnite/engine";
export interface CoinProps {
position: Position;
/** The burst where the coin is taken, one of the scene's
* `<Particles effects>`. */
sparks: ParticleEffect;
}
export function Coin({ position, sparks }: CoinProps) {
return (
<Entity position={position}>
<Pickup reward={1} />
<CoinTaken position={position} sparks={sparks} />
<Views>
<mesh>
<cylinderGeometry args={[0.4, 0.4, 0.1, 24]} />
<meshStandardMaterial color="gold" />
</mesh>
</Views>
</Entity>
);
}
// Under the coin's Entity, so `useEntity()` is this coin. Without
// `{ entity }` the view hears every coin's TakenEvent, and each coin
// would burst whenever any coin is taken.
function CoinTaken({ position, sparks }: CoinProps) {
const floatingText = useFloatingText();
const particles = useParticles();
useEvent(
TakenEvent,
() => {
floatingText.show({ at: position, text: "+1", color: "gold" });
particles.spawn(sparks, { at: position, color: "#ffd166" });
},
{ entity: useEntity() },
);
return null;
}

Three parts of it matter:

  • The view sits under the coin’s <Entity> and passes { entity: useEntity() } to useEvent. Without it, every coin’s view hears every coin’s TakenEvent, and each coin plays its effect whenever any coin is taken.
  • TakenEvent carries no data. Who took the coin is in the wallet, not in the event.
  • The view takes the place from the coin’s position prop, not from the entity. In a game played alone, the entity is already destroyed when the view hears the event. In a room, the client keeps the removed coin until its step and its views have read the event, then removes it. The prop gives the same place in both.

The engine’s test of this file mounts three coins, takes one, and checks that a single “+1” shows.

The example game’s run scene places three coins in a line ahead of the character. The following command runs the scene headless, holds W, and stops as soon as any wallet holds a coin:

Terminal window
spawnite simulate --scene run --keys w --until "Object.values(entities).some((e) => e.wallet?.coins > 0)" --fields wallet

In the example game on 2026-10-05, it printed The condition held on step 28; it returned true., then the player’s wallet with "coins": 1. The run joins a player, who holds the wallet from the start with 0 coins, so the condition holds on the step the first coin is taken. Where no coin is taken within the default budget of 10 s of game time, the command says the condition did not hold and exits 1.

  • One character takes each pickup, and takes the whole reward.
  • A taken coin comes back each time its scene loads: Pickup keeps nothing in the save, and the scene places every coin it holds again. A Loot with an id stays taken across loads.
  • The sound is the same everywhere in the world, and in a room every player hears every pickup.