Skip to content
Docs
Make your game

Roles

Let the room decide who may start, pause or change your game, with defaults that work in any room.

Some things only certain players should do: start the match, pick teams, change the settings. But who that is depends on the room: one room has admins, another lets everyone do everything.

So the work is split. Your game declares its roles, with a sensible default. The room decides who holds them, in code or by hand. Your game never needs to know what an "admin" is.

Declare

Declare roles in top-level code:

src/roles.ts

The default applies when nothing else decides:

  • "everyone": every player.

  • "nobody": no one, until the game, the room or a person gives it.

  • "owner": the room's owner, when they're playing.

Gate the Menu

Give a control the role as its audience, and its request requires. Players without the role don't see the control, or see it disabled, and can't send the request:

src/roles.ts

requires also takes several roles; holding any of them is enough.

Check and hand out

role.has(player) and role.holders() answer right away. A game can give and take its own roles, here to make the first player an admin:

src/roles.ts
  • A give or take takes effect from the next turn, with a role-change event.

  • The room's decision, and a person's in the Room management modal, wins over the game's.

  • Keep secret identities, such as who is "it", in game state with audiences, not in roles: rooms can see roles.

For the room's side, see Roles & moderation.