Users joining separates room when call JoinRandomOrCreateRoom() method.

Options

Hi everyone,

I have extremely strange problem. I'm creating multiplayer game on Unity and all was alright.

But, suddenly, when I tried to connect in room. And..... Nothing happened.

User 1 connect to room A. Then, waiting for 5 seconds and connect User 2. But he doesn't connect to Room A, instead of it, he creates new room.

To put in a nutshell, users, using JoinRandomOrCreateRoom(), create join separates rooms, even if some room is available.

Here is my code.

I will be really thankful if you help me.

public void FindQuickMatch()
{
    PhotonNetwork.ConnectUsingSettings();
    Debug.Log($"<color=green> Try to connect.. </color>");
}

public override void OnConnectedToMaster()
{
    PhotonNetwork.JoinLobby();
    Debug.Log($"<color=green> Connected to master. </color>");
}

public override void OnJoinedLobby()
{
    Debug.Log($"<color=green> Connected to lobby {PhotonNetwork.CurrentLobby.Name} </color>");
    var options = new RoomOptions();
    options.MaxPlayers = 2; // Max player in room 2
    options.EmptyRoomTtl = 5000; // 5 seconds TTL empty room
    PhotonNetwork.JoinRandomOrCreateRoom();
}

public override void OnPlayerEnteredRoom(Photon.Realtime.Player newPlayer)
{
    Debug.Log($"<color=green> Connected to room {PhotonNetwork.CurrentRoom.Name}. In the room {PhotonNetwork.CurrentRoom.PlayerCount} players. </color>");
    if (PhotonNetwork.CurrentRoom.PlayerCount == 2)
    {
        Debug.Log($"<color=green> User connected. Starting loading level...</color>");
        LoadRoom();
        return;
    }
    Debug.Log($"<color=green> Waiting for other person...</color>");
}

public override void OnJoinedRoom()
{
    Debug.Log($"<color=green> Connected to room {PhotonNetwork.CurrentRoom.Name}. In the room {PhotonNetwork.CurrentRoom.PlayerCount} players. </color>");
    if (PhotonNetwork.CurrentRoom.PlayerCount == 2)
    {
        Debug.Log($"<color=green> Starting loading level...</color>");
        LoadRoom();
        return;
    }
    Debug.Log($"<color=green> Waiting for other person...</color>");
}


private void LoadRoom()
{
    if (!PhotonNetwork.IsMasterClient)
    {
        Debug.LogError("PhotonNetwork : Trying to Load a level but we are not the master Client");
        return;
    }
    Debug.LogFormat("PhotonNetwork : Loading Level : {0}", PhotonNetwork.CurrentRoom.PlayerCount); // CHANGE SCENE NAME
    PhotonNetwork.LoadLevel(_gameplaySceneName); 
}


Answers

  • Tobias
    Options

    This happens from time to time, if all players create the room at the same time.

    We distribute rooms across machines and those machines need to confirm the room being opened, before anyone can join.

    It's uncommon that users create rooms at the same time and it's a non-problem for busy games (as someone can join quickly).

    For tests, you will have to coordinate that one person creates the room and the other waits a second or so to get in.