Connection: input property
Available since
@irtio/room 0.1Hosted roomsirt dev@irtio/testingSelf-hosted room hostThe input read-only property of the Connection interface is the input the room applied for the connection’s player this tick.
Value
The input as the stepper’s parseInput() returned it, typed unknown, or undefined.
- It’s set before the stepper runs and cleared after
onTick, so the stepper andonTickboth see it. - It’s
undefinedwhen the player sent no input for this tick, in a room without a stepper, and at any time between ticks, such as inonMessage.
Description
Inputs go to the stepper, which runs on the room and in the player’s client to predict it. Act on the parts the room alone decides, such as a shot or a pickup, in onTick, where input is still set, together with the viewTick the input was sent from.
A stepper without parseInput() passes the input on as the client sent it, so check it before trusting it.
Examples
Spending ammo on the ticks a player fires
// ammo.ts
import { defineRoom, defineSchema, defineStepper, entity } from "@irtio/room";
const Gunner = entity("gunner", { owner: "string", angle: "angle", ammo: "u8" }, { key: "owner" });
type Input = { turn: number; fire: boolean };
const stepper = defineStepper<Input>({
parseInput(raw) {
const { turn, fire } = raw as { turn?: unknown; fire?: unknown };
if (typeof turn !== "number" || Math.abs(turn) > 1 || typeof fire !== "boolean") throw new TypeError("input is { turn, fire }");
return { turn, fire };
},
step(world, inputs, dt) {
for (const gunner of world.all(Gunner)) gunner.angle += (inputs.get(gunner.owner)?.turn ?? 0) * 3 * dt;
},
});
export default defineRoom({
tickRate: 30,
schema: defineSchema({ entities: [Gunner] }),
stepper,
onJoin(room, connection) {
if (!room.entities.get(Gunner, connection.player)) room.entities.create(Gunner, { owner: connection.player, ammo: 30 });
},
onTick(room) {
for (const connection of room.connections.values()) {
const gunner = room.entities.get(Gunner, connection.player);
if (!gunner || !(connection.input as Input | undefined)?.fire || gunner.ammo === 0) continue;
gunner.ammo -= 1;
room.broadcast({ type: "shot", from: connection.player, angle: gunner.angle }, { unreliable: true });
}
},
});
See also
Runtime availability
| Hosted rooms | irt dev | @irtio/testing | Self-hosted room host | |
|---|---|---|---|---|
input | 0.1 | 0.1 | 0.1 | 0.1 |