History
Available since
@irtio/room 0.1Hosted roomsirt dev@irtio/testingSelf-hosted room hostThe History interface of the Room API is the fields of one entity kind that a room records after each tick’s state update, as defineHistory() returns them. List it in the room definition’s history and read it back with room.rewind(history, tick).
Instance properties
History.kind- The entity kind recorded. Read only.
History.fields- The names of the fields recorded. Read only.
Description
room.rewind(history, tick) returns a Map from entity ID to a frozen { id, ...fields } with the fields as tick tick’s state update sent them:
- An entity that didn’t exist at
tickis absent. - A tick older than the oldest kept is clamped to it. A tick not yet recorded, such as
room.tickinonTick, or a later one, gives the entities as they are now. A fractional tick is rounded down. - The room keeps
ceil(tickRate × MAX_REWIND_MS / 1000) + 2ticks, whereMAX_REWIND_MSis 250: 7 at 20 ticks a second, 32 at 120. - The history isn’t room state: it’s never saved, and it starts empty when the room starts or wakes.
room.rewind() throws an Error for a history the definition doesn’t list, and a TypeError for a tick that isn’t a finite number. Steppers don’t get it, since a stepper also runs on the client, which keeps no history.
Examples
Rewinding to the tick a swing was aimed at
// melee.ts
import { defineHistory, defineRoom, defineSchema, defineStepper, entity } from "@irtio/room";
const Knight = entity("knight", { owner: "string", x: "f32", y: "f32" }, { key: "owner" });
const knights = defineHistory(Knight, ["x", "y"]);
type Move = { swing?: boolean };
export default defineRoom({
tickRate: 20,
schema: defineSchema({ entities: [Knight] }),
stepper: defineStepper<Move>({ step() {} }),
history: [knights],
onTick(room) {
for (const connection of room.connections.values()) {
if (!(connection.input as Move | undefined)?.swing) continue;
const me = room.entities.get(Knight, connection.player);
if (!me) continue;
for (const [id, past] of room.rewind(knights, connection.viewTick)) {
if (id !== me.id && Math.hypot(past.x - me.x, past.y - me.y) < 2) room.broadcast({ type: "struck", knight: id });
}
}
},
});
See also
Runtime availability
| Hosted rooms | irt dev | @irtio/testing | Self-hosted room host | |
|---|---|---|---|---|
History | 0.1 | 0.1 | 0.1 | 0.1 |