CustomProperties on current room players not correct for new players entering the room
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).
CustomProperties on current room players not correct for new players entering the room
eligolf
2021-11-10 17:39:06
Hi,
I have a problem with setting and getting custom properties on players. So far I am able for the current players in the room to get the correct info from other players properties. The problem is that new players don't seem to get the updated property values that was changed on players in the room before they entered the room.
More specifically I am trying to create a player list in my room and all have to press "ready" for master to be able to start the game. All players have an image next to their name in the player list to let people know who is ready and who is not.
When a player presses Ready button the function below runs on all players in the room.
[PunRPC]
private void RPC_ChangeReadyState(Player player)
{
int index = _listings.FindIndex(x => x.Player == player);
if (index != -1)
{
bool _newReadyState = !(bool)player.CustomProperties["ready"];
_listings[index].Player.CustomProperties["ready"] = _newReadyState;
_listings[index].UpdateReadyText(_newReadyState);
}
// Let master check if the game is now ready to be started
FindObjectOfType<CurrentRoomCanvas>().CheckIfAllPlayersReady(_listings);
}
Where _listings holds all player listing items and each listing is connected to a specific player. When a player enters the room I run the following function locally, going through all players in PhotonNetwork.CurrentRoom.Players and adding each one with this function:
private void AddPlayerListing(Player player)
{
PlayerListing listing = Instantiate(_playerListing, _content);
listing.SetPlayerInfo(player);
_listings.Add(listing);
}
In SetPlayerInfo is where I find all players properties and this is where it doesn't detect the players CustomProperty which was set before a player joined:
public void SetPlayerInfo(Player player)
{
// Update the player
Player = player;
playerNameText.text = player.NickName;
// THIS IS THE IF STATEMENT THAT DOESN'T DETET CORRECTLY
// Check if player is ready or not
if ((bool)player.CustomProperties["ready"])
{
playerReadyText.text = "Ready!";
readyPanel.GetComponent<Image>().color = readyColor;
}
else
{
playerReadyText.text = "Waiting...";
readyPanel.GetComponent<Image>().color = notReadyColor;
}
}
All players now go into the else statement no matter if they previously set themselves to ready.
Do I need to do something special for new players entering the room to take part of the already entered players updated properties?
Comments
Hi @eligolf,
I'm sorry to say that you are tried to reinvent the wheel as you missed how custom properties should be used properly.
Instead of using RPCs to set them you should use SetCustomProperties methods instead and wait for callbacks OnPlayerPropertiesUpdate or OnRoomPropertiesUpdate. Never set methods directly via CustomProperties Hashtable, use it only to read values.
Hi,
Thanks for the reply, I get what you are saying and I will probably rewrite the code to fit that after googling around for some time.
The question still stands though, how do new players that enteres the room get the previously changed CustomProperties from players already in the room? The custom properties doesn't seem to update for the new player entering, only the ones already in the room.
Hi @eligolf,
Please use player.SetCustomProperties method and you will see that it works as expected.
This is all done internally for you in client SDK under the hood so you don't worry about it.
But here are a few notes (I can go into further deeper details if you need):
For every remote player joins the room there is a Join event broadcast which contains the properties of that joiner (unless you create rooms with roomOptions.SuppressRoomEvents).
When local player joins the room the Join operation response contains the list of actors already joined with their respective properties
Thanks John, it works great now! You are a life saver.
Back to top