Entities: count property

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

The count read-only property of the Entities interface is the number of entities in the room.

Value

A whole number: every live entity, of every kind, hidden kinds included. room.state, private state and presence entries aren’t entities here and aren’t counted, unlike on the client.

Examples

Refusing to spawn past a budget

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

const Slime = entity("slime", { x: "f32", y: "f32" });
const MAX_ENTITIES = 500;

export default defineRoom({
  tickRate: 10,
  schema: defineSchema({ entities: [Slime] }),
  onTick(room) {
    if (room.tick % 50 === 0 && room.entities.count < MAX_ENTITIES) {
      room.entities.create(Slime, { x: Math.random() * 100, y: Math.random() * 100 });
    }
  },
});

See also

Runtime availability

Hosted roomsirt dev@irtio/testingSelf-hosted room host
count0.10.10.10.1