[UPDATED] I Need help changing rooms

Options

Players can connect and see and hear each other. Now I'm trying to add the capability to change "rooms". Note that this is not an actual game. I don't need match making or lobbies.

The current UX is like this:

  1. Players sign in and are put in a default lobby and room. They can see and hear each other. This is working.
  2. Players open a menu and can select from 3 predefined rooms to join. When they click "Join" on a room, they should stop seeing and hearing the people in the previous room.

Previously I was getting an error about "JoinOrCreateRoom failed. Client is on GameServer", so I rewrote everything to be very linear, trying to rely on events to do the timing. I even got frustrated and decided it would be easier to just completely disconnect and then reconnect. However, with the code below in the current state, it never reconnects.

Here is the relevant code:

  void Start()
  {
    PhotonNetwork.NickName = "Logging In...";
    PhotonNetwork.ConnectUsingSettings(PhotonNetwork.PhotonServerSettings.AppSettings);
    isConnecting = true;
  }


  public override void OnConnectedToMaster()
  {
    base.OnConnectedToMaster();
    if (isConnecting)
    {
      TypedLobby myLobby = new TypedLobby(LobbyName, LobbyType.Default);
      PhotonNetwork.JoinLobby(myLobby);
      isConnecting = false;
    }
  }

  public override void OnJoinedLobby()
  {
    base.OnJoinedLobby();
    Debug.Log("[PHOTON] Joined Lobby: " + PhotonNetwork.CurrentLobby.Name);
    RoomOptions roomOptions = new RoomOptions();
    roomOptions.MaxPlayers = 20;
    PhotonNetwork.JoinOrCreateRoom(RoomName, roomOptions, TypedLobby.Default);
  }
  public override void OnLeftLobby()
  {
    base.OnLeftLobby();
    Debug.Log("[PHOTON] Left Lobby");
  }
  public override void OnLeftRoom()
  {
    base.OnLeftRoom();
    Debug.Log("[PHOTON] Left Room");
    PhotonNetwork.Disconnect();
  }

  public override void OnDisconnected(DisconnectCause cause)
  {
    base.OnDisconnected(cause);
    Debug.Log("[PHOTON] Disconnected: " + cause);
    PhotonNetwork.ConnectUsingSettings(PhotonNetwork.PhotonServerSettings.AppSettings);
  }
  public void switchRoom(string channel)
  {
    RoomName = channel;
    Debug.Log("NetworkManager.switchRoom(" + channel + ")");
    PhotonNetwork.LeaveRoom();
  }

  public override void OnJoinedRoom()
  {
    base.OnJoinedRoom();
    Debug.Log("[PHOTON] Joined Room: " + PhotonNetwork.CurrentRoom.Name);
    if (!hasSpawned) {
      SpawnMyPlayer();
      hasSpawned = true;
    }
  }


Answers

  • FusionScott
    Options

    A little more information about the scenario and expected results.

    1. A few users log in to the application and automatically join a default room and can see and hear each other. (This is working)
    2. Users can open a GUI and click "Join" on one of 3 other hard-coded "rooms". This is what the switchRoom method does. This is what is causing the error.
  • Tobias
    Options

    Photon's matchmaking is done by the Master Server and the actual gameplay is done on a range of Game Servers. While connected to the Game Servers, you can't ask the server to join a different room. This is what "JoinOrCreateRoom failed. Client is on GameServer" tries to hint at.

    You should be able to use LeaveRoom(), wait for it's callback (you get another callback for arriving on the Master Server) and then join the designated room.

    This should have the necessary details: https://doc.photonengine.com/en-us/pun/current/lobby-and-matchmaking/matchmaking-and-lobby

    FYI: I deleted the other comment (as per request).