defineHistory() function
Available since
@irtio/room 0.1Hosted roomsirt dev@irtio/testingSelf-hosted room hostThe defineHistory() function declares fields of one entity kind for a room to record after each tick’s state update, so room.rewind() can give them as a past tick had them, for lag compensation.
Syntax
defineHistory(kind, fields)
Parameters
kind- An entity kind of the room’s schema.
fields- An array of the kind’s number and
boolfields to record, at least one.
Return value
A History, to list in the room definition’s history.
Exceptions
TypeError- Thrown for a field the kind doesn’t have, a
string,bytes,jsonorquatfield, a field listed twice, or no fields.
Description
defineRoom() throws when history lists a kind that isn’t one of the schema’s entity kinds, or when the room has no tickRate. Each recorded entity costs 8 bytes per field per kept tick, plus 128 bytes, and all of a room’s histories together are capped at MAX_HISTORY (8 MB).
Examples
Checking a shot where the shooter saw the target
The shot comes in the player’s input, so connection.viewTick is the tick the player saw when aiming.
// sniper.ts
import { defineHistory, defineRoom, defineSchema, defineStepper, entity } from "@irtio/room";
const Target = entity("target", { x: "f32", y: "f32", alive: "bool" });
const positions = defineHistory(Target, ["x", "y", "alive"]);
type Aim = { shot?: { x: number; y: number } };
export default defineRoom({
tickRate: 30,
schema: defineSchema({ entities: [Target] }),
stepper: defineStepper<Aim>({ step() {} }),
history: [positions],
onTick(room) {
for (const connection of room.connections.values()) {
const shot = (connection.input as Aim | undefined)?.shot;
if (!shot) continue;
for (const [id, past] of room.rewind(positions, connection.viewTick)) {
if (past.alive && Math.hypot(past.x - shot.x, past.y - shot.y) < 1) room.broadcast({ type: "hit", target: id });
}
}
},
});
See also
Runtime availability
| Hosted rooms | irt dev | @irtio/testing | Self-hosted room host | |
|---|---|---|---|---|
defineHistory() | 0.1 | 0.1 | 0.1 | 0.1 |