Back To Home

Photon

7-04-2024

Don't Be a Lone Wolf! Understanding Local & Remote Players in Photon

"This is a continuation of part 2. This post delves into the concepts of local and remote players within (PUN). We'll explore how to identify your self and opponent in the game"

In (PUN), each player in a multiplayer game is represented by a Player object. There are two types of players: local player (the player using the current client device) and remote players (other players in the same room). Let's discuss how to differentiate between local and remote players and how to access player properties:

Local Player :

  1. The local player refers to the player using the current client device.

  2. To access the local player's properties, you can use PhotonNetwork.LocalPlayer.

  3. For example, to access the local player's nickname:

  4.   
      string localPlayerNickname = PhotonNetwork.LocalPlayer.NickName;
    

  5. You can also access custom properties set for the local player:

  6.   
      object customProperty = PhotonNetwork.LocalPlayer.CustomProperties["PropertyName"];
    

Remote Player :

  1. Remote players are players other than the local player in the same room.

  2. To access remote players, you can loop through PhotonNetwork.PlayerList and exclude the local player.

  3. For example, to access the nicknames of all remote players:

  4.   
    foreach (Player remotePlayer in PhotonNetwork.PlayerListOthers)
    {
        string remotePlayerNickname = remotePlayer.NickName;
        // Do something with the remote player's nickname
    }
    

  5. You can also access custom properties set for remote players in a similar way:

  6.   
    foreach (Player remotePlayer in PhotonNetwork.PlayerListOthers)
    {
        object customProperty = remotePlayer.CustomProperties["PropertyName"];
        // Do something with the remote player's custom property
    }
    

Let's break down custom properties in (PUN) using a simple example:

  1. Example Scenario :
    Imagine you're developing a multiplayer game where players can choose different character classes (e.g., warrior, mage, archer). Each character class has unique attributes such as health, damage, and special abilities. You want to store these attributes for each player and synchronize them across all clients in the game.

  2. How Custom Properties Help :
    Custom properties in PUN allow you to attach additional data to players (or other networked objects) beyond the built-in properties like nickname and ID. In our example, we can use custom properties to store the attributes of each player's chosen character class.

  3. Simple Example :
    Let's say a player chooses the warrior class. We can store the warrior's attributes (e.g., health = 100, damage = 20) as custom properties for that player. When the player joins a game, their custom properties are synchronized with all other clients, ensuring that everyone sees the correct attributes for each player.

Here's how you can use custom properties in PUN :

1. Setting Custom Properties :

When a player selects a character class, you set the corresponding attributes as custom properties for that player.

  
ExitGames.Client.Photon.Hashtable warriorProps = new ExitGames.Client.Photon.Hashtable();
warriorProps["Health"] = 100;
warriorProps["Damage"] = 20;
PhotonNetwork.LocalPlayer.SetCustomProperties(warriorProps);

2. Accessing Custom Properties :

Other players can access the custom properties of each player in the room. For example, to get the health of a remote player:

  
int health = (int)remotePlayer.CustomProperties["Health"];

3. Synchronization :

Custom properties are automatically synchronized with all clients in the room. When a player's custom properties change (e.g., due to taking damage), those changes are propagated to all other clients.

Benefits of Custom Properties :

  1. Flexibility :
    Custom properties allow you to attach any type of data to players, giving you flexibility in designing your game's networked objects.

  2. Synchronization :
    Changes to custom properties are automatically synchronized across all clients, ensuring consistency in the game state.

  3. Efficiency :
    Custom properties are efficiently synchronized over the network, minimizing bandwidth usage.

In summary, custom properties in PUN are a powerful feature that allows you to store and synchronize additional data for players (or other networked objects) in your multiplayer game. They enable you to implement various gameplay mechanics and ensure a consistent experience for all players.


Here are a few more key concepts related to players and remote players in (PUN) that you should know:

1. Master Client :

  1. In every Photon room, one client is designated as the "Master Client."

  2. The Master Client is responsible for tasks such as creating and closing rooms, starting and stopping games, kicking players, and handling certain network-related tasks.

  3. By default, the first client that creates and joins the room becomes the Master Client. However, the Master Client can change if the original Master Client leaves the room.

  4. In some cases, you may want to change the Master Client programmatically. For example, if the current Master Client needs to leave the game or if you want to transfer control to another client temporarily.

  5. When the Master Client changes, it can have implications for ongoing gameplay and network synchronization. It's crucial to handle Master Client changes gracefully and update the game state accordingly to prevent inconsistencies or disruptions in gameplay.

The Master Client often plays a crucial role in enforcing game rules and ensuring consistency across all clients. For example, if the Master Client is responsible for spawning enemies or distributing loot, you must ensure that these actions are synchronized correctly across all clients. Lets see why ?

  1. Synchronization and Control :
    In a multiplayer game, all players need to see the same things happening at the same time.
    The Master Client is like the referee; it makes sure everyone sees the game the same way.
    By letting the Master Client handle important stuff, like adding enemies or giving out rewards, we make sure everyone's game stays fair and fun.

  2. Reduced Network Traffic :
    By having the Master Client handle certain actions, you can minimize network traffic and reduce the risk of conflicts or inconsistencies between clients.
    For example, if every client were allowed to spawn enemies independently, it could lead to discrepancies in the enemy positions or behaviors across different players' screens.

  3. Fairness and Performance :
    Having one player in charge also helps keep things fair.
    Plus, it helps keep the game running smoothly, especially if there are a lot of players.

2. Player IDs :

  1. Each player in a room is assigned a unique Player ID by Photon.

  2. Player IDs are useful for identifying individual players in the room, especially in scenarios where players may change their nicknames or disconnect and reconnect.

3. Player Lifecycle Events :

  1. PUN provides callbacks for various player lifecycle events, such as when a player joins or leaves the room, when custom properties are updated, etc.

  2. These callbacks allow you to respond to player-related events and update the game state accordingly.

4. Player Nicknames :

  1. Each player in a Photon room has a nickname that can be set and retrieved.

  2. Nicknames are useful for displaying player names in the game UI and communicating with other players.


Please read => Part 4 for further exploration of Photon Unity Networking features and implementation details.


I hope you found this blog post enjoyable!


More Blogs

See All