Bots: fill() method
Available since
@irtio/room 0.1Hosted roomsirt dev@irtio/testingSelf-hosted room hostThe 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.0removes 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 unlessplayersis 0.
Return value
None (undefined).
Exceptions
RangeError- Thrown if
playersisn’t an integer from 0 toroom.maxPlayers. Error- Thrown if
playersisn’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 atroom.maxPlayers. A person who leaves is replaced once their presence ends. The bots it adds have no name and emptydata; name them instartwithroom.presence.set(). - Removing. A person who joins at the level takes the seat of the bot added last, in the join itself:
onPlayerLeaveruns for the bot, thenonJoinfor the person. Afill()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 forPRESENCE_GRACE(10 seconds), so people who were there come back to their seats rather than to bots. - A behaviour that fails, throwing in
startortickor withdatathat can’t be saved, has its bot removed, and filling waitsPRESENCE_GRACEbefore adding again. - A level saved in room state above a
maxPlayerslowered since fills only tomaxPlayers.
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 rooms | irt dev | @irtio/testing | Self-hosted room host | |
|---|---|---|---|---|
fill() | 0.1 | 0.1 | 0.1 | 0.1 |