EntityStore: mine() method

Available since @irtio/client 5.0BrowsersNode

The mine() method of the EntityStore interface returns an iterator over the entities of a kind the local player owns. Their writable fields can be set.

Syntax

mine(kind)

Parameters

kind
A kind, the object entity() returned.

Return value

An iterator over the kind’s entities the local player owns, each an OwnedEntity. It’s empty before the client is first ready, and for a kind the player owns none of.

Exceptions

None.

Description

The room decides who owns an entity, with Entities.setOwner(), and a player always sees the entities it owns, whatever the room’s interest says. In a room with room code, a client can’t take or give up ownership: it asks the room with a message or a call.

Setting a writable field of an owned entity changes the client’s copy at once, and the client sends the write to the room. Setting any other field throws a TypeError. See OwnedEntity for when writes are sent and how the room corrects them.

Examples

Moving the player’s cursors with the pointer

The room gives each player a cursor whose position is writable. The client moves every cursor it owns as the pointer moves.

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

const Cursor = entity("cursor", { x: "f32", y: "f32", color: "u8" }, { writable: ["x", "y"] });
const schema = defineSchema({ entities: [Cursor] });

const room = await connect("whiteboard", { schema });

addEventListener("pointermove", (e) => {
  for (const cursor of room.entities.mine(Cursor)) {
    cursor.x = e.clientX;
    cursor.y = e.clientY;
  }
});

color isn’t writable, so cursor.color = 2 is a type error, and throws a TypeError if the types are bypassed.

See also

Runtime availability

BrowsersNode
mine()5.05.0