PhotonNetwork.Disconnect() Problem

Options
Hi.
I have a problem. I wanna load a level after getting diconnected from a room. But i can't do this
PhotonNetwork.Disconnect();
Application.Loadlevel();
Because if i do that, i doesn't disconnect fast enough and loads the level without getting disconnected. I also can't use this
void OnPhotonPlayerDisconnected()
{
    Application.Loadlevel();
}
Since that message only gets send to people inside the room that isn't disconnected. So i'm lost and can't find a logic to this one. Could use a timer, but if people lag or there is a server lag or something, that isn't gonna work. How could i make this?

Comments

  • dreamora
    Options
    There are two ways, depending on if you want to visualize it and how:

    1. Use OnDisconnectedFromPhoton instead of OnPhotonPlayerDisconnected (see the Enums.cs file in the photon unity networking/plugins folder)

    2. What you need to do is wait for connected to no longer be true and once thats the case you load the new level.
    Below is a quick hacked together code, not sure if connected really is present like that as my PUN version ain't the stock one anymore but it should be simple enough to understand :)

    public void SwitchLevel (string level)
    {
      StartCoroutine (DoSwitchLevel(level));
    }
    
    IEnumerator DoSwitchLevel (string level)
    {
      PhotonNetwork.Disconnect ();
      while (PhotonNetwork.connected)
        yield return null;
      Application.LoadLevel(level);
    }
    
  • Thank you! Worked out perfect.
    Have a lovely day :)
  • Dannark
    Options
    Oh that is why, ty
  • kk99
    Options
    4 years later and luckily it still works flawless

    public void OnDisconnectedFromPhoton()
    {
    Debug.Log("OnPhotonPlayerDisconnected");
    }

    thanks
  • BFX
    Options
    If anyone is reading this in 2019, I have used this great answer to create a similar solution after the latest PUN2 upgrade v2.16
    You can check the end of this post.
  • Migmac
    Options
    dreamora wrote: »
    There are two ways, depending on if you want to visualize it and how:

    1. Use OnDisconnectedFromPhoton instead of OnPhotonPlayerDisconnected (see the Enums.cs file in the photon unity networking/plugins folder)

    2. What you need to do is wait for connected to no longer be true and once thats the case you load the new level.
    Below is a quick hacked together code, not sure if connected really is present like that as my PUN version ain't the stock one anymore but it should be simple enough to understand :)

    public void SwitchLevel (string level)
    {
      StartCoroutine (DoSwitchLevel(level));
    }
    
    IEnumerator DoSwitchLevel (string level)
    {
      PhotonNetwork.Disconnect ();
      while (PhotonNetwork.connected)
        yield return null;
      Application.LoadLevel(level);
    }
    

    I've been struggling with this issue for the past 5 hours without any success, thank you so much, you are a Legend!
  • Blenderro247
    Options
    seven years later from this post and this helped with a problem i had today, disconnecting and reconnecting when the application goes into background, that coroutine rocks!
  • Am I missing something? When I do this I get stuck in the loop forever...

            PhotonNetwork.Disconnect();
            while (PhotonNetwork.IsConnected)
            {
                Debug.Log("In loop");
                yield return null;
            }
            SceneManager.LoadScene(0);
    

    I can't break out of the loop.
  • Am I missing something? When I do this I get stuck in the loop forever...

            PhotonNetwork.Disconnect();
            while (PhotonNetwork.IsConnected)
            {
                Debug.Log("In loop");
                yield return null;
            }
            SceneManager.LoadScene(0);
    

    I can't break out of the loop.

    Same here