- Home /
Photon player RaiseEvent sending bools
Hi there, I'm having issues sending my bool across the network and I'm using the RaiseEvent from photon and I'm not sure I really understand it and that's why it's not working. My scenario is: there are 4 players and once a player hits a button then they ready up and that's it but I can't seem to send the 'playerXactivated' bool across.
Here is my code, if someone can help me understand that would be great!:)
#region Testing Variables
byte eventCode = 0;
bool reliable = true;
#endregion
#region Testing Methods
void OnEnable()
{
PhotonNetwork.OnEventCall += this.OnEvent;
}
void OnDisable()
{
PhotonNetwork.OnEventCall -= this.OnEvent;
}
private void OnEvent(byte _eventCode, object content, int senderid)
{
if(_eventCode == 0)
{
PhotonPlayer sender = PhotonPlayer.Find(senderid);
byte[] test = content as byte[];
for (int i = 0; i < test.Length; i++)
{
byte unitId = test[i];
}
}
}
#endregion
#region Public Methods
public void ThiefCollectionButton()
{
PhotonNetwork.RaiseEvent(eventCode, player1Activated = true, reliable, null);
canvasObj.SetActive(false);
}
Thanks again!
Answer by ChristianSimon · Mar 13, 2018 at 10:39 AM
Hi,
the problem is that you are sending just a boolean value but trying to cast it to an array of bytes in the OnEvent function, which obviously won't work at all. So in order to make this work you would have to do something like bool test = (bool) content;
.
However instead of using RaiseEvent in this case, I would recommend you using the Custom Player Properties. Whenever a client presses a certain button, he can use his Player Properties to let each other client know, that he is ready. This can be done by using the following code snippet for example:
ExitGames.Client.Photon.Hashtable properties = new ExitGames.Client.Photon.Hashtable() { { "PlayerIsReady", true } };
PhotonNetwork.player.SetCustomProperties(properties);
Whenever a client set his Player Properties, those are automatically updated on each other client as well. The client is notified about this when the callback void OnPhotonPlayerPropertiesChanged(object[] playerAndUpdatedProps)
is called. You can see how this callbacks works here.
The MasterClient can use this callback and iterates through the player list in order to see if all clients are ready (if you want this scenario).
Hi Simon, thanks for the reply much appreciated! However, I'm still having a little bit of an issue. I have tried using the custom player properties but still not sure on where I'm going wrong. Currently when a player hits the button to ready up then the bool is only activated on them and not send to the other players... Here is my code: So my start function looks like this:
properties.Add("Player1Ready", player1Activated);
properties.Add("Player2Ready", player2Activated);
properties.Add("Player3Ready", player3Activated);
properties.Add("Player4Ready", player4Activated);
PhotonNetwork.player.SetCustomProperties(properties);
And then when a player hits their button to ready up I do this:
PhotonNetwork.player.CustomProperties["Player1Ready"] = true;
PhotonNetwork.player.SetCustomProperties(properties);
Finally in the update statement im just checking if the bool has been activated then doing something. I had put the code in the OnPhotonPlayerPropertiesChanged method but that didnt seem to work as well...
if (player1Activated)
{
print("player1 active");
player1Button.SetActive(false);
}
Thanks again!
Don't modify Properties directly. Please use the given function for modifications ins$$anonymous$$d, to make sure, that those properties are synchronized correctly with all clients.
Another hint: use the callback to check for modified properties. This is much more efficient than checking this each time Unity's Update function runs. You also only have to do this on the $$anonymous$$asterClient, as he is the one to start the game, unless you want to show the state to all clients.