0

I am trying to understand the Photon Pun architecture. I cannot get the callback OnPhotonSerializeView() in 'IPunObservable' to be called.

What I have in my scene is

  • a GameObject called DataHandler at the top of the hierachy

  • DataHandler has a PhotonView and PhotonTransformView attached

  • DataHandler also has a script attached called DataHandler

  • The DataHandler script component has been dragged to the first observable in the PhotonView component (so it shows 'DataHandler (DataHandler)' as the observed component).

  • The DataHandler script implements IPunObservable, and the one method in that is

     public class DataHandler : MonoBehaviourPunCallbacks, IPunObservable
     {
     ....
    
         public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
         {
             Debug.Log("OnPhotonSerializeView(): " + (stream.IsWriting ? "writing" : "reading"));
         }
     ....
     }
    

Nothing shows up in the log when it is run.

However when built and run as a Windows EXE, connected with a room in place, it comes up with NullReferenceExceptions. The log shows:

    NullReferenceException: Object reference not set to an instance of an object
      at Photon.Pun.PhotonView.SerializeComponent (UnityEngine.Component component, Photon.Pun.PhotonStream stream, Photon.Pun.PhotonMessageInfo info) [0x0001e] in C:\Users\nick\Documents\Unity3D\PhotonPunTest\Assets\Photon\PhotonUnityNetworking\Code\PhotonView.cs:368 
      at Photon.Pun.PhotonView.SerializeView (Photon.Pun.PhotonStream stream, Photon.Pun.PhotonMessageInfo info) [0x00024] in C:\Users\nick\Documents\Unity3D\PhotonPunTest\Assets\Photon\PhotonUnityNetworking\Code\PhotonView.cs:330 
      at Photon.Pun.PhotonNetwork.OnSerializeWrite (Photon.Pun.PhotonView view) [0x00089] in C:\Users\nick\Documents\Unity3D\PhotonPunTest\Assets\Photon\PhotonUnityNetworking\Code\PhotonNetworkPart.cs:1593 
      at Photon.Pun.PhotonNetwork.RunViewUpdate () [0x000b3] in C:\Users\nick\Documents\Unity3D\PhotonPunTest\Assets\Photon\PhotonUnityNetworking\Code\PhotonNetworkPart.cs:1522 
      at Photon.Pun.PhotonHandler.LateUpdate () [0x00042] in C:\Users\nick\Documents\Unity3D\PhotonPunTest\Assets\Photon\PhotonUnityNetworking\Code\PhotonHandler.cs:155 
     
    (Filename: C:/Users/nick/Documents/Unity3D/PhotonPunTest/Assets/Photon/PhotonUnityNetworking/Code/PhotonView.cs Line: 368)

and the code around line 368 in PhotonView.cs is

    protected internal void SerializeComponent(Component component, PhotonStream stream, PhotonMessageInfo info)
    {
        IPunObservable observable = component as IPunObservable;
        if (observable != null)
        {
            observable.OnPhotonSerializeView(stream, info);
        }
        else
        {
            Debug.LogError("Observed scripts have to implement IPunObservable. "+ component + " does not. It is Type: " + component.GetType(), component.gameObject);
        }
    }

(The Debug.LogError line is 368).

Don't see what is wrong, though I assume component is probably null. The script is implementing IPunObservable. Can anyone help?

nmw01223
  • 1,611
  • 3
  • 23
  • 40
  • yes it sounds like `component` is `null` ... – derHugo Jun 24 '20 at 09:53
  • Yes. Any idea why? – nmw01223 Jun 24 '20 at 10:00
  • 1
    No, I don't see where your method gets called on ... but it seems to be PUN own code so maybe some Bug or at least unhandled case ... did you try to simply [debug your code](https://docs.unity3d.com/Manual/ManagedCodeDebugging.html) and set a breakpoint in that line so you can go through it step by step and see what exactly happens? – derHugo Jun 24 '20 at 10:00
  • I don't have the debugger set up, but I put a line "Debug.Log("SerializeComponent(): component is " + (component == null ? "null" : "not null"));" at the start of SerializeComponent() - never showed in the log, so I don't know what is going on. – nmw01223 Jun 24 '20 at 10:33

1 Answers1

0

The problem here is the photon logic about local player and master client. The rules about who can send are bound up in this.

In the end I found it much easier to go below the higher level 'helper' components (PhotonView etc) and send everything with the lowever level RaiseEvent() / OnEvent() mechanism.

See answer to previous question.

nmw01223
  • 1,611
  • 3
  • 23
  • 40