Entities: delete() method

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

The delete() method of the Entities interface deletes an entity from the room. Clients that see it drop it in the next state update.

Syntax

delete(entity)

Parameters

entity
The RoomEntity to delete.

Return value

None (undefined).

Exceptions

TypeError
Thrown if entity isn’t an entity object.

Description

Deleting an entity frees its key at once, and an explicit id can give its ID to a new entity. The entity is no longer returned by get(), all() or ownedBy().

The object you hold still reads the values it had, but writing a field of it, or calling its patch(), throws an Error. Deleting an entity that’s already deleted does nothing.

Examples

Removing a player’s avatar when the player leaves

onPlayerLeave runs once the player’s presence ends, so a dropped network that reconnects in time keeps the avatar.

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

const Avatar = entity("avatar", { owner: "string", x: "f32", y: "f32" }, { key: "owner" });

export default defineRoom({
  schema: defineSchema({ entities: [Avatar] }),
  onJoin(room, connection) {
    if (!room.entities.get(Avatar, connection.player)) room.entities.create(Avatar, { owner: connection.player });
  },
  onPlayerLeave(room, player) {
    const avatar = room.entities.get(Avatar, player);
    if (avatar) room.entities.delete(avatar);
  },
});

See also

Runtime availability

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