.CurrentRoom do not return true after creating room/joining room

Options

I did the same as this video has done


public class MenuController : MonoBehaviourPunCallbacks
{
  private void Start()
  {
    PhotonNetwork.ConnectUsingSettings();
    Debug.Log("ConnectingToMaster");
  }

  public override void OnConnectedToMaster()
  {
    Debug.Log("ConnectedToMasterNowConnectingToLobby");
    PhotonNetwork.JoinLobby();
  }

  public override void OnJoinedLobby()
  {
    base.OnJoinedLobby();
    Debug.Log("ConnectedToLobby");
    SceneManager.LoadScene("Lobby");
  }
}


The second script is this in scene"Lobby"


public class CreateAndJoinRooms : MonoBehaviourPunCallbacks
{
  public InputField createInput;
  public InputField joinInput;

  public void CreateRoom()
  {
    Debug.Log("created room");
    PhotonNetwork.CreateRoom(createInput.text);
    if (PhotonNetwork.CurrentRoom == null)
    {
      Debug.Log("you are not in any room");
    }
  }

  public void JoinRoom()
  {
    PhotonNetwork.JoinRoom(joinInput.text);
  }

  public override void OnJoinedRoom()
  {
    base.OnJoinedRoom();
    PhotonNetwork.LoadLevel("MainTown");
  }
}

Clicking create game:

Clicking create game 1 more time:


Answers

  • JohnTube
    JohnTube ✭✭✭✭✭
    edited December 2021
    Options

    Hi @AKihiro,

    Thank you for choosing Photon!

    PhotonNetwork.CurrentRoom is of type Room and not bool. It gives you the currently joined room if the client is joined to one or null if not.

    PhotonNetwork.InRoom is of type bool and tells you if you are joined to a room or not.

    Checking both just after calling PhotonNetwork.CreateRoom (even if it returns true) is wrong.

    Photon operations are asynchronous, you need to wait for the server's response which will trigger client code callbacks.

    In this case OnJoinedRoom is the callback for both room creation (CreateRoom) or room join (JoinRoom). In Photon creating a room will automatically join it. There is a callback OnCreatedRoom if needed also but you are not still fully joined to it (locally) yet the time of triggering that callback, that's why OnJoinedRoom is the best candidate for executing logic after entering the room.

    So you need to use OnJoinedRoom.

    Besides, once you are in a room, you cannot switch to another room (create or join). That's why you got the error when you click the button one more time. You need to leave the current room before by calling PhotonNetwork.LeaveRoom.

    Once on master server again (OnConnectedToMaster) you can enter another room.

    Please go through PUN Basics Tutorial or a full YouTube series about PUN 2 before jumping into action.

    We now also recommend starting with Photon Fusion for new projects as it replaces both PUN 2 and Photon Bolt.