I want to create a multiplayer application using Photon (Pun2). After a player joins the room, I want to hide some buttons and display others for all the players in that room. At the moment I have this function:
public override void OnPlayerEnteredRoom(Player newPlayer)
{
print(newPlayer); // #02
if (PhotonNetwork.CurrentRoom.PlayerCount == 2 && PhotonNetwork.IsMasterClient)
{
print(PhotonNetwork.CurrentRoom.PlayerCount + "/2 Starting game...");
searchingBtn.SetActive(false); // hide this button
findMatchBtn.SetActive(false); // hide this button
scanningBtn.SetActive(true); // display this button
}
}
But this won't work, because only for 1 of the 2 players the buttons will change, for the other one they will stay the same.
I saw some examples on the Internet, where in this function OnPlayerEnteredRoom
it's called PhotonNetwork.LoadLevel(scene_index)
, but at that moment, I don't want to load another scene, I want to stay in the current scene and just hide/display some buttons. Is this even possible?
EDIT: Here is the final version
public override void OnJoinedRoom()
{
Debug.Log("You joined the room.");
Debug.Log(PhotonNetwork.CurrentRoom.PlayerCount);
Debug.Log(PhotonNetwork.IsMasterClient);
UpdateButtons();
}
public override void OnPlayerEnteredRoom(Player newPlayer)
{
Debug.Log("Other players joined the room.");
if (PhotonNetwork.CurrentRoom.PlayerCount > 1 && PhotonNetwork.IsMasterClient)
{
Debug.Log(PhotonNetwork.CurrentRoom.PlayerCount + "/2 Starting game...");
UpdateButtons();
}
}
private void UpdateButtons()
{
searchingBtn.SetActive(false); // hide this button
findMatchBtn.SetActive(false); // hide this button
scanningBtn.SetActive(true); // display this button
}
From Photon Documentation:
virtual void OnJoinedRoom ()
//Called when the LoadBalancingClient entered a room, no matter if this client
//created it or simply joined.
virtual void OnPlayerEnteredRoom (Player newPlayer)
//Called when a remote player entered the room. This Player is already added to
//the playerlist.
With this knowledge, OnPlayerEnteredRoom
is only called when a remote player joins the room. So in order to solve your problem, you'll need to call your code in the OnJoinedRoom
callback for the local client and OnPlayerEnteredRoom
when a remote player joins.
In summary, your new code should look like this:
public override void OnPlayerEnteredRoom(Player newPlayer)
{
UpdateButtons(newPlayer);
}
public override void OnJoinedRoom(Player player)
{
UpdateButtons(player);
}
private void UpdateButtons(Player player)
{
if (PhotonNetwork.CurrentRoom.PlayerCount > 1 && PhotonNetwork.IsMasterClient)
{
print(PhotonNetwork.CurrentRoom.PlayerCount + "/2 Starting game...");
searchingBtn.SetActive(false); // hide this button
findMatchBtn.SetActive(false); // hide this button
scanningBtn.SetActive(true); // display this button
}
}