Bots: add() method

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

The add() method of the Bots interface adds a bot running a behaviour the room declares, and runs the behaviour’s start before it returns.

Syntax

add(behaviour)
add(behaviour, options)

Parameters

behaviour
The name of a behaviour in the room definition’s bots.
options Optional

An object with any of these properties:

name
The bot’s presence name, at most 64 characters. Defaults to "".
status
The bot’s presence status, JSON of at most 1,024 bytes. Defaults to null.
data
The bot’s data, a JSON object of at most 4,096 bytes. Defaults to {}.

Return value

The new Bot.

Exceptions

Error
Thrown if the room declares no behaviour of that name.
RangeError
Thrown if the room already holds MAX_BOTS (64) bots, or if people and bots already fill room.maxPlayers seats.
TypeError or RangeError
Thrown if name, status or data isn’t the type described above, or is over its limit.

Description

The bot gets the player ID bot: and the next number, and is in presence at once, holding a seat. onJoin doesn’t run for it. If start throws or returns a promise, the error is logged and the bot is removed again.

A bot add() puts over a fill level stays until a person’s join or a fill() call removes it.

Examples

An opponent for a player alone

// duel.ts
import { defineRoom, isBot } from "@irtio/room";

export default defineRoom({
  bots: { duelist: {} },
  onJoin(room) {
    const people = [...room.presence].filter((entry) => !isBot(entry.player));
    if (people.length === 1 && room.bots.size === 0) room.bots.add("duelist", { name: "Training dummy", status: { ready: true } });
  },
});

See also

Runtime availability

Hosted roomsirt dev@irtio/testingSelf-hosted room host
add()0.10.10.10.1