# player

sdk.player.get() gives the player's id, nickname and guest state on the current host; on('change') follows a nickname or an account link.

## `player.get(): Player`

Synchronous. Throws before `init()` resolves; after it, always answers.

<CodePanel title="Read the player">
  <Fragment slot="javascript">
    ```js
    const player = sdk.player.get();
    // { id: string, nickname: string, guest: boolean, host: HostName }
    drawName(player.guest ? 'Guest' : player.nickname);
    ```

  </Fragment>
  <Fragment slot="typescript">
    ```ts
    import type { Player } from '@filudi/game-sdk';

    const player: Player = sdk.player.get();
    drawName(player.guest ? 'Guest' : player.nickname);
    ```

  </Fragment>
</CodePanel>

`id` is stable per host. On filudi.com it is the player id of the API: the same for a guest and for the
account that guest later creates. On portals it is the portal's user id when the portal exposes one, else
a random id kept in `localStorage`. `nickname` is the hub's or the portal's display name; a guest on
filudi.com gets a generated one like `Guest-Fox`. Do not key saves on `id`: the SDK does that for you.

## `player.on('change', listener): Off`

Fires when the nickname changes or the player links an account while the game runs. The listener gets the
new `Player`. Returns a function that removes the listener.

<CodePanel title="Follow the player">
  <Fragment slot="javascript">
    ```js
    const off = sdk.player.on('change', (player) => drawName(player.nickname));
    // off() when the scene goes away
    ```

  </Fragment>
  <Fragment slot="typescript">
    ```ts
    import type { Off, Player } from '@filudi/game-sdk';

    const off: Off = sdk.player.on('change', (player: Player) => drawName(player.nickname));
    ```

  </Fragment>
</CodePanel>

## Unavailable cases

None: every host has a player. On Poki and on the standalone mock the player is always a guest. `features.account`
from `init()` says whether the host lets a guest become an account; the hub owns that flow, the game only
reads the result through `change`.
