cellKey() function

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

The cellKey() function returns the interest key grid() gives a cell: the cell coordinates joined by commas.

Syntax

cellKey(x, y)
cellKey(x, y, z)

Parameters

x, y, z
The cell coordinates, each Math.floor(position / cellSize) on its axis.

Return value

A string such as "3,-1" or "3,-1,0". It doesn’t include a grid’s prefix.

Examples

Letting spectators watch a chosen cell

A spectator subscribes to the cell it picked as well as the cells the grid gives it, so it sees the entities in both.

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

const Unit = entity("unit", { x: "f32", y: "f32" });
const CELL = 64;
const area = grid({ cellSize: CELL, radius: 1, dims: "xy", around: (_room, c) => c.data.at });

export default defineRoom({
  schema: defineSchema({ entities: [Unit] }),
  interest: {
    keysOf: area.keysOf,
    subscriptionsOf(room, c) {
      const keys = [...area.subscriptionsOf(room, c)];
      if (c.data.watch) keys.push(cellKey(c.data.watch[0], c.data.watch[1]));
      return keys;
    },
  },
  onMessage(_room, connection, message) {
    const m = message as { watch?: [number, number] };
    if (m.watch) connection.data.watch = [Math.floor(m.watch[0] / CELL), Math.floor(m.watch[1] / CELL)];
  },
});

See also

Runtime availability

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