EntityStore

Available since @irtio/client 5.0BrowsersNode

The EntityStore interface of the Client API holds the entities a client sees: it finds them by ID, key or kind, lists the ones the local player owns and runs callbacks as they enter, change, get a new owner and leave. Each client has one, as room.entities.

Game code never constructs an EntityStore. The client makes one when it’s constructed, and a replay has one too, as replay.entities on a Replay.

Instance properties

EntityStore.count
The number of entities the client holds, the shared state, private state and presence entries included. Read only.

Instance methods

EntityStore.get()
Returns the entity with an ID, or the entity of a kind with a key.
EntityStore.all()
Returns an iterator over the entities of a kind in the client’s view.
EntityStore.mine()
Returns an iterator over the entities of a kind the local player owns.
EntityStore.ownerOf()
Returns the player who owns an entity, or undefined for the room.
EntityStore.onEnter()
Calls a function when an entity of a kind enters the client’s view.
EntityStore.onChange()
Calls a function when fields of an entity of a kind change, with the fields that changed.
EntityStore.onCorrect()
Calls a function when the room overrules the local player’s writes to an entity of a kind.
EntityStore.onOwner()
Calls a function when an entity of a kind gets a new owner.
EntityStore.onLeave()
Calls a function when an entity of a kind leaves the client’s view.

Description

The store holds every entity the room sends the client: the shared state as entity 0, its private state as entity 1, a presence entry for each player in the room, and the room’s entities in the client’s view. Each is a ClientEntity, and each the local player owns is an OwnedEntity too. Entities of a hidden kind never arrive.

The client applies a state update whole, then runs the callbacks in the order the changes happened: leaves, enters, changes, owner changes and corrections. The callbacks of room.presence run next, then the client’s update event.

On a change of the room’s generation, or a reconnect the room can’t resume, every entity leaves and the full state enters again as new objects. On a reconnect the room resumes, the entities that changed while the client was away and every entity the player owns are new objects, so owner writes not yet sent when the connection dropped are dropped. Look entities up again after the open event, or keep them from onEnter(), rather than holding on to them.

Examples

Keeping a scene in step with the room

A board game adds a sprite for each piece that comes into view, moves it when the piece moves and removes it when the piece leaves.

// board.ts
import { connect, defineSchema, entity } from "@irtio/client";

const Piece = entity("piece", { x: "u8", y: "u8", color: "u8" });
const schema = defineSchema({ entities: [Piece] });

const room = await connect("board-1", { schema });
const sprites = new Map<number, { x: number; y: number }>();

room.entities.onEnter(Piece, (piece) => sprites.set(piece.id, { x: piece.x, y: piece.y }));
room.entities.onChange(Piece, (piece) => sprites.set(piece.id, { x: piece.x, y: piece.y }));
room.entities.onLeave(Piece, (piece) => sprites.delete(piece.id));

// Pieces already in view when connect() resolved
for (const piece of room.entities.all(Piece)) sprites.set(piece.id, { x: piece.x, y: piece.y });

connect() resolves once the first state update has been applied, so the pieces in it entered before the callbacks were added. The loop at the end picks them up.

See also

Runtime availability

BrowsersNode
EntityStore5.05.0