Skip to content

How to set player name/username? #676

Answered by endel
psociety asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @psociety, the client.userData is meant for server usage only, the only structures that are synchronized automatically are from the room's state. You can do it like this:

class Player extends Schema {
  @type("string") username: string;
}

// ...

onJoin(client, options)  {
  this.state.players.set(client.sessionId, new Player().assign({ 
    username: client.auth.username
  }));
  console.log(client.auth.username, "joined!");
}

Reading it from the client-side:

const room = await client.joinOrCreate("my_room");
room.state.players.onAdd((player, sessionId) => {
    console.log(player.username, "joined!");
});

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@psociety
Comment options

Answer selected by psociety
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants