Loot
Loot puts a stack of an item on an entity in the world. A target whose body comes within its radius takes it into its bag, and whatever no bag can take stays on the ground. 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, with the kinds, layers, teams and rules Trigger lists. A view reads the local player’s own character with useCharacter().
Its props are in LootProps.
How it looks
Section titled “How it looks”Loot draws the model its item’s definition names in model. Where the definition names none, it draws a small glowing gem in the definition’s color, white unless the definition says otherwise, so an item needs no model to be seen. The gem is the view the plugin registers for LootTrait with defineTraitView, which an entity draws where nothing names its view, so a view on the entity replaces it and view={false} draws none. Headless it draws nothing. Minecraft draws a lying item as its own icon, and Roblox draws a dropped Tool’s handle; Unity, Unreal and Godot draw what the game gives them.
Taking it
Section titled “Taking it”Each step, Loot meets every target whose body overlaps the sphere of radius about its entity, the target’s capsule rather than its feet. It adds its stack to the nearest target’s bag, as addItem does: into the stacks it can merge with first, then empty slots. Where the target’s bag is full, or it has none, it offers the rest to the next target inside, so a full bag never holds it from another. Its count drops by what the bags took, and the entity is destroyed when none is left. Loot in a Match is taken by its members alone.
Loot and Pickup share the same reach. Pickup stays for coins, which go into the wallet.
Loot for one team
Section titled “Loot for one team”A team in targets narrows who may take it. The following cache gives its arrows to red’s players alone, and a blue player standing on it takes none. The engine’s tests stand a blue player on it, then a red one:
import { Entity, type EntityProps } from "@spawnite/engine";import { Loot } from "@spawnite/engine/inventory";import { defineTeam } from "@spawnite/engine/teams";
/** The red team, which the game lists in `teams({ teams: [red, blue] })`. */export const red = defineTeam("red", { name: "Red", color: "#e5484d" });
/** A cache of arrows only red's players take: a team in `targets` narrows * the default, a character a player controls now, to red's. */export function RedCache({ position }: Pick<EntityProps, "position">) { return ( <Entity position={position}> <Loot item="arrow" count={20} targets={[red]} /> </Entity> );}After a reload
Section titled “After a reload”Loot comes back by default: each load lays the scene as it is written, so every loot lies again, whole. Give a loot an id and it stays taken instead: the save keeps what is left of it under loot, by id, and a reload lays only that, or nothing once it is all taken.
import { Entity } from "@spawnite/engine";import { Loot } from "@spawnite/engine/inventory";
export function MeadowLoot() { return ( <> {/* TakenEvent once, gone for good. */} <Entity position={[3, 0, -2.5]}> <Loot id="meadow-potions" item="potion" count={3} /> </Entity> {/* Back at every load. */} <Entity position={[8, 0, 1]}> <Loot item="herb" /> </Entity> </> );}The id names this loot in the save, so it has to be unique in the game, and it has to stay the same between versions of the game, or a player’s taken loot lies again. Two loot mounted at once with one id throw in development, naming the id. inventory.saves.loot, on by default, keeps what is left of each in the world’s record, so a game writes no code for it. A flower picked on one island stays gone there and lies on every other. The counts live on the world, so a save written from any scene keeps the loot taken in the others. A room that holds no world, such as a hub, keeps none, as Worlds says. The place_loot tool takes an id too.
No engine keeps this for you. Unity, Unreal, PlayCanvas and Roblox rebuild a level as authored, and the game saves the ids of taken objects itself; Godot saves only the nodes in its “Persist” group. Zelda: Breath of the Wild marks each object with its own revival policy, and Skyrim refills a whole area after days away. Loot takes the per-object choice, and makes the id the switch, since a stable name is what the save needs anyway.
Dropping it
Section titled “Dropping it”dropItem(authority, holder, { slot, count }) takes count copies out of the holder’s bag slot slot, on the world the authority decides outcomes on. It spawns an entity with Loot in front of the holder, carrying the stack’s data and the item’s model. The drop lands clear of the holder’s capsule and of the loot’s radius: 1.5 metres from the feet of a character of the default size. That is past a loot’s default radius of 1 metre, so the holder does not take it back on the next step. It returns the new entity, or nothing when the slot is empty or holds fewer than count. A page in a room sends inventory.commands.drop instead.
An item’s definition names the model it is drawn as in the world with model, a name registered with registerModel. A dropped copy is drawn as that model, or as the gem in the item’s color where it names none, as a Loot in the scene is. In a room, the engine draws it from the stream, unless Game sets replicas={false}. Playing alone, the engine also draws every entity that gameplay spawns with a model, such as a weapon’s projectile. Roblox, Unity, Unreal and Godot draw a spawned object whether or not a network is up.
Where it runs
Section titled “Where it runs”Server. Loot has a consequence: it changes the bag of whoever takes it.
In the AI tree, a loot entity is its item’s display name and count, Potion ×3, with the action “take it”.
What other engines do
Section titled “What other engines do”Minecraft drops an item as its own entity carrying the stack, merges nearby stacks, and waits two seconds before anyone can pick it up. Unreal’s Lyra spawns a collectable actor that holds item definitions or instances. Roblox moves the Tool itself into the workspace, and a touch collects it. Loot takes Minecraft’s item entity and leaves out its pickup delay, its despawn timer and the merging of stacks on the ground.
import { Entity } from "@spawnite/engine";import { Loot, defineItem } from "@spawnite/engine/inventory";
defineItem("potion", { displayName: "Potion", color: "crimson", maxStack: 10,});
export function PotionPile() { return ( <Entity position={[4, 0, -2]}> <Loot item="potion" count={3} /> </Entity> );}