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
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: newPlayer.name = playerId. This is handy because you can retrieve player entities by their name like so: this.app.root.findByName(playerId). 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.
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.