Bots: remove() method

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

The remove() method of the Bots interface removes a bot from the room, running onPlayerLeave for it before it returns.

Syntax

remove(bot)

Parameters

bot
The Bot, or its player ID.

Return value

true if the bot was removed, or false if it wasn’t in the room.

Description

onPlayerLeave runs with the bot’s player ID. Until the hook first awaits, the bot’s presence entry is still there to read, no longer holding a seat; the presence ends before remove() returns. So code that cleans up after a player cleans up after bots too. onLeave doesn’t run, since a bot has no connection.

While the room is under its fill level, a removed bot is replaced by a new one.

Examples

Taking out an eliminated bot

// last-standing.ts
import { defineRoom, defineSchema, entity, isBot } from "@irtio/room";

const Fighter = entity("fighter", { owner: "string", hp: "u8" }, { key: "owner" });

export default defineRoom({
  tickRate: 10,
  schema: defineSchema({ entities: [Fighter] }),
  bots: {
    brawler: {
      start(room, bot) {
        if (!room.entities.get(Fighter, bot.player)) room.entities.create(Fighter, { owner: bot.player, hp: 100 });
      },
    },
  },
  onTick(room) {
    for (const fighter of room.entities.all(Fighter)) {
      if (fighter.hp === 0 && isBot(fighter.owner)) room.bots.remove(fighter.owner);
    }
  },
  onPlayerLeave(room, player) {
    const fighter = room.entities.get(Fighter, player);
    if (fighter) room.entities.delete(fighter);
  },
});

See also

Runtime availability

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