OwnedEntity
@irtio/client 5.0BrowsersNodeThe OwnedEntity interface of the Client API is an entity the local player owns: a ClientEntity whose writable fields the client can set. The client’s private state, room.private, is one, and so is each entity the room gives the player with Entities.setOwner().
EntityStore.mine() lists the entities of a kind the player owns, and onCorrect() passes one to its callback.
Instance properties
OwnedEntity has the properties of ClientEntity: id, kind and a property for each field. The fields the kind lists in writable can be set; the rest are read only.
Instance methods
OwnedEntity has the method of ClientEntity, toJSON().
Description
OwnedEntity<K> takes the entity’s kind and types its writable fields as writable, so setting any other field is a type error. A kind lists its writable fields in writable, as Declaring writable fields describes.
Writing fields
Setting a writable field checks the value, changes the client’s copy at once and queues the write for the room. Other clients see it once the room has applied it.
- A value of the wrong type throws a
TypeError, and a value out of its field’s range or over its size limit throws aRangeError. Both name the field. - Setting a field that isn’t writable, or a field of an entity the player no longer owns, throws a
TypeError. - Setting a field of an entity that has left the client’s view throws an
Error.
Sending writes
In a room with ticks, the client sends writes at most once per room tick, since the room applies them at the tick; until then the last write to a field wins. In a room without ticks, it sends them at the end of the task that made them, and during a burst, such as a drag that writes on every pointer move, at most once per WRITE_GAP (16 ms).
The client also sends them before each send() and call and before close(), so the room applies them before it handles the message, the call or the close. flush() sends them at once. Writes travel reliably, so over a real connection an unreliable message can arrive ahead of the writes made before it.
Corrections
The room has the last word. Its writes handlers can clamp or reject a write, and room code can write the field itself. When the room’s value differs from the owner’s, the owner gets a correction: the room’s value overwrites the client’s copy, the client drops its unsent writes to the field, and onChange() then onCorrect() run. Until then, the client keeps its own values for the writable fields it owns and ignores the room’s ordinary changes to them.
Examples
Saving a player’s settings in private state
The private state holds the player’s volume, which they set, and their coins, which only the room changes.
// settings.ts
import { connect, defineSchema, entity } from "@irtio/client";
const Settings = entity("settings", { volume: "u8", coins: "u32" }, { writable: ["volume"] });
const schema = defineSchema({ private: Settings });
const room = await connect("lobby", { schema });
export function setVolume(volume: number): void {
try {
room.private.volume = volume;
} catch (e) {
// A RangeError for a volume outside 0 to 255
console.warn("volume not set", e);
}
}
console.log(`${room.private.coins} coins`);
room.private.coins = 10 is a type error, and throws a TypeError if the types are bypassed: coins are the room’s to give.
See also
Runtime availability
| Browsers | Node | |
|---|---|---|
OwnedEntity | 5.0 | 5.0 |