Bots: get() method

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

The get() method of the Bots interface returns the bot with a player ID, or undefined when the room has no such bot.

Syntax

get(player)

Parameters

player
A player ID, such as "bot:3".

Return value

The Bot, or undefined.

Examples

Changing a bot’s behaviour data from a message

// coach.ts
import { defineRoom } from "@irtio/room";

export default defineRoom({
  bots: { opponent: {} },
  onCreate(room) {
    room.bots.add("opponent", { name: "Opponent", data: { level: 1 } });
  },
  onMessage(room, _connection, message) {
    const m = message as { type?: string; bot?: string; level?: number };
    if (m.type !== "difficulty" || typeof m.bot !== "string" || typeof m.level !== "number") return;
    const bot = room.bots.get(m.bot);
    if (bot) bot.data.level = Math.min(Math.max(m.level, 1), 5);
  },
});

See also

  • Bot
  • isBot(), which tells a bot’s player ID from a person’s without looking it up

Runtime availability

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