Set Player a random Colour on joining a room?
Best Answer
-
@LeytonMate I see, its probably because theres no room properties initialized before making that call. You can set the room properties using a similar process:LeytonMate said:@Gage_IAG I get a NullReference error when joining, For some reason the GetRoomData method isn't working?
---void InitializeRoomData() { PhotonNetwork.room.SetCustomProperties(new ExitGames.Client.Photon.Hashtable() { { "RoomData", JsonUtility.ToJson(new RoomData()) } }); }
---
Just remember, youd want to call this when the room is created, so that way anyone who joins already has this data key/value pair. (ex: Call inside your lobby creation script or game start script)5
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] } });
1 -
@Gage_IAG how can i use this? like where should i add the foreach loop and setting properties?0
-
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.0 -
@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?0
-
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.0
-
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-state0 -
@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" }; }
1 -
Hi Tobias.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
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).0 -
@Spektre try moving this to a different thread thx x.0
-
@Gage_IAG how would i call the methods?0
-
All you need to do is call the
SetRoomData()
method when youjoin
as a player into theroom
. From there, just access thecolor
string value from it as im doing nothing with that variable inside it.0 -
oh yeah aight. cheers!0
-
@Gage_IAG I get a NullReference error when joining, For some reason the GetRoomData method isn't working?0
-
As, I mentioned...
0 -
I think this will run into problem when two players join close to one another.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] } });
0 -
@LeytonMate I see, its probably because theres no room properties initialized before making that call. You can set the room properties using a similar process:LeytonMate said:@Gage_IAG I get a NullReference error when joining, For some reason the GetRoomData method isn't working?
---void InitializeRoomData() { PhotonNetwork.room.SetCustomProperties(new ExitGames.Client.Photon.Hashtable() { { "RoomData", JsonUtility.ToJson(new RoomData()) } }); }
---
Just remember, youd want to call this when the room is created, so that way anyone who joins already has this data key/value pair. (ex: Call inside your lobby creation script or game start script)5