combine() function
Available since
@irtio/room 0.1Hosted roomsirt dev@irtio/testingSelf-hosted room hostThe combine() function joins grids and other interest parts into one interest. A connection sees an entity when, in some part, they share a key and that part’s sees, if it has one, passes it.
Syntax
combine(part1)
combine(part1, part2)
combine(part1, part2, /* …, */ partN)
Parameters
part1, …,partN- Interest parts: objects with
keysOf,subscriptionsOfand an optionalsees, such asgrid()returns.
Return value
An interest part with keysOf, subscriptionsOf and sees. Spread it to add a limit: { ...combine(a, b), limit }.
Description
Each part keeps its own semantics: a radius grid shows its whole cells, and a range grid’s distance check applies only to what it placed, not to what another part shows. Give each grid the kinds it places, or every grid also places the other grids’ entities, as EVERYWHERE when they have no position on its axes; and give each a prefix, so cells of different sizes don’t share keys.
Examples
Nearby players and the whole squad
// squads.ts
import { combine, defineRoom, defineSchema, entity, grid, type Connection, type Room, type RoomEntity } from "@irtio/room";
const Trooper = entity("trooper", { owner: "string", squad: "string", x: "f32", y: "f32" }, { key: "owner" });
type T = RoomEntity<typeof Trooper>;
const nearby = grid({ cellSize: 50, range: 120, dims: "xy", prefix: "g:", around: (room, c) => room.entities.get(Trooper, c.player) });
const squad = {
keysOf: (_room: Room, e: RoomEntity) => (e.kind === Trooper ? [`squad:${(e as T).squad}`] : []),
subscriptionsOf: (_room: Room, c: Connection) => [`squad:${c.data.squad as string}`],
};
export default defineRoom({
schema: defineSchema({ entities: [Trooper] }),
interest: combine(nearby, squad),
onJoin(room, connection) {
connection.data.squad = (connection.meta as { squad?: string } | null)?.squad ?? "red";
if (!room.entities.get(Trooper, connection.player)) room.entities.create(Trooper, { owner: connection.player, squad: connection.data.squad });
},
});
See also
Runtime availability
| Hosted rooms | irt dev | @irtio/testing | Self-hosted room host | |
|---|---|---|---|---|
combine() | 0.1 | 0.1 | 0.1 | 0.1 |