On clicking A Button in a current 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).
On clicking A Button in a current Room
shemtom
2021-10-18 02:19:00
So I have created 3 scenes: 1 . the lobby, 2. a scene for players to enter their specific data, and 3. a waiting room. Whenever the players transition in the 2nd scene and one player clicks a button automatically the others are transitioned without them being able to continue filling in their details. I know there is a demo scene explaining this aspect called the Asteroids but I don't understand the concept well.
I would love to get simple feedback on how to solve this problem.
I had this idea but it is not that complete:
for each (KeyValuePair<int, Player> player in PhotonNetwork.CurrentRoom.Players)
{
//how do I set an if statement to know if each player in that room has clicked a button
IamReadyButton.onClick.AddListener(() =>{
//load scene...
});
}
Comments
[Deleted User]
2021-10-18 03:18:02
Hi, there are many approaches to a problem like this.
Your problem is essentially that you have no way of knowing whether everyone else in the room has clicked the button, correct? Now in a normal circumstance you would have to make every client send a network event (Rpc or regular event) which broadcasts the fact they have indeed finished specifying their data. Then you would need to store that somewhere. You can take this approach if you want (and this is usually better and safer since people control their own events). However, if this is just a prototype then player properties are configured for this sort of thing already.
While a player is configuring their data on scene 2, you can set a flag in their custom properties, like so:
PhotonNetwork.LocalPlayer.SetCustomProperties(new Hashtable
{
{ "Configuring", true }
});
Now when you're iterating over all the players, you can check their properties to see if they're all ready:
var isEveryoneReady = true;
foreach(var player in PhotonNetwork.CurrentRoom.Players)
{
var props = player.Value.CustomProperties;
if (props.ContainsKey("Configuring") && props["Configuring"] is bool)
{
if ((bool)props["Configuring"])
{
isEveryoneReady = false;
break;
}
}
}
Debug.Log(isEveryoneReady ? "Ready" : "Not ready");
Whenever someone clicks the "ready" button, you can swap their "Configuring" property to false, to signal that they are no longer waiting.
Like I explained above, eventually you will want to transition into making your own RPC/Event for this sort of thing. Photon doesn't block people from editing other people's properties, so there's no authority control there if you use custom properties.
Sorry for the late reply, I was trying to understand the RPC and how it functions. I think using the RPC would be a great idea although I find it quite challenging since there are few tutorials on this particular topic. I have the picture but proceeding on in the code is where I end up confused. If it would be great to guide me and at least give me a hint on using RPC. This would really help me.
RPCs are a basic topic and are mentioned in practically any tutorial there is.
Read and code along the PUN Basics Tutorial, if you like.
Thanks, Tobias...As I was told there were different approaches to accomplish this particular problem. I tried using RPC but it required a photon view component I, therefore, figured out that the best way was through custom properties. So I typed this script but I adjusted it in a way that it was through an int but not a bool where when a player clicks a button then a debug.log shows how many clicked the button and if the players in the room match with the players who clicked the button then start the countdown to transition them to the game scene. I also get some logic errors...
When the other client (in Development Build) clicks the I am ready button it does not show the debug.log unless I click it (Local Player). Does this make sense?
using Hashtable = ExitGames.Client.Photon.Hashtable;
//Variables
public int ReadyPlayers = 0;
//added this to the button on the inspector : onclick event
public void OnClickIamReady()
{
ReadyPlayers++;
Hashtable hash = new Hashtable();
hash.Add("PlayerReady", ReadyPlayers);
PhotonNetwork.LocalPlayer.SetCustomProperties(hash);
//when a player clicks this button deactivate it
IamReadyButton.gameObject.SetActive(false);
foreach (Player player in PhotonNetwork.PlayerList)
{
Debug.Log(player.NickName.ToString() + " " + " is Ready. " + " " + " Players Who are Ready : " + player.CustomProperties["PlayerReady"].ToString());
}
}
https://stackoverflow.com/questions/69785435/onclick-i-am-ready-button-using-photon
Got an answer.
Back to top