RoomEntity

Available since @irtio/room 0.1Hosted roomsirt dev@irtio/testingSelf-hosted room host

The RoomEntity interface of the Room API is one entity of a room, read and written in place: an entity from room.entities, the shared state room.state, or a player’s private state from room.private(). It has a property for each field of its kind, and id, kind and patch().

RoomEntity<K> takes the entity’s kind, typeof what entity() returned, and types each field property with its field’s type. It’s the same type as the WorldEntity a stepper’s world holds, on the room and in a client’s prediction.

Room code never constructs a RoomEntity. Entities.create() makes one, and the room makes the shared and private state.

Field values

Each field of the kind is a property of the entity. Reading it gives the current value. Setting it checks the value and records the change, which goes to the clients that see the entity in the next state update.

  • Setting a field to the value it holds already changes nothing and sends nothing.
  • A value of the wrong type, or a number that isn’t an integer for an integer field, throws a TypeError. A number out of its field’s range, or a value over its size limit, throws a RangeError. Both name the field.
  • A write that would make the entity over 61,376 bytes encoded throws a RangeError; split the data across several entities.
  • f32 and angle values are rounded with Math.fround, so reading back gives the rounded number.
  • A quat reads as a frozen { x, y, z, w }. A write takes any object with those finite numbers and is scaled to length 1; one of length 0 throws a RangeError.
  • A bytes write keeps a copy of the array, and a json write keeps a copy of the value, so changing your object afterwards changes nothing. Changing the value a field holds in place records no change either: assign a new value, or for bytes use patch().
  • The key field of a kind with a key is fixed for the entity’s life: setting it throws a TypeError.
  • Writing a field of a deleted entity throws an Error. Reading it still gives the values it had.

For the field types and their limits, see Declaring kinds.

Defaults

A field left out of create(), and every field of new shared and private state, starts at its default:

Field type Default
bool false
numbers and angle 0
string, string(max) ""
bytes an empty Uint8Array
json null
json<T>(initial) a copy of initial, its own for each entity
quat the identity rotation, { x: 0, y: 0, z: 0, w: 1 }

id

The read-only id property is the entity’s ID, a number: 0 for the shared state, 1 for private state, and from 2 for entities. It stays the same for the entity’s life, across hibernation, and is how Entities.get() and a stepper’s world.get() find it.

kind

The read-only kind property is the entity’s kind, the object entity() returned. Compare it with your kinds to tell what an entity from get(id) is; kind.name is the kind’s name.

patch()

The patch() method writes bytes into a bytes field at an offset, and clients get only the bytes that changed, not the whole field.

patch(field, offset, bytes)
field
The name of one of the kind’s bytes fields.
offset
Where to write, a whole number of bytes from the start of the field.
bytes
A Uint8Array to write there.

It returns nothing. A write past the end of the field grows it, filling any gap with zeros, up to the 48 KB a bytes field holds. Writing bytes the field holds already changes nothing.

The changed ranges go out in the next state update. When the ranges patched since the last update add up to half the field or more, the whole field goes instead.

It throws:

TypeError
If field isn’t a bytes field of the kind, or bytes isn’t a Uint8Array.
RangeError
If offset isn’t a whole number, or the write would end past 48 KB, or the grown field would make the entity over 61,376 bytes encoded.
Error
If the entity was deleted.

Examples

Painting pixels on a shared canvas

// canvas.ts
import { defineRoom, defineSchema, entity } from "@irtio/room";

const SIZE = 64;
const Canvas = entity("canvas", { pixels: "bytes", painted: "u32" });

export default defineRoom({
  schema: defineSchema({ state: Canvas }),
  onCreate(room) {
    room.state.pixels = new Uint8Array(SIZE * SIZE);
  },
  onMessage(room, _connection, message) {
    const m = message as { x?: unknown; y?: unknown; color?: unknown } | null;
    if (typeof m?.x !== "number" || typeof m.y !== "number" || typeof m.color !== "number") return;
    if (![m.x, m.y].every((v) => Number.isInteger(v) && v >= 0 && v < SIZE) || !Number.isInteger(m.color) || m.color < 0 || m.color > 255) return;
    room.state.patch("pixels", m.y * SIZE + m.x, new Uint8Array([m.color]));
    room.state.painted += 1;
  },
});

See also

Runtime availability

Hosted roomsirt dev@irtio/testingSelf-hosted room host
RoomEntity0.10.10.10.1