Replay
@irtio/client 5.0BrowsersNodeThe Replay interface plays a replay or clip into an EntityStore a tick at a time, with the same events as a live room, so a game renders a replay with its usual code. loadReplay() from @irtio/client/replay returns one, and so does loadClip().
Constructor
new Replay(bytes)
new Replay(bytes, options)
bytes- A
Uint8Array: the replay or clip file. optionsOptional- An object with the option
loadReplay()takes,schema.
new Replay(bytes, options) is what loadReplay(bytes, options) returns, and throws the same errors.
Instance properties
All are read only except speed.
Replay.entities- The entities at the tick shown, in an
EntityStore. Replay.state- The shared state at the tick shown.
Replay.presence- Who was in the room at the tick shown.
Replay.speed- How fast
play()runs, as a multiple of the room’s tick rate. Replay.firstTick- The first tick the replay can show.
Replay.lastTick- The last tick the replay can show.
Replay.tick,Replay.playingandReplay.tickRate- The tick shown, whether it’s playing and the room’s tick rate.
Replay.players- The players whose private state the replay holds at the tick shown.
Replay.schema,Replay.startedAt,Replay.recordedPrivateandReplay.recordedMessages- The schema the replay is read with, when the recording began and what it recorded.
Instance methods
Replay.play()- Plays from the tick shown until the last tick or
pause(). Replay.pause()- Stops playing at the tick shown.
Replay.seek()- Shows the state at a tick.
Replay.step()- Shows the next tick.
Replay.kind()- Returns a recorded kind by name, for kinds that changed since the recording.
Replay.privateOf()- Returns a player’s private state at the tick shown, when it was recorded.
Events
Listen to these events with addEventListener().
tickevent- A tick was shown, by playing through it, stepping or seeking.
messageevent- Playback passed a broadcast the room recorded.
endedevent- Playback reached the last tick.
Description
Entities in a replay are read-only. A replay of a room without a tickRate is recorded on a clock of 20 ticks a second, so it seeks and plays the same way.
Playback state
tick is the number of the tick shown; change it with seek() or step(). playing is true from play() until pause() or the last tick. tickRate is the room’s ticks per second, which play() runs at times speed.
Recording
schema is the schema the replay is read with: the schema option’s kinds where the recorded ones have the same fields, and the recorded kinds for those that changed. startedAt is when the recording began, in milliseconds since the Unix epoch. recordedPrivate is true when the recording kept players’ private state, which privateOf() reads, and recordedMessages is true when it kept the room’s broadcasts, which the message event gives.
Recorded private state
players is an array of the player IDs whose private state the replay holds at the tick shown. It’s empty when recordedPrivate is false.
Examples
Building a scrub bar
A replay viewer’s range input seeks the replay, and follows it while it plays.
// scrub.ts
import type { Replay } from "@irtio/client/replay";
export function scrubBar(replay: Replay, input: HTMLInputElement): void {
input.min = String(replay.firstTick);
input.max = String(replay.lastTick);
input.addEventListener("input", () => replay.seek(Number(input.value)));
replay.addEventListener("tick", () => (input.value = String(replay.tick)));
}
See also
Runtime availability
| Browsers | Node | |
|---|---|---|
Replay | 5.0 | 5.0 |