Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Third Party Using Photon after leaving room user can't connect to room by name

Discussion in 'Multiplayer' started by Kujji, Jul 10, 2023.

  1. Kujji

    Kujji

    Joined:
    Oct 20, 2021
    Posts:
    109
    I connect to lobby:
    Code (CSharp):
    1. private void Start()
    2.     {
    3.         PhotonNetwork.ConnectUsingSettings();
    4.     }
    5.  
    6.     public override void OnConnectedToMaster()
    7.     {
    8.         PhotonNetwork.JoinLobby();
    9.         SceneManager.LoadScene("Lobby");
    10.     }
    Then users create room or enter in a room by its name

    Code (CSharp):
    1.     public void TryCreateRoom()
    2.     {
    3.         if (CheckCorrectName(_createServer.text) && !CheckServerExist(_createServer.text) && CheckCorrectName(_nickname.text))
    4.         {
    5.             SetNickname();
    6.  
    7.             CreateRoom();
    8.         }
    9.  
    10.         else if (!CheckCorrectName(_nickname.text))
    11.         {
    12.             _errorText.SetText("Nickname cannot contain only spaces or be empty");
    13.         }
    14.  
    15.         else if (CheckServerExist(_createServer.text))
    16.         {
    17.             _errorText.SetText("The name of the room is occupied");
    18.         }
    19.     }
    20.  
    21.     public void TryJoinRoom()
    22.     {
    23.         OnRoomListUpdate(_roomList);
    24.  
    25.         if (CheckServerExist(_joinServer.text))
    26.         {
    27.             SetNickname();
    28.  
    29.             PhotonNetwork.JoinRoom(_joinServer.text);
    30.         }
    31.  
    32.         else if (!CheckCorrectName(_nickname.text))
    33.         {
    34.             _errorText.SetText("Nickname cannot contain only spaces or be empty");
    35.         }
    36.  
    37.         else if (!CheckServerExist(_joinServer.text))
    38.         {
    39.             _errorText.SetText("There is no room with such name");
    40.         }
    41.     }
    42.     private bool CheckCorrectName(string name)
    43.     {
    44.         int spaceAmount = 0;
    45.  
    46.         for (int i = 0; i < name.Length; i++)
    47.         {
    48.             if (name[i] == ' ')
    49.             {
    50.                 spaceAmount++;
    51.             }
    52.         }
    53.  
    54.         if (spaceAmount == name.Length || name.Length == 0)
    55.         {
    56.             _errorText.SetText("Nickname cannot contain only spaces or be empty");
    57.         }
    58.  
    59.         return !(spaceAmount == name.Length || name.Length == 0);
    60.     }
    61.  
    62.     private bool CheckServerExist(string currentRoom)
    63.     {
    64.         bool isAvaliable = false;
    65.  
    66.         foreach (RoomInfo room in _roomList)
    67.         {
    68.             if (room.Name == currentRoom)
    69.             {
    70.                 isAvaliable = true;
    71.             }
    72.         }
    73.  
    74.         return isAvaliable;
    75.     }
    76.  
    77.      public override void OnRoomListUpdate(List<RoomInfo> roomList)
    78.     {
    79.         _roomList = roomList;
    80.     }
    81.  
    82.     public override void OnJoinedRoom()
    83.     {
    84.         PhotonNetwork.LoadLevel("Game");
    85.     }
    Everything works well, then users leave room and enter lobby:
    Code (CSharp):
    1.     public void Leave()
    2.     {
    3.         PhotonNetwork.LeaveRoom();
    4.         SceneManager.LoadScene("Lobby");
    5.     }
    In lobby creating room works fine, but entering to room doesn't work, i get - There is no room with such name.
    I commented verification of server existing, and then user connected, i don't understand what is the problem in verification
     
    Last edited: Jul 10, 2023
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,018
    If you test with one client and it leaves the room, then the room becomes empty and gets cleaned up. It won't be listed anymore due to the Leave() and you can't join it.
     
  3. Kujji

    Kujji

    Joined:
    Oct 20, 2021
    Posts:
    109
    All users leave room and then one user creates room and second trying to join, but he gets "There is no room with such name", room is definitly existing because another user is there
     
  4. Kujji

    Kujji

    Joined:
    Oct 20, 2021
    Posts:
    109
    Solved the problem by simple adding
    Code (CSharp):
    1. public override void OnConnectedToMaster()
    2. {
    3.     if (!PhotonNetwork.InLobby)
    4.         PhotonNetwork.JoinLobby();
    5. }
     
    tobiass likes this.