Entities: ownerOf() method
Available since
@irtio/room 0.1Hosted roomsirt dev@irtio/testingSelf-hosted room hostThe 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
RoomEntityto ask about.
Return value
The owner’s player ID, a string, or undefined for the room.
Exceptions
Error- Thrown if
entityisn’t a live entity of the room’sentities, such as one already deleted, orroom.state. TypeError- Thrown if
entityisn’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 rooms | irt dev | @irtio/testing | Self-hosted room host | |
|---|---|---|---|---|
ownerOf() | 0.1 | 0.1 | 0.1 | 0.1 |