workadventure/docs/maps/api-player.md
David Négrier 90f7287860 Adding the ability to set the player's color outline via the scripting API
(currently not shared with the other players yet)
2021-12-21 17:02:53 +01:00

4.3 KiB

{.section-title.accent.text-primary}

API Player functions Reference

Get the player name

WA.player.name: string;

The player name is available from the WA.player.name property.

{.alert.alert-info} You need to wait for the end of the initialization before accessing WA.player.name

WA.onInit().then(() => {
    console.log('Player name: ', WA.player.name);
})

Get the player ID

WA.player.id: string|undefined;

The player ID is available from the WA.player.id property. This is a unique identifier for a given player. Anonymous player might not have an id.

{.alert.alert-info} You need to wait for the end of the initialization before accessing WA.player.id

WA.onInit().then(() => {
    console.log('Player ID: ', WA.player.id);
})

Get the tags of the player

WA.player.tags: string[];

The player tags are available from the WA.player.tags property. They represent a set of rights the player acquires after login in.

{.alert.alert-warn} Tags attributed to a user depend on the authentication system you are using. For the hosted version of WorkAdventure, you can define tags related to the user in the administration panel.

{.alert.alert-info} You need to wait for the end of the initialization before accessing WA.player.tags

WA.onInit().then(() => {
    console.log('Tags: ', WA.player.tags);
})

Get the user-room token of the player

WA.player.userRoomToken: string;

The user-room token is available from the WA.player.userRoomToken property.

This token can be used by third party services to authenticate a player and prove that the player is in a given room. The token is generated by the administration panel linked to WorkAdventure. The token is a string and is depending on your implementation of the administration panel. In WorkAdventure SAAS version, the token is a JWT token that contains information such as the player's room ID and its associated membership ID.

If you are using the self-hosted version of WorkAdventure and you developed your own administration panel, the token can be anything. By default, self-hosted versions of WorkAdventure don't come with an administration panel, so the token string will be empty.

{.alert.alert-info} A typical use-case for the user-room token is providing logo upload capabilities in a map. The token can be used as a way to authenticate a WorkAdventure player and ensure he is indeed in the map and authorized to upload a logo.

{.alert.alert-info} You need to wait for the end of the initialization before accessing WA.player.userRoomToken

WA.onInit().then(() => {
    console.log('Token: ', WA.player.userRoomToken);
})

Listen to player movement

WA.player.onPlayerMove(callback: HasPlayerMovedEventCallback): void;

Listens to the movement of the current user and calls the callback. Sends an event when the user stops moving, changes direction and every 200ms when moving in the same direction.

The event has the following attributes :

  • moving (boolean): true when the current player is moving, false otherwise.
  • direction (string): "right" | "left" | "down" | "top" the direction where the current player is moving.
  • x (number): coordinate X of the current player.
  • y (number): coordinate Y of the current player.
  • oldX (number): old coordinate X of the current player.
  • oldY (number): old coordinate Y of the current player.

callback: the function that will be called when the current player is moving. It contains the event.

Example :

WA.player.onPlayerMove(console.log);

Set the outline color of the player

WA.player.setOutlineColor(red: number, green: number, blue: number): Promise<void>;
WA.player.removeOutlineColor(): Promise<void>;

You can display a thin line around your player's name (the "outline").

Use setOutlineColor to set the outline and removeOutlineColor to remove it.

Colors are expressed in RGB. Each parameter is an integer between 0 and 255.

// Let's add a red outline to our player
WA.player.setOutlineColor(255, 0, 0);

When you set the outline on your player, other players will see the outline too (the outline color is shared across browsers automatically).