DataClient: playerData property

Available since @irtio/client 5.0BrowsersNode

The playerData read-only property of the DataClient interface is the data of the player the client is, kept per project across every room.

Value

A PlayerData, with the Room API’s get(), set(), increment(), delete(), list() and refresh() methods, for this player only.

Description

A client reads every key of its own player’s data, and writes only the keys in the project’s data.player.clientWrite; a write to any other key rejects with a DataError with code E_FORBIDDEN. Writes from a client can’t use once, which rejects with E_INVALID, but version works as in room code.

Nothing is cached on the client, so every get() reads from the router and refresh() has nothing to drop. Without a token, the first call gets the client a guest; see dataClient().

Examples

Counting games played

The client counts the player’s finished games in one step, which needs gamesPlayed in data.player.clientWrite.

// games-played.ts
import { dataClient } from "@irtio/client";

const data = dataClient();

export async function finishGame() {
  const played = await data.playerData.increment("gamesPlayed");
  console.log(`games played: ${played}`);
}

A count that matters, such as one that unlocks a reward, belongs in room code, since a client can write any number to a key it may write.

See also

Runtime availability

BrowsersNode
playerData5.05.0