RoomDefinition: bots property
@irtio/room 0.1Hosted roomsirt dev@irtio/testingSelf-hosted room hostThe bots property of a room definition declares the behaviours the room’s bots run, by the name room.bots.add() and room.bots.fill() take. A bot is a player the room’s code drives instead of a person.
Value
An object whose keys are behaviour names and whose values are objects with these optional methods:
start(room, bot)- Runs when the bot is added, and again after each wake, after
onWake(). tick(room, bot, dt)- Runs each tick before the stepper, for each bot in the order they were added. Only a room with a
tickRatemay have a behaviour with one.
Both must be synchronous.
Exceptions
defineRoom() throws an Error for a behaviour with a tick hook in a room without a tickRate.
Description
A behaviour that throws, or returns a promise, has the error logged and its bot removed, and the room keeps running. So does a bot whose data stops being JSON or grows over MAX_BOT_DATA, 4 KB.
With a stepper, tick sets bot.input, and the stepper gets it under the bot’s player ID, without parseInput. Without a stepper, a behaviour changes entities itself. A room without ticks plays its bots from its own hooks, such as onMessage for a turn, or onAlarm for a bot that waits before it moves.
Bots are room state: their IDs, names, statuses, behaviours and data come back after a wake or a room host crash. A room holds at most 64 bots (MAX_BOTS). See Bots for seats, filling and what bots can’t do.
Examples
A practice opponent
// practice.ts
import { defineRoom, defineSchema, entity } from "@irtio/room";
const Paddle = entity("paddle", { owner: "string", y: "f32" }, { key: "owner" });
const Ball = entity("ball", { y: "f32" });
export default defineRoom({
schema: defineSchema({ entities: [Paddle], state: Ball }),
tickRate: 30,
bots: {
wall: {
start(room, bot) {
if (!room.entities.get(Paddle, bot.player)) room.entities.create(Paddle, { owner: bot.player, y: 240 });
},
tick(room, bot, dt) {
const paddle = room.entities.get(Paddle, bot.player);
if (paddle) paddle.y += Math.sign(room.state.y - paddle.y) * Math.min(Math.abs(room.state.y - paddle.y), 200 * dt);
},
},
},
onCreate(room) {
room.bots.add("wall", { name: "Wall" });
},
});
See also
Runtime availability
| Hosted rooms | irt dev | @irtio/testing | Self-hosted room host | |
|---|---|---|---|---|
bots | 0.1 | 0.1 | 0.1 | 0.1 |