Prediction: onEvent() method
@irtio/client 5.0BrowsersNodeThe onEvent() method of the Prediction interface handles one event kind the stepper emits for the local player: once when the live step emits it, then again when the room’s state shows whether the room emitted it too.
Syntax
onEvent(event, handlers)
Parameters
event- A kind: one of the schema’s
events. handlers-
An object with any of these properties:
predict- A function called with the event’s values and the tick it was predicted for, when the live step emits the event. Replays of that step don’t call it again.
confirm- A function called with the predicted values, the room’s values and the room’s tick, when the room’s step emitted the same kind within
windowticks of the predicted one. reject- A function called with the predicted values and their tick, when the room’s state has passed the predicted tick plus
windowwithout the room’s emit. In a room that sends no state updates, it’s called once the prediction is 64 ticks past the event. unpredicted- A function called with the room’s values and its tick, for an event the room emitted that the prediction didn’t.
window- A number: how many ticks either side of the predicted tick the room’s emit may land and still confirm it. Defaults to
2. With several handlers for one kind, the widest window holds for all of them.
Return value
A function that removes the handlers.
Exceptions
Error- Thrown if
eventisn’t one of the schema’sevents.
Description
The stepper emits an event with world.emit(event, values, { by }), and the room sends it to player by’s connections only. On the client, an event by another player does nothing, since only the local player is predicted. Events awaiting the room are dropped, with no handler called, on a reconnect.
Play an effect in predict rather than in the step: a step runs again on each replay, but predict runs once. Undo it in reject, and play it late in unpredicted. Predicted events covers declaring events and the room’s side.
Examples
Playing a landing sound when the player lands
A runner’s step emits landed when the runner touches the ground. The client plays the sound at once, takes it back if the room disagrees, and plays it late if only the room saw the landing.
// landing.ts
import { connect, defineSchema, defineStepper, entity } from "@irtio/client";
const Runner = entity("runner", { owner: "string", y: "f32", vy: "f32" });
const Landed = entity("landed", { speed: "f32" });
const schema = defineSchema({ entities: [Runner], events: [Landed] });
const stepper = defineStepper<{ jump: boolean }>({
step(world, inputs, dt) {
for (const r of world.all(Runner)) {
if (inputs.get(r.owner)?.jump && r.y === 0) r.vy = 12;
if (r.y === 0 && r.vy <= 0) continue;
r.vy -= 30 * dt;
r.y = Math.max(0, r.y + r.vy * dt);
if (r.y > 0) continue;
world.emit(Landed, { speed: -r.vy }, { by: r.owner });
r.vy = 0;
}
},
});
const room = await connect("rooftops", { schema });
const keys = new Set<string>();
addEventListener("keydown", (e) => keys.add(e.key));
addEventListener("keyup", (e) => keys.delete(e.key));
const me = room.predict(stepper, {
find: (r) => [...r.entities.all(Runner)].find((runner) => runner.owner === r.player),
sample: () => ({ jump: keys.has(" ") }),
});
me.onEvent(Landed, {
predict: (land) => console.log(`thud at ${land.speed.toFixed(1)} m/s`),
reject: () => console.log("the room says the runner didn't land"),
unpredicted: (land) => console.log(`late thud at ${land.speed.toFixed(1)} m/s`),
});
See also
mispredicteventdefineStepper(), the step that emits events- Predicted events
Runtime availability
| Browsers | Node | |
|---|---|---|
onEvent() | 5.0 | 5.0 |