O'Reilly logo

Unity Multiplayer Games by Alan Stagner

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Assigning player IDs

Another feature required by nearly all multiplayer games, is the ability to track players by player ID. We need some kind of lightweight data, which uniquely identifies a specific player, such as a number (for instance, an object might store this number as a reference to the player who owns it). So, let's modify our server to assign player IDs to peers as they connect. It will also notify the client of its player ID upon connecting.

We'll use a long integer for the purpose.

A static long variable is initialized with the smallest value possible (long.MinValue), the last assigned player ID. When a player joins, we copy the static variable as their ID, and then increment the static variable.using Photon.SocketServer; using PhotonHostRuntimeInterfaces; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required