RaiseEvents intercepts events and PUNCallbacks not called

Options

Hello Everybody,

I am using PUN2 on Unity 2019.3.0 and I am facing a quite simple problem (once I found what was happening ahah).

I have a class managing actions when player entering/leaving a room. In order to do that, the class inherits MonoBehaviourPunCallbacks and I use :

  • public override void OnPlayerEnteredRoom(Player newPlayer)
  • public override void OnPlayerLeftRoom(Player newPlayer)

This worked fine !


After that, I needed the MasterClient of the room to send actions to other players in the room. I tried to use RaiseEvent by using a random available byte number (<200) for eventcode and registering a new callback to manage this

public override void OnEnable()
{
    PhotonNetwork.NetworkingClient.EventReceived += NetworkingClient_EventReceived;
}

public override void OnDisable()
{
    PhotonNetwork.NetworkingClient.EventReceived -= NetworkingClient_EventReceived;
}

private void NetworkingClient_EventReceived(EventData obj)
{
    if (obj.Code == MY_EVENT_CODE)
    {
        // Do Stuff
    }
}

Now the function NetworkingClient_EventReceived is perfectly called but it also intercepts other callbacks of Photon especially in my case "A player entered" (code 255) or "A player left"

Result : OnPlayerEnteredRoom & OnPlayerLeftRoom are not called anymore.

I assume it is not a bug at all and it seems logical to me but I miss the code to tell "call the other callbacks, I am not the one you need". I could test the event code received and call myself the functions but it seems not to be the right way as I would need to get the Player variable.


As I have the problem on another class running a lots of custom events, I am using a switch :

switch (obj.Code)
{
    case MY_CODE_1:
        // Do Something
        break;

    case MY_CODE_2:
        // Do Something Else

    default:
        // I search a code to put here to reroute the event
        break;
}


I hope my explanations are clear.

Thanks to read and to help 😊

Comments

  • JohnTube
    JohnTube ✭✭✭✭✭
    edited December 2019
    Options

    Hi @sthill,

    Thank you for choosing Photon!

    As we already mention in the documentation page:

    if you extend MonoBehaviourPunCallbacks make sure to call the base class methods if you override OnEnable() and OnDisable()

    public override void OnEnable()
    {
        base.OnEnable();
        PhotonNetwork.NetworkingClient.EventReceived += NetworkingClient_EventReceived;
    }
    
    public override void OnDisable()
    {
        base.OnDisable();
        PhotonNetwork.NetworkingClient.EventReceived -= NetworkingClient_EventReceived;
    }
    


  • sthill
    Options

    Perfect, it works like a charm.

    Sorry, I've already read this in another context but I had not made the connection.

    Thank you for the answer and for Photon globally which is very good !