Entities: all() method

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

The all() method of the Entities interface returns an iterator over every entity of a kind.

Syntax

all(kind)

Parameters

kind
The kind of entities to visit.

Return value

An iterator of RoomEntity objects of kind, typed as that kind. For the shared state kind it yields room.state. It yields nothing for a kind with no entities.

Description

all() visits every entity of the kind on each call. In a stepper, which runs every tick, loop with it over the few kinds that move, and reach everything else by ID or key with get().

Examples

Regrowing trees over time

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

const Tree = entity("tree", { x: "f32", y: "f32", wood: "u8" });

export default defineRoom({
  tickRate: 1,
  schema: defineSchema({ entities: [Tree] }),
  onTick(room) {
    for (const tree of room.entities.all(Tree)) {
      if (tree.wood < 10) tree.wood += 1;
    }
  },
});

See also

Runtime availability

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