So I’m trying to use lobby and relay to allow two friends to play against each other. However, I can’t seem to get the client to ever actually be able to join the host’s lobby because of invalid character errors. Certain characters are fine and don’t throw an error. For example, if I pass in MMMMMM as the lobby code, I don’t get an error (I also don’t connect to anything though because that’s not a real code). I’ve noticed that the letters L and U seem to be giving me this error. Any ideas on what’s happening here? I’ve seen plenty of similar posts on the forums, but none that have been exactly what I’ve experienced.
Below is how I’m passing the code in for joining the lobby.
LobbyCodeString = LobbyCodeGameObject.GetComponent<TMPro.TMP_InputField>().text;
Debug.Log(LobbyCodeString);
await JoinPrivateLobby(LobbyCodeString);
private async Task<Lobby> JoinPrivateLobby(string LobbyCodeString)
{
UnityServices.InitializeAsync();
await AuthenticationService.Instance.SignInAnonymouslyAsync();
try
{
Lobby privateLobby = await Lobbies.Instance.JoinLobbyByCodeAsync(LobbyCodeString);
return privateLobby;
}
catch (LobbyServiceException e)
{
Debug.Log(e);
return null;
}
}
Thanks in advance for the help! Let me know what additional information I can provide to better understand what’s going on.
You can join a Lobby via 2 methods:
LobbyService.Instance.JoinLobbyByIdAsync(“lobbyId”);
OR
LobbyService.Instance.JoinLobbyByCodeAsync(lobbyCode);
The call you are using, JoinLobbyByCodeAsync, uses the auto-generated join code that is available upon a lobby creation. It will not take an arbitrary string. Look at the lobbyCode
property on the model Class Lobby - Lobby v1.2.1
You could programatically share that join id with other players via something like the Unity Friends system, another out of band process, or just manually do it by exposing that code to your host player in the UI and they can copy/paste or verbally give it to the player. After which your join method should start working. Or, you can try to use the ByID join option. I’d personally recommend the convenience of the auto-generated LobbyCode.
Thanks for the response. I think I am doing what you mentioned above. I have two separate instances running, one as the host and the other as a connecting client. When the host creates the lobby, I expose the lobby code as follows:
string joinCode = await Unity.Services.Relay.RelayService.Instance.GetJoinCodeAsync(allocation.AllocationId);
When I just tested, the code that it provided was QRWN8H. Then, on the client I’m trying to connect to the lobby, I paste that code in to join the lobby in this line: Lobby privateLobby = await Lobbies.Instance.JoinLobbyByCodeAsync(LobbyCodeString);
and I get the exception
[Lobby]: , (16010). Message: lobby code ‘QRWN8H’ contains an invalid character ‘Q’ (U+0051) at index 0
UnityEngine.Logger:LogError (string,object)
This is the relevant code for both sides:
public async void CreatePrivateLobbyFunction()
{
await CreatePrivateLobby();
}
private async Task<Lobby> CreatePrivateLobby()
{
UnityServices.InitializeAsync();
await AuthenticationService.Instance.SignInAnonymouslyAsync();
int maxPlayersPrivateLobby = 2;
Allocation allocation = await Unity.Services.Relay.RelayService.Instance.CreateAllocationAsync(maxPlayersPrivateLobby);
string privateLobbyName = "newlobby";
CreateLobbyOptions options = new CreateLobbyOptions();
options.IsPrivate = true;
Lobby privateLobby = await LobbyService.Instance.CreateLobbyAsync(privateLobbyName, maxPlayersPrivateLobby, options);
string joinCode = await Unity.Services.Relay.RelayService.Instance.GetJoinCodeAsync(allocation.AllocationId);
Debug.Log(joinCode);
return privateLobby;
}
public async void JoinPrivateLobbyFunction()
{
LobbyCodeString = LobbyCodeGameObject.GetComponent<TMPro.TMP_InputField>().text;
Debug.Log(LobbyCodeString);
await JoinPrivateLobby(LobbyCodeString);
}
private async Task<Lobby> JoinPrivateLobby(string LobbyCodeString)
{
UnityServices.InitializeAsync();
await AuthenticationService.Instance.SignInAnonymouslyAsync();
try
{
Lobby privateLobby = await LobbyService.Instance.JoinLobbyByCodeAsync(LobbyCodeString);
//Lobby privateLobby = await Lobbies.Instance.JoinLobbyByCodeAsync(LobbyCodeString);
return privateLobby;
}
catch (LobbyServiceException e)
{
Debug.Log(e);
return null;
}
}
Did I capture the intent of what you suggested, or did I do something stupid?
Thanks!
Yup, that’s the stupid thing I did :), confused the two services.
I changed
string joinCode = await Unity.Services.Relay.RelayService.Instance.GetJoinCodeAsync(allocation.AllocationId);
to
string joinCode = privateLobby.LobbyCode;
and now I can get both players in the same lobby. Thank you both for the help!
1 Like