Set Player a random Colour on joining a room?

Aight, so i have a list of colours that a player can have, and when a player joins, i want to assign him a random colour from that list, then remove the colour from the list so nobody has the same colour. For some reason, i cant sync up the player list and I'm completely stuffed on how to do this. can someone point me in the right direction?

Best Answer

Answers

  • Okay, so in my opinion i think you can get away with this with doing a for loop on all the players in the room and checking their custom properties such as "playerColor" and check that against your color palette, then based upon the result you can apply the color to your player and set its custom color property so a new player can reference it.

    Ex:
    List<string> allowedColors = new List<string> { "red", "green", "blue" };
                foreach (PhotonPlayer player in PhotonNetwork.playerList) {
                    if (player != PhotonNetwork.player) {
                        string playerColor = (string)player.CustomProperties["playerColor"];
                        if (allowedColors.Contains(playerColor)) {
                            allowedColors.Remove(playerColor);
                        }
                    }
                }
                int randomColor = Random.Range(0, allowedColors.Count);
                PhotonNetwork.player.SetCustomProperties(new ExitGames.Client.Photon.Hashtable() { { "playerColor", allowedColors[randomColor] } });
  • @Gage_IAG how can i use this? like where should i add the foreach loop and setting properties?
  • You can add it in an initialize method locally on the player or wherever your player instantiations happening. You can copy paste that into a function, no need to make it an RPC either since SetCustomProperties handles all that for you.

    The only thing extra you need to do is just convert that string value to a Color via whatever logic you choose to correlate it to an actual color.
  • @Gage_IAG i did some coding, and i just realized that i need to be able to edit the list in the inspector instead of creating it in code, how can i make this sync with the other players?
  • I've asked a similar question to this for month. Based on the responses it appears to not be possible to assign a unique attribute from a list to a player upon joining a room with Photon.
  • Your case was a little more complex, as you had a pre-defined sequence in which you were setting up entities / players.

    In general, it's possible to use the PlayerNumbering / PlayerIndexing for this sort of setup.
    If you define the colors in the inspector, you don't need to sync them. All builds will have them. Alternatively, if created via code, you could store them in Custom Room Properties so everyone can access them.
    See: https://doc.photonengine.com/en-us/pun/v2/gameplay/synchronization-and-state
  • @LeytonMate If youre wanting a centralized data source then you want to go with using RoomProperties as @Tobias stated.

    Heres an example of a use case for you:
            void SetRoomData() {
                RoomData data = GetRoomData();
                int randColor = Random.Range(0, data.allowedColors.Count);
                string color = data.allowedColors[randColor];
                data.allowedColors.RemoveAt(randColor); //Do your randomize code here
                PhotonNetwork.room.SetCustomProperties(new ExitGames.Client.Photon.Hashtable() { { "RoomData", JsonUtility.ToJson(data) } });
            }
    
            RoomData GetRoomData() {
                RoomData data = JsonUtility.FromJson<RoomData>(PhotonNetwork.room.CustomProperties["RoomData"].ToString());
                return data;
            }
    
            [System.Serializable]
            public class RoomData {
                public List<string> allowedColors = new List<string>() { "red", "green", "blue" };
            }
  • Tobias said:

    Your case was a little more complex, as you had a pre-defined sequence in which you were setting up entities / players.

    In general, it's possible to use the PlayerNumbering / PlayerIndexing for this sort of setup.
    If you define the colors in the inspector, you don't need to sync them. All builds will have them. Alternatively, if created via code, you could store them in Custom Room Properties so everyone can access them.
    See: https://doc.photonengine.com/en-us/pun/v2/gameplay/synchronization-and-state

    Hi Tobias.

    I'm not sure I understand the difference. This poster has a list of colors he wants to select from, I have a list of numbers (indices).
  • @Spektre try moving this to a different thread thx x.
  • @Gage_IAG how would i call the methods?
  • All you need to do is call the SetRoomData() method when you join as a player into the room. From there, just access the color string value from it as im doing nothing with that variable inside it.
  • oh yeah aight. cheers!
  • @Gage_IAG I get a NullReference error when joining, For some reason the GetRoomData method isn't working?
  • As, I mentioned...
  • Gage_IAG said:

    Okay, so in my opinion i think you can get away with this with doing a for loop on all the players in the room and checking their custom properties such as "playerColor" and check that against your color palette, then based upon the result you can apply the color to your player and set its custom color property so a new player can reference it.

    Ex:

    List<string> allowedColors = new List<string> { "red", "green", "blue" };
                foreach (PhotonPlayer player in PhotonNetwork.playerList) {
                    if (player != PhotonNetwork.player) {
                        string playerColor = (string)player.CustomProperties["playerColor"];
                        if (allowedColors.Contains(playerColor)) {
                            allowedColors.Remove(playerColor);
                        }
                    }
                }
                int randomColor = Random.Range(0, allowedColors.Count);
                PhotonNetwork.player.SetCustomProperties(new ExitGames.Client.Photon.Hashtable() { { "playerColor", allowedColors[randomColor] } });
    I think this will run into problem when two players join close to one another.
  • @Spektre It should be fine as the room properties are sent similar to an RPC call. However, if all players are joining at once, you can just let the master client handle the distribution of the colors and making the calls.