Can someone please help me convert this from PUN One to PUN Two?

Options
private void Awake ()
		{
			PhotonNetwork.OnEventCall += OnEvent;
		}

		private void OnEvent (byte eventCode, object content, int senderId)
		{
			Events ev = (Events)eventCode; 
			object[] eventData = (object[])content;

			PhotonView photonView = PhotonView.Find ((int)eventData [0]);
			if (photonView == null) {
				return;
			}
			switch (ev) {
			case Events.Invoke:
				string methodName = (string)eventData [1];
				object[] parameters = (object[])eventData [2];
				if (photonView != null) {
					Proxy.Invoke (photonView.gameObject, methodName, parameters);
				}
				break;
			case Events.Destroy:
				PhotonNetwork.Destroy (photonView.gameObject);
				break;
			}
		}

According to this documentation:
PhotonNetwork.OnEventCall is gone.
You have two options:
use PhotonNetwork.NetworkingClient.EventReceived(EventData) instead.
use IOnEventCallback.OnEvent(EventData)
https://doc.photonengine.com/en-us/pun/v2/getting-started/migration-notes

How do I convert the above code to Pun 2?

Comments

  • S_Oliver
    S_Oliver ✭✭✭
    Options
    HI
    public class ClassName : MonoBehaviour, IOnEventCallback
    {
    	private void OnEnable ()
    	{
    		PhotonNetwork.AddCallbackTarget(this);
    	}
    
    	private void OnDisable()
    	{
    		PhotonNetwork.RemoveCallbackTarget(this);
    	}
    
    	public void OnEvent(EventData photonEvent)
    	{
    		Events ev = (Events)photonEvent.Code;
    		object[] eventData = (object[])photonEvent.CustomData;
    
    		PhotonView photonView = PhotonView.Find ((int)eventData [0]);
    		if (photonView == null) {
    			return;
    		}
    		switch (ev) {
    			case Events.Invoke:
    				string methodName = (string)eventData [1];
    				object[] parameters = (object[])eventData [2];
    				if (photonView != null) {
    					Proxy.Invoke (photonView.gameObject, methodName, parameters);
    				}
    				break;
    			case Events.Destroy:
    				PhotonNetwork.Destroy (photonView.gameObject);
    				break;
    		}
    	}
    }
    
    You have to implement the interface
    IOnEventCallback
    
    and register the callback with
    PhotonNetwork.AddCallbackTarget(this);
    
    to make it work.
    And the event stuff is warpped in an object called EventData , so you get the code and your data out of this.
  • JohnTube
    JohnTube ✭✭✭✭✭
    edited April 2020
    Options
    Hi @markashburner,

    Thank you for choosing Photon!

    Here:
    private void Awake()
            {
                PhotonNetwork.NetworkingClient.EventReceived += this.OnEvent;
            }
    
            private void OnEvent(EventData eventData)
            {
                if (eventData.Code < 200)
                {
                    this.OnEvent(eventData.Code, eventData.CustomData, eventData.Sender);
                }
            }
    
            private void OnEvent(byte eventCode, object content, int senderId)
            {
    
            }
    

    I suggest you move OnEvent callback registration to OnEnable (call base.OnEnable) if you extend MonoBehaviourPunCallbacks and unsubcribe (-=) in OnDisable (also call base.OnDisable if needed).
  • markashburner
    edited April 2020
    Options
    S_Oliver wrote: »
    HI
    public class ClassName : MonoBehaviour, IOnEventCallback
    {
    	private void OnEnable ()
    	{
    		PhotonNetwork.AddCallbackTarget(this);
    	}
    
    	private void OnDisable()
    	{
    		PhotonNetwork.RemoveCallbackTarget(this);
    	}
    
    	public void OnEvent(EventData photonEvent)
    	{
    		Events ev = (Events)photonEvent.Code;
    		object[] eventData = (object[])photonEvent.CustomData;
    
    		PhotonView photonView = PhotonView.Find ((int)eventData [0]);
    		if (photonView == null) {
    			return;
    		}
    		switch (ev) {
    			case Events.Invoke:
    				string methodName = (string)eventData [1];
    				object[] parameters = (object[])eventData [2];
    				if (photonView != null) {
    					Proxy.Invoke (photonView.gameObject, methodName, parameters);
    				}
    				break;
    			case Events.Destroy:
    				PhotonNetwork.Destroy (photonView.gameObject);
    				break;
    		}
    	}
    }
    
    You have to implement the interface
    IOnEventCallback
    
    and register the callback with
    PhotonNetwork.AddCallbackTarget(this);
    
    to make it work.
    And the event stuff is warpped in an object called EventData , so you get the code and your data out of this.

    Thank you.

    However I have an issue with implementing the interface because my script already registers another interface.
    #if PUN
    using UnityEngine;
    using System;
    
    using System.Collections;
    using System.Collections.Generic;
    using Unitycoding.UIWidgets;
    using Photon.Realtime;
    using Photon.Pun;
    using System.Linq;
    
    namespace Unitycoding
    {
    	public class PhotonNetworkingProxy : MonoBehaviour, IProxyHandler
        {
    
    
        }
    }
    
    

    How would I register a second interface?
  • S_Oliver
    S_Oliver ✭✭✭
    Options
    Just add it afterwards
    public class PhotonNetworkingProxy : MonoBehaviour, IProxyHandler,IOnEventCallback
    
    In c# you can implement more than one interface but can only inherit from one class.
  • markashburner
    edited April 2020
    Options
    Ok I managed to solve the post above by implementing
    public interface IOnEventCallback { }
    
        public class PhotonNetworkingProxy : MonoBehaviour, IProxyHandler, IOnEventCallback
    

    However in this code from you S_Oliver

    I am recieving this red squiggly line under EventData. I have used both Photon.Realtime;
    and Photon.Pun namespaces

    I1vn4S0.jpg

  • S_Oliver
    S_Oliver ✭✭✭
    edited April 2020
    Options
    You have to implement the right namespace
    using Photon.Realtime;
    
    and dont declare the interface yourself
    public interface IOnEventCallback { }
    
    it is a part of the photon asset, delete this line^^

    Edit:
    Like this.
    using ExitGames.Client.Photon;
    using Photon.Pun;
    using Photon.Realtime;
    using UnityEngine;
    
    public class ClassName : MonoBehaviour, IOnEventCallback
    {
    	private void OnEnable ()
    	{
    		PhotonNetwork.AddCallbackTarget(this);
    	}
    
    	private void OnDisable()
    	{
    		PhotonNetwork.RemoveCallbackTarget(this);
    	}
    
    	public void OnEvent(EventData photonEvent)
    	{
    		Events ev = (Events)photonEvent.Code;
    		object[] eventData = (object[])photonEvent.CustomData;
    
    		PhotonView photonView = PhotonView.Find ((int)eventData [0]);
    		if (photonView == null) {
    			return;
    		}
    		switch (ev) {
    			case Events.Invoke:
    				string methodName = (string)eventData [1];
    				object[] parameters = (object[])eventData [2];
    				if (photonView != null) {
    					Proxy.Invoke (photonView.gameObject, methodName, parameters);
    				}
    				break;
    			case Events.Destroy:
    				PhotonNetwork.Destroy (photonView.gameObject);
    				break;
    		}
    	}
    }
    
  • Chandlerya
    Options
    I have this same issue, if I remove public interface IOnEventCallback { }, I get this error:
    Assets\Race Online\Scripts\Network\UI.cs(10,34): error CS0535: 'UI' does not implement interface member 'IOnEventCallback.OnEvent(EventData)'
    and this error
    Assets\Race Online\Scripts\Network\UI.cs(92,25): error CS0246: The type or namespace name 'EventData' could not be found (are you missing a using directive or an assembly reference?)

    When I add public interface IOnEventCallback { }I only get this error: Assets\Race Online\Scripts\Network\UI.cs(92,25): error CS0246: The type or namespace name 'EventData' could not be found (are you missing a using directive or an assembly reference?)

    I have these declared:
    using UnityEngine;
    using UnityEngine.UI;
    using Photon.Pun;
    using Photon.Realtime;
    using UnityEditor.UI;

    using Hashtable = ExitGames.Client.Photon.Hashtable;


    Any ideas how to fix this?

    Thanks for looking