Player numbers online

Hi! I have a party game I’m working on and I need a way to number each player on an online server it uses a slightly modified version of the playcanvas networking tutorial. I need first player to be label as 1 second to be 2 ext ext I also need a live player count which I have semi working already in a different project

Here is the code

here is the server

There are many many different ways you can do this. Here are some simple ideas.

For the live player count, you could have a variable that you increment once for every connection and decrement once for every disconnection. Or if you’re storing your player entities in their own array, you can simply retrive the array’s length.

For the player entity names, you could have an incrementing idGenerator variable that you simply increase by 1 every time a new player connects. var playerId = this.idGenerator++; Then in your createPlayerEntity() function, you could simply set the player entity’s name to the incrementing variable like so: = playerId. This is handy because you can retrieve player entities by their name like so: Keep in mind this incrementing variable will always give you a unique ID, but it won’t reset, it’ll keep counting up forever without reusing player IDs.

1 Like

could I do something like this?
this.Pcount = 0 in server then on connection this.Pcount = this.Pcount + 1 and on disconnection this.Pcount = this.Pcount - 1 then finally have a variable in the player class called PNUM and have PNUM = this.Pcount

Yeah that could work on the client side. On the server side, I don’t recommend storing the Pcount on each individual player. It would make more sense to keep a single instance of the PCount in the class that manages all the players.

Are you writing your own server logic?

I started with a base but yes
(the base was the playcanvas tutorial)