Bots: fill() method

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

The fill() method of the Bots interface sets a fill level: the room keeps people and bots together at that number, adding bots running a behaviour as seats free up and removing them as people join.

Syntax

fill(players, behaviour)
fill(0)

Parameters

players
The number of seats people and bots fill together, an integer from 0 to room.maxPlayers. 0 removes every bot and clears the level.
behaviour
The name of a behaviour in the room definition’s bots, which the bots it adds run. Needed unless players is 0.

Return value

None (undefined).

Exceptions

RangeError
Thrown if players isn’t an integer from 0 to room.maxPlayers.
Error
Thrown if players isn’t 0 and the room declares no behaviour of that name.

Description

  • Adding. As seats free up, the room adds bots up to the level, stopping at MAX_BOTS (64) and at room.maxPlayers. A person who leaves is replaced once their presence ends. The bots it adds have no name and empty data; name them in start with room.presence.set().
  • Removing. A person who joins at the level takes the seat of the bot added last, in the join itself: onPlayerLeave runs for the bot, then onJoin for the person. A fill() call removes bots, last added first, down to the new level at once.
  • Who counts is everyone holding a seat: people in presence, including those in their 10-second presence grace after a dropped connection, and every bot, however it was added.
  • The level is room state, so it holds after hibernation or a crash, and calling fill() again replaces it. After a wake or a crash, the room adds nothing for PRESENCE_GRACE (10 seconds), so people who were there come back to their seats rather than to bots.
  • A behaviour that fails, throwing in start or tick or with data that can’t be saved, has its bot removed, and filling waits PRESENCE_GRACE before adding again.
  • A level saved in room state above a maxPlayers lowered since fills only to maxPlayers.

Examples

Racing against a full grid

// grid-start.ts
import { defineRoom } from "@irtio/room";

export default defineRoom({
  tickRate: 20,
  bots: {
    racer: {
      start(room, bot) {
        room.presence.set(bot.player, { name: `Racer ${bot.player.slice(4)}` });
      },
    },
  },
  onCreate(room) {
    room.bots.fill(Math.min(8, room.maxPlayers), "racer");
  },
  onMessage(room, _connection, message) {
    if (message === "race-over") room.bots.fill(0);
  },
});

See also

Runtime availability

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