RoomDefinition: interest property

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

The interest property of a room definition decides which entities each connection sees, so a player in a large world gets only what’s near it.

Value

An Interest: an object of functions that give each entity its interest keys and each connection its subscriptions, with an optional sees and limit. grid() and combine() make the common ones. Without interest, every connection sees every entity.

Description

A connection sees an entity when they share an interest key and, if the interest has sees, sees returns true for them; limit caps what’s left. An entity that enters a connection’s view arrives with all its fields, and one that leaves it is removed from that client. The room works out every connection’s view with each state update. Each function gets the room first, as hooks do.

  • A connection subscribes to at most 4,096 keys and sees at most 16,384 entities.
  • Entities of a hidden kind get no keys, whatever interest returns, so no connection sees them.
  • Presence doesn’t follow interest. To narrow it, use seesPresence().
  • Proximity voice by default lets a listener hear the players whose entities it sees, ranked by limit’s priority.

An error in an interest function stops the room, and its connections close with close code 4007.

Examples

Seeing what’s within 30 units

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

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

export default defineRoom({
  schema: defineSchema({ entities: [Scout] }),
  tickRate: 20,
  interest: grid({
    cellSize: 32,
    dims: "xy",
    range: 30,
    around: (room, connection) => room.entities.get(Scout, connection.player),
  }),
  onJoin(room, connection) {
    if (!room.entities.get(Scout, connection.player)) {
      room.entities.create(Scout, { owner: connection.player, x: Math.random() * 500, y: Math.random() * 500 });
    }
  },
});

See also

Runtime availability

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