英文:
Using Photon in Unity after leaving room user can't connect to room by name
问题
以下是您提供的代码的中文翻译:
用户连接到大厅:
private void Start()
{
PhotonNetwork.ConnectUsingSettings();
}
public override void OnConnectedToMaster()
{
PhotonNetwork.JoinLobby();
SceneManager.LoadScene("Lobby");
}
然后用户通过房间名创建房间或进入房间:
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("昵称不能只包含空格或为空");
}
else if (!CheckServerAvaliable(_createServer.text))
{
_errorText.SetText("房间名称已被占用");
}
}
public void TryJoinRoom()
{
if (!CheckServerAvaliable(_joinServer.text))
{
SetNickname();
PhotonNetwork.JoinRoom(_joinServer.text);
}
else if (!CheckNullOrEmpty(_nickname.text))
{
_errorText.SetText("昵称不能只包含空格或为空");
}
else if (CheckServerAvaliable(_joinServer.text))
{
_errorText.SetText("没有该名称的房间");
}
}
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);
}
一切都正常工作,然后用户离开房间并返回大厅:
public void Leave()
{
PhotonNetwork.LeaveRoom();
SceneManager.LoadScene("Lobby");
}
在大厅中创建房间运行正常,但进入房间不起作用,我收到 "没有该名称的房间" 的消息。我已注释掉服务器存在的验证部分,然后用户连接,但我不理解验证中的问题。
英文:
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
答案1
得分: 1
通过简单地添加以下代码解决了这个问题:
public override void OnConnectedToMaster()
{
if (!PhotonNetwork.InLobby)
PhotonNetwork.JoinLobby();
}
英文:
Solved the problem by simple adding
public override void OnConnectedToMaster()
{
if (!PhotonNetwork.InLobby)
PhotonNetwork.JoinLobby();
}
答案2
得分: 0
问题出在条件的顺序上。目前,您首先检查服务器是否可用(!CheckServerAvaliable(_joinServer.text)),然后检查昵称是否为空或仅包含空格(!CheckNullOrEmpty(_nickname.text)),最后再次检查服务器是否可用(CheckServerAvaliable(_joinServer.text))。
由于您收到了"没有这个名称的房间"的错误消息,这表明第二个条件(else if (!CheckNullOrEmpty(_nickname.text)))被评估为真,并显示了错误消息。
public void TryJoinRoom()
{
if (!CheckNullOrEmpty(_nickname.text))
{
_errorText.SetText("昵称不能仅包含空格或为空");
}
else if (!CheckServerAvaliable(_joinServer.text))
{
SetNickname();
PhotonNetwork.JoinRoom(_joinServer.text);
}
else if (CheckServerAvaliable(_joinServer.text))
{
_errorText.SetText("没有这个名称的房间");
}
}
我认为这段代码将解决您的问题。
英文:
- 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论