RoomClient: entities property

Available since @irtio/client 5.0BrowsersNode

The entities read-only property of the RoomClient interface is the client’s EntityStore: every entity the client holds, with callbacks for when they enter, change and leave.

Value

An EntityStore. The same store holds the entities across reconnects.

Description

The store holds the shared state, the player’s private state, a presence entry for each player in the room and the room’s entities in the player’s view, as the room’s interest decides it. Entities of a hidden kind never arrive.

The client applies each state update to the store whole, then runs the store’s callbacks: leaves, enters and changes in the order they happened, then the update event. On a reconnect that can’t resume, or a change of generation, every entity leaves and the full state enters again.

Examples

Drawing the coins in view

The room’s coins enter and leave the player’s view as they move, and the client adds and removes a sprite for each.

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

const Coin = entity("coin", { x: "f32", y: "f32" });
const room = await connect("main", { schema: defineSchema({ entities: [Coin] }) });

const sprites = new Map<number, { x: number; y: number }>();
room.entities.onEnter(Coin, (coin) => sprites.set(coin.id, { x: coin.x, y: coin.y }));
room.entities.onChange(Coin, (coin) => sprites.set(coin.id, { x: coin.x, y: coin.y }));
room.entities.onLeave(Coin, (coin) => sprites.delete(coin.id));

See also

Runtime availability

BrowsersNode
entities5.05.0