Leaving a room and joining a new random room?

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.

Leaving a room and joining a new random room?

GalacticCyclist
2021-11-11 22:37:49

Hi,

I am trying to leave a room and join a new random room. On my OnLeftRoom() handler, I try calling PhotonNetwork.JoinRandomRoom(newRoomHash) but I get the following error:

JoinRandomRoom failed. Client is on MasterServer (must be Master Server for matchmaking) but not ready for operations (State: Authenticating). Wait for callback: OnJoinedLobby or OnConnectedToMaster.    
UnityEngine.Debug:LogError (object)    

So by the look of this error, I need to do some handling of the MasterClient, however I am not sure what. The documentation says the following about the LeaveRoom() function which I am calling:

LeaveRoom (bool becomeInactive=true) Leave the current room and return to the Master Server where you can join or create rooms (see remarks)

So based on the above docs, I am attempting to do just that -> Leave the current room and join or create another random room. In my current test, I am the MasterClient as I am the only player. Should I be disconnecting completely? That would not seem to make sense. Please advise the best approach and what I need to handle in regards the master client.

Comments

Murky
2021-11-12 03:01:15

Hi!, Usually OnLeftRoom is used to clean the user's list when the local user leave the room. For example:

public override void OnLeftRoom()    
  {    
    Debug.Log("Exit a Room");    
    //Destroying the user list    
    //PlayersDictionary is a list of players    
    foreach(GameObject ObjectPlayer in PlayersDictionary.Values)    
    {    
      Destroy(ObjectPlayer);    
    }    
    PlayersDictionary.Clear();    
    PlayersDictionary = null;    

  }    

After you leave a room the method OnConnectedToMaster is called. So I suggest you to create another method to Join a random room after you connected to master.

public void JoinRandomRoom()    
  {    
    PhotonNetwork.JoinRandomOrCreateRoom();    
  }    

So don't use PhotonNetwork.JoinRandomOrCreateRoom() in OnLeftRoom method

Back to top