How to set Alias Name to the Players in Multiplayer?

How to set player name in Multiplayer

Original Post on Discord

by user 406778568597831690

When a player connects he just gets an id by the server. If you have a player name (or want to allow players to have names) you can just network a list of names (or a dictionary)

can you help me with the sample or Demo

by user 406778568597831690

I tried this using @syncField created an Array and pushed the name of the player is local player, tried accessing it from another player it’s length is always 1 no matter how many users join

@syncField()
NameArray:String = ;

start(): void {
if(this.playerState?.isLocalPlayer)
{
//Set the Name in Server
this.NameArray.push(this.playerState?.owner!)
}
}

tried to see the updated Values in Update didn’t work

by user 406778568597831690

Ah yeah that can be a limitation of syncField - it doesnt recognize when you change the array content.
Try setting the field again after pushing another value. E.g. after the line with this.nameArray.push add another line with this.NameArray = this.NameArray

But I’m not sure if that works as well right now.

If it doesnt then you can sync your array manually using the networking methods like:
this.context.connection.beginListen("player-names-changed", (names) => this.NameArray = names)
and
this.context.connection.send("player-names-changed", this.NameArray) where you currently push
The code above is a bit simplified but hopefully enough to get started :slightly_smiling_face:

@serializable()
@syncField(“usersUpdated”)
nameArray:Map<String,String> = new Map<String,String>

codegen is Unable to covert this script properly, and its giving me Error in unity instead of this Above code what i can use

error CS0305: Using the generic type ‘HashSet’ requires 1 type arguments

by user 406778568597831690

If the codegen doesnt produce correct results you can annotate it to force a specific type, see: https://www.npmjs.com/package/@needle-tools/needle-component-compiler