RoomDefinition: history property

Available since @irtio/room 0.1Hosted roomsirt dev@irtio/testingSelf-hosted room host

The history property of a room definition lists the entity fields the room records after each tick’s state update, so room.rewind() can return them as a past tick sent them. It’s for lag compensation: checking a shot against what the shooter saw.

Value

A read-only array of History objects, each made with defineHistory() for one entity kind. It needs a tickRate.

Exceptions

defineRoom() throws an Error for a history in a room without a tickRate, and for one that lists a kind that isn’t an entity kind of the room’s schema.

Description

room.rewind(history, tick) takes one of the histories listed here; it throws for any other.

  • The room keeps ceil(tickRate × MAX_REWIND_MS / 1000) + 2 ticks: 7 at 20 ticks a second, 32 at 120. MAX_REWIND_MS is 250.
  • Each recorded entity costs 8 bytes per field per kept tick, plus 128 bytes. All of a room’s histories together hold at most MAX_HISTORY, 8 MB; past it, new entities aren’t recorded until deleted ones leave the kept ticks, and the room logs an error once.
  • A history isn’t room state. It’s kept in memory, never saved, and starts empty when the room starts or wakes.

Examples

Checking a hit where the shooter saw it

// sniper.ts
import { defineHistory, defineRoom, defineSchema, entity } from "@irtio/room";

const Target = entity("target", { x: "f32", y: "f32" });
const targets = defineHistory(Target, ["x", "y"]);

export default defineRoom({
  schema: defineSchema({ entities: [Target] }),
  tickRate: 30,
  history: [targets],
  onMessage(room, connection, message) {
    const shot = message as { x?: unknown; y?: unknown };
    if (typeof shot.x !== "number" || typeof shot.y !== "number") return;
    for (const [id, past] of room.rewind(targets, connection.viewTick)) {
      if (Math.hypot(past.x - shot.x, past.y - shot.y) < 1) room.broadcast({ type: "hit", target: id, by: connection.player });
    }
  },
});

See also

Runtime availability

Hosted roomsirt dev@irtio/testingSelf-hosted room host
history0.10.10.10.1