Can't start OFFLINE mode while connected!

Options
So I'm currently trying to make player able to create a single-player room. My current code to set offline mode to true looks like this:
public void SwitchConnection(bool connect)
    {
        if (connect)
        {
            PhotonNetwork.OfflineMode = false;
        }
        else
        {
            PhotonNetwork.OfflineMode = true;
        }
However, when I set it to true, it gives me "Can't start OFFLINE mode while connected!" which makes no sense to me because I checked and this error gets thrown when IsConnected is true and it is true even in offline mode. So how can I possibly resolve this?

Thanks in advance!

Comments

  • Iggy
    Options
    Looking at my own code here, it seems that if you're connected you need to disconnect before starting an offline game.
    async UniTask ConnectOfflineAsync()
    {
    	// When in online mode and connected, first disconnect.
    	if (!PhotonNetwork.OfflineMode && PhotonNetwork.IsConnected)
    		await DisconnectAsync();
    
    	// Then change mode to offline.
    	PhotonNetwork.OfflineMode = true;
    
    	// And try connecting.
    	await ConnectAsync();
    }
    
  • D3RRIXX
    D3RRIXX
    edited February 2021
    Options
    Thanks, but I already figured this out on my own and did it the same way using Coroutines. So, for anyone reading this in the future, what do you need to know when implementing offline mode:
    1. Don't connect to master after going offline
    2. Create room without joining lobby as it will throw up an error otherwise
    3. Disconnecting doesn't happen immediately, so you might want to use something like this:
    IEnumerator Disconnect()
        {
            PhotonNetwork.Disconnect();
            while (PhotonNetwork.IsConnected)
            {
                yield return null;
            }
            PhotonNetwork.OfflineMode = true;
        }
    
  • Tobias
    Options
    Disconnect will attempt to reach the server and tell it the client disconnects. This makes sure the remaining players know asap.

    There is a callback which tells you when the client is disconnected, so you don't have to make this a coroutine (but it's OK).