Search code examples
c#unity-game-enginemultiplayerphotoncustom-properties

PUN 2 Getting Custom Properties


I've recently taken on the task of custom properties in Photon. I have been able to figure out how to set the custom properties, but not get the custom properties. My hashtable is in my player controller script, while the place where I set (and where I want to get) properties is in a round loop script.

From RoundSystem:

private IEnumerator TeamBalance()
    {
        angelCount = Mathf.Floor(PhotonNetwork.PlayerList.Length * angelPercent);
        currentAngels = angelCount;
        currentPlayers = PhotonNetwork.PlayerList.Length;

        foreach (var item in PhotonNetwork.PlayerList)
        {
            var itemPhotonView = (PhotonView)item.TagObject;

            itemPhotonView.RPC("SetPlayerTeam", item, citiString);
        }

        for (int i = 0; i < angelCount;)
        {
            var item = PhotonNetwork.PlayerList[Random.Range(0, PhotonNetwork.PlayerList.Length)];
            var itemPhotonView = (PhotonView)item.TagObject;

            if (/* random player selected's, AKA, item's team == citiString */)
            {
                itemPhotonView.RPC("SetPlayerTeam", item, angelString);

                i++;
            }
        }

        yield return null;
        //the reason this is in an IEnumerator with 'yield return null'
        //is because I plan to add a waiting period once I figure this out
        //it's for the game loop
    }

From PlayerController:

[PunRPC]
        public void SetPlayerTeam(string teamString)
        {
            //in the class:     private ExitGames.Client.Photon.Hashtable playerProperties; 
            if (!playerProperties.ContainsKey("team"))
            {
                playerProperties.Add("team", teamString);
            }
            playerProperties["team"] = teamString;
            PhotonNetwork.LocalPlayer.SetCustomProperties(playerProperties);
        }

At the beginning of the round, a percentage (in this case 1/3) of players are chosen to be an "angel". The check here is needed because in cases of multiple angels, you don't want an already existing angel to count as a new change. (Also, it's probably important to known generally how to get custom properties if I'm going to be using them.) If I don't include the check in RoundSystem, the outcome is 2 citizens and 1 angel (in a test with 3 players). Also, if you see any spaghetti code that could be improved on, please don't hesitate to tell me. :)


Solution

  • Use Player.CustomProperties dictionary to access player's custom properties.

        foreach (var item in PhotonNetwork.PlayerList)
        {
            if (item.CustomProperties.ContainsKey("team"))
            {
                Debug.Log(item.CustomProperties["team"]);
            }
        }
    

    Also, the RoundSystem can implement IInRoomCallbacks interface and listen to OnPlayerPropertiesUpdate to catch the exact moment when the team gets updated. https://doc-api.photonengine.com/en/pun/v2/interface_photon_1_1_realtime_1_1_i_in_room_callbacks.html