1

Users connect to lobby:

private void Start()
{
    PhotonNetwork.ConnectUsingSettings();
}

public override void OnConnectedToMaster()
{
    PhotonNetwork.JoinLobby();
    SceneManager.LoadScene("Lobby");
}

Then users create room or enter in a room by its name

private Dictionary<string, RoomInfo> _roomList = new Dictionary<string, RoomInfo>();

private bool CheckNullOrEmpty(string name)
{
    return !string.IsNullOrEmpty(name);
}

private bool CheckServerAvaliable(string qwe)
{
    bool isAvaliable = true;

    foreach (var item in _roomList)
    {
        RoomInfo info = item.Value;

        if (info.Name == qwe)
        {
            isAvaliable = false;
        }
    }

    return isAvaliable;
}

private void SetNickname()
{
    PhotonNetwork.NickName = _nickname.text;
    PlayerPrefs.SetString("name", _nickname.text);
}

private void CreateRoom()
{
    RoomOptions roomOptions = new() { MaxPlayers = _maximumPlayers };
    PhotonNetwork.CreateRoom(_createServer.text, roomOptions);
}

public void TryCreateRoom()
{
    if (CheckNullOrEmpty(_createServer.text) && CheckServerAvaliable(_createServer.text) && CheckNullOrEmpty(_nickname.text))
    {
        SetNickname();

        CreateRoom();
    }

    else if (!CheckNullOrEmpty(_nickname.text))
    {
        _errorText.SetText("Nickname cannot contain only spaces or be empty");
    }


    else if (!CheckServerAvaliable(_createServer.text))
    {
        _errorText.SetText("The name of the room is occupied");
    }
}

public void TryJoinRoom()
{
    if (!CheckServerAvaliable(_joinServer.text))
    {
        SetNickname();

        PhotonNetwork.JoinRoom(_joinServer.text);
    }

    else if (!CheckNullOrEmpty(_nickname.text))
    {
        _errorText.SetText("Nickname cannot contain only spaces or be empty");
    }

    else if (CheckServerAvaliable(_joinServer.text))
    {
        _errorText.SetText("There is no room with such name");
    }
}
private void UpdateCachedRoomList(List<RoomInfo> roomList)
{
    for (int i = 0; i < roomList.Count; i++)
    {
        RoomInfo info = roomList[i];
        if (info.RemovedFromList)
        {
            _roomList.Remove(info.Name);
        }
        else
        {
            _roomList[info.Name] = info;
        }
    }
}

public override void OnRoomListUpdate(List<RoomInfo> roomList)
{
    UpdateCachedRoomList(roomList);
}

Everything works well, then users leave room and enter lobby:

  public void Leave()
{
    PhotonNetwork.LeaveRoom();
    SceneManager.LoadScene("Lobby");
}

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

Rollis
  • 93
  • 8
  • Afaik when you leave a room in Photon you also automatically leave the Lobby - try to call `PhotonNetwork.JoinLobby();` again before entering a room – derHugo Jul 10 '23 at 11:50
  • @derHugo ok i will try,but why user can create a room after leaving the Lobby? – Rollis Jul 10 '23 at 11:52
  • Well are you sure this even succeeded? – derHugo Jul 10 '23 at 11:53
  • Yes room was created, tryed join to Lobby, still have problem with entering to room by name – Rollis Jul 10 '23 at 11:55
  • So do you even need the first `JoinLobby`? In the API of `OnLeftLobby` it states `When you leave a lobby, OpCreateRoom and OpJoinRandomRoom automatically refer to the default lobby.` It sounds like you can not use `JoinRoom` to enter a specific room in the default lobby (without using `JoinLobby()`), you can only use `JoinRandomRoom` if not in a specific lobby – derHugo Jul 10 '23 at 11:59
  • Would probably do e.g. `public void Leave() { PhotonNetwork.LeaveRoom(); } public override void OnLeftRoom() { PhotonNetwork.JoinLobby(); } public override void OnJoinedLobby(){ SceneManager.LoadScene("Lobby"); }` – derHugo Jul 10 '23 at 12:04
  • in general for the name check: [`string.IsNullOrWhitespace`](https://learn.microsoft.com/dotnet/api/system.string.isnullorwhitespace) .. should probably also check this also before creating a room – derHugo Jul 10 '23 at 12:19

2 Answers2

1

Solved the problem by simple adding

public override void OnConnectedToMaster()
{
    if (!PhotonNetwork.InLobby)
        PhotonNetwork.JoinLobby();
}
Rollis
  • 93
  • 8
0
  • The issue lies in the order of your conditionals. Currently, you are checking if the server is available (!CheckServerAvaliable(_joinServer.text)) first, and then checking
  • if the nickname is empty or contains only spaces (!CheckNullOrEmpty(_nickname.text)), and finally checking if the server is available again (CheckServerAvaliable(_joinServer.text)).

Since you are getting the "There is no room with such name" error, it indicates that the second conditional (else if (!CheckNullOrEmpty(_nickname.text))) is evaluated to true, and the error message is displayed.

 public void TryJoinRoom()
 {
    if (!CheckNullOrEmpty(_nickname.text))
   {
       _errorText.SetText("Nickname cannot contain only spaces or be empty");
    }
    else if (!CheckServerAvaliable(_joinServer.text))
   {
       SetNickname();
       PhotonNetwork.JoinRoom(_joinServer.text);
   }
   else if (CheckServerAvaliable(_joinServer.text))
   {
        _errorText.SetText("There is no room with such name");
    }
  }

i think this code will solve your problem

sameraze agvvl
  • 397
  • 4
  • 11