Can't start OFFLINE mode while connected!

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

Can't start OFFLINE mode while connected!

D3RRIXX
2021-02-25 13:08:00

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
2021-02-25 22:08:24

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
2021-02-26 13:15:51

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
2021-03-04 15:33:03

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).

Back to top