PlayerData: get() method
@irtio/room 0.1Hosted roomsirt dev@irtio/testingSelf-hosted room hostThe get() method of the PlayerData interface resolves with a key’s value and version, or undefined when the key has none.
Syntax
get(key)
get<T>(key)
Parameters
key- The key, 1 to 64 of
A-Z a-z 0-9 _ . : -.
Return value
A Promise that resolves with { value, version }, or undefined. value is JSON or a Uint8Array; the type parameter T types it, without checking it.
Exceptions
The promise rejects with a DataError, such as E_INVALID for a malformed key or E_UNAVAILABLE when the data service can’t be reached.
Description
A get() within 5 seconds of the room’s last read or write of the same key is answered from memory, with no call; a hit doesn’t extend those 5 seconds. Writes by other rooms, clients or your backend show within 5 seconds, or at once after refresh(). Each get() resolves with its own copy of the value.
Load what a player needs once, in onJoin, rather than calling get() every tick.
Examples
Applying a player’s chosen skin
// skins.ts
import { defineRoom, defineSchema, entity } from "@irtio/room";
const Avatar = entity("avatar", { owner: "string", skin: "string" }, { key: "owner" });
export default defineRoom({
schema: defineSchema({ entities: [Avatar] }),
async onJoin(room, connection) {
const skin = await room.playerData(connection.player).get<string>("skin");
const avatar = room.entities.get(Avatar, connection.player) ?? room.entities.create(Avatar, { owner: connection.player });
avatar.skin = skin?.value ?? "default";
},
});
See also
Runtime availability
| Hosted rooms | irt dev | @irtio/testing | Self-hosted room host | |
|---|---|---|---|---|
get() | 0.1 | 0.1 | 0.1 | 0.1 |