Switching offline and online mode back and forth

Hello,

When player chooses single player offline mode, I set GlobalSettings.IsNetwork to false, and true when player plays online.
Here is the relevant code:

    void OnEnable()
    {
        
        if (GlobalSettings.isNetworked)
        {
            PhotonNetwork.offlineMode = false;
            PhotonNetwork.ConnectUsingSettings("v0.7");
            PhotonNetwork.JoinLobby();
        }
        else
        {
            PhotonNetwork.Disconnect();
            PhotonNetwork.offlineMode = true;
            PhotonNetwork.CreateRoom(testRoom);
        }
        

    }


    void OnJoinedLobby()
    {
 
        RoomOptions roomOptions = new RoomOptions();
        roomOptions.IsVisible = true;
        roomOptions.MaxPlayers = 8;
        PhotonNetwork.JoinOrCreateRoom(testRoom, roomOptions, TypedLobby.Default);

    }

    void OnPhotonJoinRoomFailed()
    {
        Debug.Log("Joining failed");
        PhotonNetwork.CreateRoom(testRoom);
    }

    void OnJoinedRoom()
    {

        // Run Game
    }

It works up to some point..

When I load multiplayer scene for a second time I got stuck on Connecte To Nameserver

For example,
I start with offline mode (works), change to online mode (works), then back to offline (works), back to online mode (stuck, doesn't work).

There are some exceptions, but I don't know why they happen. The above scenerio is most common however.

Any ideas? :)

Comments

  • Hi @mattbnn,

    if the thrown exceptions are related to Photon, please mention them here because they might already give a hint what doesn't work. As far as I can tell, switching between Off- and Online mode works fine.
  • Oh, I missed the piece of code responsible for going back to menu and leaving room:

    public void OnClickToMenu()
    {
    PhotonNetwork.LeaveRoom();

    }
    public void OnLeftRoom()
    {
    PhotonNetwork.LeaveLobby();
    SceneManager.LoadScene(0);
    }

    After I leave scene with offline mode I got the error pasted below.
    However after first leaving and going to online mode everything works well (even though the error is present)

    Cannot send op: 228 Not connected. PeerState: Disconnected
    UnityEngine.Debug:LogError(Object)
    NetworkingPeer:DebugReturn(DebugLevel, String) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:1518)
    ExitGames.Client.Photon.EnetPeer:EnqueueOperation(Dictionary`2, Byte, Boolean, Byte, Boolean, EgMessageType)
    ExitGames.Client.Photon.PeerBase:EnqueueOperation(Dictionary`2, Byte, Boolean, Byte, Boolean)
    ExitGames.Client.Photon.PhotonPeer:OpCustom(Byte, Dictionary`2, Boolean, Byte)
    ExitGames.Client.Photon.PhotonPeer:OpCustom(Byte, Dictionary`2, Boolean)
    LoadBalancingPeer:OpLeaveLobby() (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/LoadbalancingPeer.cs:106)
    PhotonNetwork:LeaveLobby() (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonNetwork.cs:2133)
    MarsForce.MenuManager:OnLeftRoom() (at Assets/Raba Games/Scripts/MenuManager.cs:34)
    UnityEngine.GameObject:SendMessage(String, Object, SendMessageOptions)
    NetworkingPeer:SendMonoMessage(PhotonNetworkingMessage, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:2702)
    PhotonNetwork:LeaveRoom() (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonNetwork.cs:2151)
    MarsForce.MenuManager:OnClickToMenu() (at Assets/Raba Games/Scripts/MenuManager.cs:29)
    UnityEngine.EventSystems.EventSystem:Update()
  • Hi @Christian_Simon,

    I forgot to paste a fragment of code that is executed when I go back to menu:

    public void OnClickToMenu()
    {
    PhotonNetwork.LeaveRoom();
    }
    public void OnLeftRoom()
    {
    PhotonNetwork.LeaveLobby();
    SceneManager.LoadScene(0);
    }


    As for the errors, each time I go back from offline mode I get the error pasted below.
    However, after first starting offline mode, then switching to online mode everything works well (even though this error is present).

    Cannot send op: 228 Not connected. PeerState: Disconnected
    UnityEngine.Debug:LogError(Object)
    NetworkingPeer:DebugReturn(DebugLevel, String) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:1518)
    ExitGames.Client.Photon.EnetPeer:EnqueueOperation(Dictionary`2, Byte, Boolean, Byte, Boolean, EgMessageType)
    ExitGames.Client.Photon.PeerBase:EnqueueOperation(Dictionary`2, Byte, Boolean, Byte, Boolean)
    ExitGames.Client.Photon.PhotonPeer:OpCustom(Byte, Dictionary`2, Boolean, Byte)
    ExitGames.Client.Photon.PhotonPeer:OpCustom(Byte, Dictionary`2, Boolean)
    LoadBalancingPeer:OpLeaveLobby() (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/LoadbalancingPeer.cs:106)
    PhotonNetwork:LeaveLobby() (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonNetwork.cs:2133)
    MarsForce.MenuManager:OnLeftRoom() (at Assets/Raba Games/Scripts/MenuManager.cs:34)
    UnityEngine.GameObject:SendMessage(String, Object, SendMessageOptions)
    NetworkingPeer:SendMonoMessage(PhotonNetworkingMessage, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:2702)
    PhotonNetwork:LeaveRoom() (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonNetwork.cs:2151)
    MarsForce.MenuManager:OnClickToMenu() (at Assets/Raba Games/Scripts/MenuManager.cs:29)
    UnityEngine.EventSystems.EventSystem:Update()
  • The error messages states, that you are trying to leave the lobby (this is Operation Code 228). It furthermore states, that you are already disconnected when trying to call PhotonNetwork.LeaveLobby();. Please note, that OnLeftRoom is also called when leaving a room while being in Offline Mode. A solution might be to add a if (PhotonNetwork.offlineMode) condition.
  • Thanks for reply, the error is no longer present, although it didn't solve the problem.
    Is there a way to restore Photon to the same state as in start of application?
  • You can try to set the connection state to disconnected and see if this helps you solving the problem you currently have. You can do this by using PhotonNetwork.networkingPeer.State = ClientState.Disconnected;.
  • Thanks! Looks like it solved the problem.