RoomClient: playerData property
@irtio/client 5.0BrowsersNodeThe playerData read-only property of the RoomClient interface is the data of the player the client connected as, kept across every room of the project.
Value
A PlayerData for this player, as dataClient() gives it, made with the client’s token or guest.
Description
Its calls go over the router’s HTTP API, not the room connection, so they work while the client reconnects and before it’s ready. Nothing is cached on the client; every call goes to the router.
A client reads its own player data, and writes only the keys the project’s data.player.clientWrite lists. Its writes can’t use once, and refresh() has nothing to drop. Calls reject with a DataError: E_UNAVAILABLE when the router or data service can’t be reached, E_LIMIT over the router’s 10 calls a second per address and per player, E_FORBIDDEN for a key the client can’t write or a rejected token, and E_INVALID for a write with once.
Examples
Saving the player’s settings
A settings menu saves the player’s volume to player data and loads it back. Saving returns false when the data service can’t be reached.
// settings.ts
import { connect, DataError } from "@irtio/client";
const room = await connect("main");
export async function saveVolume(volume: number): Promise<boolean> {
try {
await room.playerData.set("settings", { volume });
return true;
} catch (e) {
if (e instanceof DataError && e.code === "E_UNAVAILABLE") return false;
throw e;
}
}
export async function loadVolume(): Promise<number> {
const saved = await room.playerData.get<{ volume: number }>("settings");
return saved?.value.volume ?? 1;
}
See also
Runtime availability
| Browsers | Node | |
|---|---|---|
playerData | 5.0 | 5.0 |