Replay: kind() method
@irtio/client 5.0BrowsersNodeThe kind() method of the Replay interface returns one of the replay’s entity kinds by name, for a kind whose fields changed since the replay was recorded.
Syntax
kind(name)
Parameters
name- A string: the kind’s name, as
entity()declared it.
Return value
The kind, or undefined if the replay has no kind of that name.
Exceptions
None.
Description
With the schema passed to loadReplay(), a recorded kind with the same fields as the schema’s is the schema’s own kind object, so replay.entities.all(Ball) finds its entities. A kind you’ve changed since is read from the replay’s header instead, as a kind object of its own, and all() with your current kind finds nothing. kind() returns the replay’s kind of that name, which is your own kind when its fields match and the recorded one when they don’t, to pass to the entities store’s methods.
Its entities’ fields are the recorded ones, so read them by the names the recording had.
Examples
Playing a replay recorded before a field was added
A game added a spin field to its ball after some replays were recorded. The viewer looks the kind up by name, so it finds the balls of old and new replays alike.
// old-replays.ts
import { defineSchema, entity } from "@irtio/client";
import type { Replay } from "@irtio/client/replay";
export const Ball = entity("ball", { x: "f32", y: "f32", spin: "f32" });
export const schema = defineSchema({ entities: [Ball] });
export function ballPositions(replay: Replay): { x: number; y: number }[] {
const kind = replay.kind("ball") ?? Ball;
return [...replay.entities.all(kind)].map((b) => {
const { x, y } = b.toJSON() as unknown as { x: number; y: number };
return { x, y };
});
}
See also
loadReplay()Replay.entities- Playing back in Replays and clips
Runtime availability
| Browsers | Node | |
|---|---|---|
kind() | 5.0 | 5.0 |