Missing something with RaiseEvent setup?
Options
Here is where we register our test event:
Here is where we attempt to trigger the eventhandler:
First off, using debug.log we've verified that the eventhandler is getting registered on the master client, and that the block where PhotonNetwork.RaiseEvent is executing, but OnEventRaised never gets called.
Secondly, the API doc states the 4th argument is a PhotonPlayer type, but it's actually requiring and int. Third, the object eventContent is supposed to be optional, but if there isn't something there the game won't compile because there's no overload for RaiseEvent with 3 arguments. Anyway, the first issue is the most important so no worries about the other two... If more info is needed please ask, but according to the docs this should be working.
public class GameEventHandler : MonoBehaviour { // Use this for initialization void Start () { if (PhotonNetwork.isMasterClient){ Debug.Log("OnEventRaised registered."); PhotonNetwork.OnEventCall += this.OnEventRaised; } } public void OnEventRaised(byte eventCode, object content, int senderID){ Debug.Log(string.Format("OnEventRaised: {0}, {1}, {2}", eventCode, content, senderID)); } }
Here is where we attempt to trigger the eventhandler:
public void PrimaryAction(){ if (this.shotTimer >= this.shotDelay){ audio.Play(); this.shotTimer = 0.0f; Ray mouseRay = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0)); RaycastHit hitInfo; if (Physics.Raycast(mouseRay, out hitInfo)){ PhotonNetwork.RaiseEvent((byte)GameEventCode.TestEvent, "blank content?", true, RaiseEventOptions.Default); Debug.Log("RaisEvent called"); } Transform camera = transform.Find("MainCamera"); camera.RotateAround(transform.position, transform.right, -1.0f); this.lastGunAimPos = camera.localEulerAngles; } }
First off, using debug.log we've verified that the eventhandler is getting registered on the master client, and that the block where PhotonNetwork.RaiseEvent is executing, but OnEventRaised never gets called.
Secondly, the API doc states the 4th argument is a PhotonPlayer type, but it's actually requiring and int. Third, the object eventContent is supposed to be optional, but if there isn't something there the game won't compile because there's no overload for RaiseEvent with 3 arguments. Anyway, the first issue is the most important so no worries about the other two... If more info is needed please ask, but according to the docs this should be working.
0
Comments
-
Your code looks ok except that PhotonNetwork.isMasterClient is not always defined when Start() called. But looks like it is in your case.
What is value of GameEventCode.TestEvent? It should be < 200.
Make sure that you call event from non-master client since only master receives and events are not sent to client itself by default.
Currently PhotonNetwork has only one RaiseEvent method:
void RaiseEvent(byte eventCode, object eventContent, bool sendReliable, RaiseEventOptions options);
Set eventContent = null to skip content.
Use 4th parameter for setting RaiseEvent options.0 -
I will update the RaiseEvent documentation for the next release. The RaiseEventOptions are not optional as parameter but can be null.
I will also check the other pieces.
Let us know what your event code is. This should be the only place where it can go wrong. Anything in the logs?0 -
PhotonNetwork.isMasterClient is not always defined when Start() called. But looks like it is in your case.What is value of GameEventCode.TestEvent? It should be < 200.Make sure that you call event from non-master client since only master receives and events are not sent to client itself by default.0
-
1. Client can't figure out is it master or not until it enters the room. So wait for OnJoinedRoom message.
2. Use any byte form 1-199 range
3. By default event sent to all clients except sender (PhotonTargets.Others). This saves bandwidth and makes sender more responsive to it's events (if it handles events at send time). Set RaiseEventOptions.Receivers to PhotonTargets.All for sending to all clients including sender.
Note: you may try debugging events w/o PhotonNetwork.isMasterClient filter first since this is possible reason for at least two issues in your code (p 1. and 3.)0