Photon not rejoining Master Server after leaving room
The whole answer can be found below.
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).
Photon not rejoining Master Server after leaving room
Flipster77
2018-04-23 05:28:32
Hi, I'm having an issue where Photon Network doesn't appear to rejoin the Master Server after leaving a room.
In our game, we switch between online and offline modes. This involves changing to a transition (loading) scene, which asynchronously loads the main scene, connects to photon if not connected and joins a room.
The scenario where it fails is when it is in the main scene in offline mode, we click a button to trigger reloading the scene in online mode which:
- Sets our own offlineMode bool to false.
- Calls PhotonNetwork.LeaveRoom().
- Loads the transition scene.
- In the Start method of a script in the transition scene, our ConnectToPhoton method is called.
The ConnectToPhoton method is as follows:
public void ConnectToPhoton() {
if (offlineMode) {
if (PhotonNetwork.connected) {
PhotonNetwork.Disconnect();
}
else {
OnDisconnectedFromPhoton();
}
return;
}
else {
PhotonNetwork.offlineMode = false;
}
// If we are not connected to Photon, connect
if (!PhotonNetwork.connected) {
PhotonNetwork.ConnectUsingSettings(GAME_VERSION);
}
// Join the lobby if we haven't
else if (!PhotonNetwork.insideLobby) {
PhotonNetwork.JoinLobby();
}
// Join a random room
else {
PhotonNetwork.JoinRandomRoom();
}
Joining the offline room shows the following logs:
- OnStatusChanged: Disconnect current State: Disconnecting
- OnConnectedToMaster() was called by PUN. This client is now connected.
- OnStatusChanged: Connect current State: ConnectingToGameserver
- Joined a room: offline room // This is our own log which logs the room name when a room is joined
When the room is left in order to switch to an online room, only the method for the OnLeftRoom callback provides logs:
public override void OnLeftRoom() {
base.OnLeftRoom();
Debug.Log("OnLeftRoom() was called by PUN.");
}
I am expecting either of the following methods to receive callbacks after the room is left:
public override void OnConnectedToMaster() {
base.OnConnectedToMaster();
Debug.Log("OnConnectedToMaster() was called by PUN. This client is now connected.");
PhotonNetwork.JoinRandomRoom();
}
public override void OnJoinedLobby() {
Debug.Log("OnJoinedLobby was called by PUN. This client is now connected.");
PhotonNetwork.JoinRandomRoom();
}
The client state of Photon at this point is 'ConnectedToGameserver', which according to the documentation means "Still in process to join/create room.(will-change)". Leaving it to process for a few minutes still does not trigger connecting to the master or joining the lobby.
I am not sure why Photon is not connecting to the Master Server after leaving the room, and appears to be still on the Game server. Any suggestions as to why it isn't, or what I might be doing wrong are appreciated.
In case it's useful, the Start method for our persistent (DoNotDestroyOnLoad) Network Manager script is:
void Start() {
PhotonNetwork.autoJoinLobby = true;
PhotonNetwork.automaticallySyncScene = false;
PhotonNetwork.logLevel = logLevel; // Currently set to PhotonLogLevel.Full, and set to ALL in the inspector
offlineMode = loadSceneOffline;
}
I can provide more code blocks and logs if needed.
Comments
Before connecting to master, photon connects lobby, so if you wanna connect to master, try like this.
public void OnJoinedLobby()
{
OnConnectedToMaster();
}
then call OnJoinedRoom();
Flipster77
2018-07-23 04:26:24
Thanks for the help jokerTM, I actually figured out the problem but forgot to post the solution.
I needed to add PhotonNetwork.ConnectUsingSettings(GAME_VERSION) to the clause where I was setting offlineMode to false, as Photon does not reconnect automatically when setting offlineMode to false, but PhotonNetwork.connected still returns true.
else
{
// NOTE: Need to connect again when switching from offline to online
PhotonNetwork.offlineMode = false;
PhotonNetwork.ConnectUsingSettings(gameVersion);
}
This wasn't the case in a previous version of Photon, we had switched over to using Unity 2017 recently, which is when the issue started.
Thanks for the assistance though!
Back to top