Bot

Available since @irtio/room 0.1Hosted roomsirt dev@irtio/testingSelf-hosted room host

The Bot interface of the Room API is one bot in a room: a player that one of the room’s behaviours drives, with no connection. Bots.add() returns one, and each behaviour hook gets the bot it runs for.

Instance properties

Bot.player
The bot’s player ID: bot: and a number the room gives out in order, never reused while the room exists. Read only.
Bot.behaviour
The name of the behaviour the bot runs, as the room definition’s bots declares it. Read only.
Bot.data
An object for the behaviour’s own state, saved in room state. It must be a JSON object of at most MAX_BOT_DATA (4,096) bytes; a bot whose data stops being one is removed, and the error logged.
Bot.input
The input the stepper gets under the bot’s player ID this tick. The behaviour’s tick sets it, and it’s undefined when tick starts. The room doesn’t run the stepper’s parseInput on it.

Description

A bot is a player like any other to presence, ownership and private state, all of which work by its player ID. It never causes onJoin or onLeave, which take a connection. It has no player data: room.playerData() throws for a bot’s ID.

isBot(player), from @irtio/room, tells a bot’s player ID from a person’s. No player token can have a sub starting with bot:.

Examples

Steering with input

The bot’s car moves by the same stepper as a person’s, from the input its behaviour sets.

// chaser.ts
import { defineRoom, defineSchema, defineStepper, entity } from "@irtio/room";

const Car = entity("car", { owner: "string", x: "f32" });

export default defineRoom({
  tickRate: 20,
  schema: defineSchema({ entities: [Car] }),
  stepper: defineStepper<number>({
    step(world, inputs) {
      for (const car of world.all(Car)) car.x += inputs.get(car.owner) ?? 0;
    },
  }),
  bots: {
    chaser: {
      start(room, bot) {
        const mine = [...room.entities.all(Car)].some((car) => car.owner === bot.player);
        if (!mine) room.entities.create(Car, { owner: bot.player });
      },
      tick(room, bot) {
        bot.input = room.tick % 40 < 30 ? 1 : 0.5;
      },
    },
  },
  onCreate(room) {
    room.bots.add("chaser", { name: "Chaser" });
  },
});

See also

Runtime availability

Hosted roomsirt dev@irtio/testingSelf-hosted room host
Bot0.10.10.10.1