Skip to content

Multiple players

const p1 = hub.player(0);
const p2 = hub.player(1);

hub.player(n) returns the handle for player n + 1 (0-based, up to four). The first controllers to connect become P1, P2 and so on, and each player keeps its controller after that: when P1’s controller disconnects, hub.player(1) still reads P2’s controller. A player without a controller reads an idle state and its outputs do nothing. The next controller that connects takes the lowest free player number. You can also pass a slot ID ('hid-1', 'gp-0') to bind to one specific controller.

hub.active is the active controller: the first one connected, or the one just connected over WebHID. When it disconnects, another connected controller becomes active. Change it with hub.hid.setActiveSlot(slotId). For testers that should follow whichever controller the player touches, see createControllerTelemetry() in Diagnostics, which switches automatically.

getSlotId() returns null while a player has no controller. Check it to pause the game and ask the player to reconnect:

const players = [hub.player(0), hub.player(1)];
hub.onCapabilitiesChange(() => {
const missing = players.findIndex((p) => p.getSlotId() === null);
if (missing === -1) resumeGame();
else pauseGame(`Reconnect the controller for P${missing + 1}`);
});

Connecting a DualSense or Switch Pro over WebHID gives it a new slot ID, but it stays the same player.

hub.getSlots(); // [{ id, playerNumber, source, identity, battery, lastActivityAt, ... }]
hub.onCapabilitiesChange(({ slots, active }) => {
renderPlayerList(slots);
});

The listener fires on connect, disconnect, WebHID upgrade, and active-controller changes. It returns an unsubscribe function.

A DualSense or Switch Pro connected through both the Gamepad API and WebHID appears once, as its WebHID slot.

connectButton.onclick = () => hub.connectWebHID(); // must be inside a click
await hub.reconnectAuthorized(); // reattach controllers the player approved before, no prompt
await hub.disconnectWebHID(); // the active controller; pass a slot ID for another
await hub.disconnectWebHID('hid-1', { forget: true }); // also revoke the browser permission

The order in which controllers connect is not always the order players want. @gamepadly/sdk/game has the seat logic hub.player(n) uses, for a lobby where each player picks a seat. Give the game host a seat list, and sdk.players.get(n) reads the controller in seat n:

import { createGameHost, reconcileSeats, toggleSeatAssignment, type SeatSlotCandidate } from '@gamepadly/sdk/game';
let seats: Array<string | null> = [null, null]; // slot ID per seat: P1, P2
const host = createGameHost({ gameId: 'duel', locale: 'en', getPlayerSeats: () => seats });
const candidates = (): SeatSlotCandidate[] => host.hub.getSlots().map((s) => ({ id: s.id, brand: s.identity.brand }));
let known = candidates(); // the controllers seats were last reconciled against
// A player clicks their controller in the lobby to take the lowest free seat, or to leave their seat.
function onControllerClicked(slotId: string) {
seats = toggleSeatAssignment(seats, slotId, 2);
host.notifyPlayerSeatsChanged();
}
// Keep seats across disconnects and WebHID upgrades; a newly connected controller fills a free seat.
host.hub.onCapabilitiesChange(() => {
const next = candidates();
seats = reconcileSeats(seats, known, next, 2);
known = next;
host.notifyPlayerSeatsChanged();
});

createInitialSeats(slots, maxPlayers) gives the default order, the first controllers in the first seats.