简体   繁体   中英

PUN 2 Getting and Setting Custom Properties

I'm still relatively new to Photon since the depreciation of UNet. I'm having trouble getting and setting local custom properties. I'm trying to have two different teams (players and angels) be chosen. Each player starts as a spectator. A certain percentage of players are chosen to be the angels, and the rest are assigned as players. I can manage to get and set the property of a randomly chosen player, but I can't seem to assign the values for the remaining. The snippet of code is below.

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

        for (int i = 0; i < angelCount;)
        {
            int index = Random.Range(0, PhotonNetwork.PlayerList.Length);
            if (PhotonNetwork.PlayerList[index].CustomProperties["team"].ToString() == "spectator")
            {
                PhotonNetwork.PlayerList[index].CustomProperties["team"] = "angel";
                i++;
            }
        }

        foreach (var player in PhotonNetwork.PlayerList)
        {
            if (player.CustomProperties["team"].ToString() == "spectator")
            {
                player.CustomProperties["team"] = "player";
            }
        }

        yield return null;
    }

The end result for 3 players ends up picking 1 angel, but with 2 spectators still remaining.

You need to use Player.SetCustomProperties function to set properties instead of assigning them directly. This allows PUN to track what's been changed and update properly. https://doc-api.photonengine.com/en/pun/v2/class_photon_1_1_realtime_1_1_player.html#a0c1010eda4f775ff56f8c86b026be41e

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM