# Replay

> The Replay interface of @irtio/client/replay plays a replay or clip into an entity store, a tick at a time.

The **`Replay`** interface plays a replay or clip into an [`EntityStore`](/docs/client/entity-store) a tick at a time, with the same events as a live room, so a game renders a replay with its usual code. [`loadReplay()`](/docs/client/loadreplay) from `@irtio/client/replay` returns one, and so does [`loadClip()`](/docs/client/loadclip).

## Constructor

```ts
new Replay(bytes)
new Replay(bytes, options)
```

`bytes`
: A `Uint8Array`: the replay or clip file.

`options` Optional
: An object with the option [`loadReplay()`](/docs/client/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`](/docs/client/replay/entities)
: The entities at the tick shown, in an `EntityStore`.

[`Replay.state`](/docs/client/replay/state)
: The shared state at the tick shown.

[`Replay.presence`](/docs/client/replay/presence)
: Who was in the room at the tick shown.

[`Replay.speed`](/docs/client/replay/speed)
: How fast `play()` runs, as a multiple of the room's tick rate.

[`Replay.firstTick`](/docs/client/replay/firsttick)
: The first tick the replay can show.

[`Replay.lastTick`](/docs/client/replay/lasttick)
: The last tick the replay can show.

[`Replay.tick`, `Replay.playing` and `Replay.tickRate`](#playback-state)
: The tick shown, whether it's playing and the room's tick rate.

[`Replay.players`](#recorded-private-state)
: The players whose private state the replay holds at the tick shown.

[`Replay.schema`, `Replay.startedAt`, `Replay.recordedPrivate` and `Replay.recordedMessages`](#recording)
: The schema the replay is read with, when the recording began and what it recorded.

## Instance methods

[`Replay.play()`](/docs/client/replay/play)
: Plays from the tick shown until the last tick or `pause()`.

[`Replay.pause()`](/docs/client/replay/pause)
: Stops playing at the tick shown.

[`Replay.seek()`](/docs/client/replay/seek)
: Shows the state at a tick.

[`Replay.step()`](/docs/client/replay/step)
: Shows the next tick.

[`Replay.kind()`](/docs/client/replay/kind)
: Returns a recorded kind by name, for kinds that changed since the recording.

[`Replay.privateOf()`](/docs/client/replay/privateof)
: Returns a player's private state at the tick shown, when it was recorded.

## Events

Listen to these events with `addEventListener()`.

[`tick` event](/docs/client/replay/tick-event)
: A tick was shown, by playing through it, stepping or seeking.

[`message` event](/docs/client/replay/message-event)
: Playback passed a broadcast the room recorded.

[`ended` event](/docs/client/replay/ended-event)
: 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()`](/docs/client/replay/seek) or [`step()`](/docs/client/replay/step). `playing` is `true` from [`play()`](/docs/client/replay/play) until [`pause()`](/docs/client/replay/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()`](/docs/client/replay/privateof) reads, and `recordedMessages` is `true` when it kept the room's broadcasts, which the [`message` event](/docs/client/replay/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.

```ts
// 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

- [`loadReplay()`](/docs/client/loadreplay)
- [`loadClip()`](/docs/client/loadclip)
- [`EntityStore`](/docs/client/entity-store)
- [Replays and clips](/docs/room/guides/replays)
