Entities: ownerOf() method

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

The ownerOf() method of the Entities interface returns the player who owns an entity, or undefined when the room owns it.

Syntax

ownerOf(entity)

Parameters

entity
The RoomEntity to ask about.

Return value

The owner’s player ID, a string, or undefined for the room.

Exceptions

Error
Thrown if entity isn’t a live entity of the room’s entities, such as one already deleted, or room.state.
TypeError
Thrown if entity isn’t an entity object.

Examples

Letting only a card’s holder play it

// cards.ts
import { defineRoom, defineSchema, entity } from "@irtio/room";

const Card = entity("card", { suit: "u8", rank: "u8", played: "bool" });

export default defineRoom({
  schema: defineSchema({ entities: [Card] }),
  onMessage(room, connection, message) {
    const id = (message as { play?: unknown } | null)?.play;
    const card = typeof id === "number" ? room.entities.get(id) : undefined;
    if (card?.kind !== Card || room.entities.ownerOf(card) !== connection.player) return;
    card.played = true;
    room.entities.setOwner(card, undefined);
  },
});

See also

Runtime availability

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