Current room's custom properties aren't changing
The whole answer can be found below.
Try Our
Documentation
Please check if you can find an answer in our extensive documentation on PUN.
Join Us
on Discord
Meet and talk to our staff and the entire Photon-Community via Discord.
Read More on
Stack Overflow
Find more information on Stack Overflow (for Circle members only).
Current room's custom properties aren't changing
kevin32
2022-03-29 20:02:42
[Unity 2020.3.32 & PUN 2 2.39]
Hello!
First of all, sorry for bad english and dumb mistakes due to my inexperience with photon.
So I've been trying to make a little multiplayer game to play with my friends. It uses a procedural generated map, so I need to have a common seed among the players in order to have this procedural generated map to be constant.
To set this seed, I've found the most convenient way to store it is to use CurrentRoom.CustomProperties, but I've been having problems with this, so I've been troubleshooting and searching in the internet for solutions during the last couple of days and I've come up with two "solutions" to this problem, but each one of them have their own problems;
#1, the problem with this one is that the custom properties aren't changing at all:
Hashtable hash = new Hashtable();
....
if(PhotonNetwork.IsMasterClient)
{
int tempSeed = UnityEngine.Random.Range(0, 100000);
hash["seed"] = tempSeed ;
PhotonNetwork.CurrentRoom.SetCustomProperties(hash);
}
seed = (int)PhotonNetwork.CurrentRoom.CustomProperties["seed"];
#2, the problem with this one is that only the client that sets the seed (MasterClient in this case), is able to read it, and I've read in some places that changing the PhotonNetwork.CurrentRoom.CustomProperties[(string)] directly is not recommended:
Hashtable hash = new Hashtable();
....
if(PhotonNetwork.IsMasterClient)
{
int tempSeed = UnityEngine.Random.Range(0, 100000);
PhotonNetwork.CurrentRoom.CustomProperties["seed"] = tempSeed ;
}
seed = (int)PhotonNetwork.CurrentRoom.CustomProperties["seed"];
Thanks for reading! Any help is appreciated :)
Comments
Approach #1 should be fine. Make sure to only set the seed once, not every frame (don't do this in Update). The value is no longer changed immediately on the client which sets it, so all clients get the prop value from the server at about the same time. There is a callback for when the property changed.
In the PUN 2 package, we've got a Procedural Demo. This should do what you need.
Back to top