EntityStore: ownerOf() method
Available since
@irtio/client 5.0BrowsersNodeThe ownerOf() method of the EntityStore interface returns the player who owns an entity, or undefined when the room owns it.
Syntax
ownerOf(entity)
Parameters
entity- A
ClientEntity.
Return value
A string, the owner’s player ID, or undefined when the room owns the entity or the client doesn’t hold it.
Exceptions
None.
Description
The owner arrives with the entity and with each change of owner, which onOwner() follows. Compare the result with room.player to tell whether the local player owns an entity, or use mine() to list the ones it owns.
Examples
Labelling each ship with its pilot
The client shows whose each ship is, and marks the player’s own.
// pilots.ts
import { connect, defineSchema, entity } from "@irtio/client";
const Ship = entity("ship", { x: "f32", y: "f32" });
const schema = defineSchema({ entities: [Ship] });
const room = await connect("sector-4", { schema });
room.entities.onEnter(Ship, (ship) => {
const pilot = room.entities.ownerOf(ship);
if (pilot === undefined) console.log(`ship ${ship.id} drifts unpiloted`);
else console.log(`ship ${ship.id} flown by ${pilot === room.player ? "you" : pilot}`);
});
See also
EntityStore.onOwner()Entities.ownerOf(), the room’s side- Ownership
Runtime availability
| Browsers | Node | |
|---|---|---|
ownerOf() | 5.0 | 5.0 |