- Home /
PUN Update countOfPlayersOnMaster to all clients?
Hello,
I have an UI-text at the lobby which displays the current connected playercount of the masterserver. The text gets set when the user is joining the lobby.
void OnJoinedLobby(){
text_onlinePlayers.text = "Online: "+ PhotonNetwork.countOfPlayersOnMaster.ToString();
}
This way the first connected client will have "Online: 1". I start now a second instance of the game, connecting to the masterserver and its text will have "Online: 2" written, but when we look back to the first client, its text will have still "Online: 1", because it is not updating for all clients.
Therefore I call PunRPC to update the text on every client everytime when someone connects or disconnects to the masterserver.
void OnJoinedLobby(){
RefreshOnlineCount();
}
void OnDisconnectedFromPhoton(){
RefreshOnlineCount();
}
void RefreshOnlineCount(){
pv.RPC("RefreshOnlineCount_RPC", PhotonTargets.All, null);
}
[PunRPC]
void RefreshOnlineCount_RPC(){
text_onlinePlayers.text = "Online: " + PhotonNetwork.countOfPlayersOnMaster.ToString();
}
This is somehow not working: Error at
pv.RPC("RefreshOnlineCount_RPC", PhotonTargets.All, null);
Answer by ChristianSimon · Sep 27, 2016 at 08:26 AM
There are multiple problems with your code. First is that you can not send RPCs when you didn't have joined a room before. Photon does not allow sending RPCs from inside a lobby. Second is that when OnDisconnectedFromPhoton()
is called, you are already disconnected. Means you can not send RPCs here either. Some solution you can think about: When connected to Photon (check by implementing OnConnectedToPhoton()
) you can start a co-routine which updates text_onlinePlayers.text
value in a fixed interval, e.g. every two seconds. Make sure to stop that co-routine when leaving the lobby.