PUN2 Can't create or join room

Options
i upgraded from pun1 to pun2 and now when i try to create a room nothing happens. i am using unity 2017.4.29f1. The code worked with pun1 and seems correct. When I try to make a room isconnected is true and inlobby is true. After I try to create the room, inlobby becomes false, but inroom is still false. OnCreateRoomFailed is never called. i imported pun2 free and used my old 100ccu subscription from pun1.what could be the cause?

Comments

  • raphael
    raphael
    edited December 2019
    Options
    It was probably because I forgot the PhotonNetwork.AddCallbackTarget(this) during migration.. now I have other bugs that I might be able to fix. EDIT: after fixing the error I still cant create a room, I will post my simple code.
  • raphael
    raphael
    edited December 2019
    Options
    
    
    using PhotonHashTable = ExitGames.Client.Photon.Hashtable;
       
     public class PhotonConnect : MonoBehaviourPunCallbacks
        {
            private int playableAssetCount;
            public static TypedLobby Lobby;
    
            private void Awake()
            {
                Lobby = new TypedLobby("Lobby", LobbyType.SqlLobby);
                PhotonNetwork.AddCallbackTarget(this);
            }
    
            private void Start()
            {
                StartCoroutine(Connect());
            }
    
            public static IEnumerator Connect()
            {
                yield return null; // in case we changed offlineMode, we wait for it to update...
    
                if (PhotonNetwork.OfflineMode || PhotonNetwork.IsConnected && !PhotonNetwork.OfflineMode)
                    yield break;
    
                Debug.LogError("CONNECT");
                PhotonNetwork.ConnectUsingSettings();
    
                yield return new WaitUntil(() => PhotonNetwork.InLobby && PhotonNetwork.IsConnectedAndReady);
                yield return new WaitForEndOfFrame();
            }
    
    // THIS IS NEVER CALLED
            public override void OnCreateRoomFailed(short returnCode, string message)
            {
                Debug.Log("CREATE ROOM FAILED");
                StopAllCoroutines();
            }
    
    // this is called
            public override void OnConnectedToMaster()
            {
                if (!PhotonNetwork.OfflineMode)
                {
                    Debug.Log("ON CONNECTED TO MASTER");
                    PhotonNetwork.JoinLobby(Lobby);
                }
            }
    
    // this is called
            public override void OnConnected()
            {
                Debug.Log("ON CONNECTED");
            }
    
    // this is never called
            public override void OnDisconnected(DisconnectCause cause)
            {
                Debug.LogError("ON DISCONNECTED");
            }
    }
    
    public class PhotonMatchmaking : MonoBehaviourPunCallbacks
        {
            private static PhotonMatchmaking instance;
            private static int currentMaxPlayerCount;
    
            private void Awake()
            {
                instance = this;
                PhotonNetwork.AddCallbackTarget(this);
            }
    
    // This is called from the game loading routine but it never ends because InRoom is Never true
            public static IEnumerator JoinOrCreateRoom(int playableAssetCount)
            {
                // in offline mode we create a simple room
                if (PhotonNetwork.OfflineMode && !PhotonNetwork.InRoom)
                {
                    PhotonNetwork.CreateRoom("Room");
                    yield break;
                }
    
                yield return new WaitUntil(() => PhotonNetwork.IsConnectedAndReady && PhotonNetwork.InLobby);
    
                currentMaxPlayerCount = playableAssetCount;
    
                var expectedProperties = new PhotonHashTable() { { "gameSlug", Game.CurrentGameSlug } };
                Debug.Log("in lobby:" + PhotonNetwork.InLobby); // output: true
                CreateRoom();
                Debug.Log("in lobby:" + PhotonNetwork.InLobby); // output: false
                yield return WaitForRoomSetup();
            }
    
            private static void CreateRoom()
            {
                Debug.Log("CREATE ROOM " + Game.CurrentGameSlug); // output: Create Room game_1
                var roomPropsInLobby = CreateCustomPropertiesInLobby();
                PhotonHashTable customRoomProperties = CreateRoomCustomProperties();
                var roomOptions = new RoomOptions { CustomRoomProperties = customRoomProperties, CustomRoomPropertiesForLobby = roomPropsInLobby, MaxPlayers = (byte)currentMaxPlayerCount, CleanupCacheOnLeave = false };
                PhotonNetwork.CreateRoom(null, roomOptions, PhotonConnect.Lobby);
            }
    
            private static IEnumerator WaitForRoomSetup()
            {
                Debug.Log("WAIT FOR ROOM SETUP");
                yield return new WaitUntil(() => PhotonNetwork.InRoom); // in room never true
            }
    
            public static string[] CreateCustomPropertiesInLobby()
            {
                return new string[] { "C0", "gameId", "gameTitle", "gameSlug" };
            }
            public static PhotonHashTable CreateRoomCustomProperties()
            {
                return new PhotonHashTable() { { "C0", 0 }, { "gameId", Game.CurrentGameId }, { "gameTitle", Game.CurrentGameTitle }, { "gameSlug", Game.CurrentGameSlug } };
            }
        }
    }
    
  • JohnTube
    JohnTube ✭✭✭✭✭
    edited December 2019
    Options
    Hi @raphael,

    Thank you for choosing Photon!

    There are few issues w/ your code:

    but inroom is still false.
    From your code, you can never know this since there is no debug message after yield return new WaitUntil(() => PhotonNetwork.InRoom); // in room never true in WaitForRoomSetup.
    - I think your code could be simplified and you can get rid of all the coroutines. I would rely on Photon callbacks only and not on properly and WaitFor or WaitUntil etc. That is error prone and not optimized and IMO not clean. Check suggested code below (not tested).
    - If you extend MonoBehaviourPunCallbacks, you don't need to call PhotonNetwork.AddCallbackTarget(this) or PhotonNetwork.RemoveCallbackTarget(this), unless you override OnEnable or OnDisable w/o calling base.OnEnable() or base.OnDisable().
    - The string[] returned by CreateCustomPropertiesInLobby could become a static field and avoid creating a new string every time (less allocation, less garbage, less GC overhead).

    using System.Collections;
    using Photon.Pun;
    using Photon.Realtime;
    using UnityEngine;
    using PhotonHashTable = ExitGames.Client.Photon.Hashtable;
    
    public class PhotonConnect : MonoBehaviourPunCallbacks
    {
        private int playableAssetCount;
        public static TypedLobby Lobby;
    
        private void Awake()
        {
            Lobby = new TypedLobby("Lobby", LobbyType.SqlLobby);
            PhotonNetwork.AddCallbackTarget(this);
        }
    
        private void Start()
        {
            Connect();
        }
    
        public static bool Connect()
        {
            if (PhotonNetwork.OfflineMode || PhotonNetwork.IsConnected && !PhotonNetwork.OfflineMode)
                return false;
    
            Debug.LogError("CONNECT");
            return PhotonNetwork.ConnectUsingSettings();
        }
    
        // THIS IS NEVER CALLED
        public override void OnCreateRoomFailed(short returnCode, string message)
        {
            Debug.Log("CREATE ROOM FAILED");
        }
    
        // this is called
        public override void OnConnectedToMaster()
        {
            if (!PhotonNetwork.OfflineMode)
            {
                Debug.Log("ON CONNECTED TO MASTER");
                PhotonNetwork.JoinLobby(Lobby);
            }
        }
    
        // this is called
        public override void OnConnected()
        {
            Debug.Log("ON CONNECTED");
        }
    
        // this is never called
        public override void OnDisconnected(DisconnectCause cause)
        {
            Debug.LogError("ON DISCONNECTED");
        }
    }
    
    public class PhotonMatchmaking : MonoBehaviourPunCallbacks
    {
        private static PhotonMatchmaking instance;
        private static int currentMaxPlayerCount;
        private static readonly string[] lobbyPropertiesKeys = new string[] { "C0", "gameId", "gameTitle", "gameSlug" };
    
        private void Awake()
        {
            instance = this;
        }
    
        // This is called from the game loading routine but it never ends because InRoom is Never true
        public static bool JoinOrCreateRoom(int playableAssetCount)
        {
            // in offline mode we create a simple room
            if (PhotonNetwork.OfflineMode && !PhotonNetwork.InRoom)
            {
                return PhotonNetwork.CreateRoom("Room");
            }
    
            currentMaxPlayerCount = playableAssetCount;
    
            return CreateRoom();
        }
    
        private static bool CreateRoom()
        {
            var roomPropsInLobby = lobbyPropertiesKeys;
            PhotonHashTable customRoomProperties = CreateRoomCustomProperties();
            var roomOptions = new RoomOptions { CustomRoomProperties = customRoomProperties, CustomRoomPropertiesForLobby = roomPropsInLobby, MaxPlayers = (byte)currentMaxPlayerCount, CleanupCacheOnLeave = false };
            return PhotonNetwork.CreateRoom(null, roomOptions, PhotonConnect.Lobby);
        }
    
        public override void OnCreatedRoom()
        {
            Debug.LogError("OnCreatedRoom");
        }
    
        public override void OnJoinedRoom()
        {
            Debug.LogError("OnJoinedRoom");
        }
    
        public static PhotonHashTable CreateRoomCustomProperties()
        {
            return new PhotonHashTable { { "C0", 0 }, { "gameId", Game.CurrentGameId }, { "gameTitle", Game.CurrentGameTitle }, { "gameSlug", Game.CurrentGameSlug } };
        }
    }
  • raphael
    raphael
    edited December 2019
    Options

    Thanks a lot, I will try it out without waitUntil. I know that InRoom is false because I have written it in the OnGUI of another script. So I guess I should put the JoinOrCreateRoom into the OnJoinedLobby and the starting of the game into the OnJoinedRoom?

  • JohnTube
    JohnTube ✭✭✭✭✭
    Options

    Hi @raphael,

    So I guess I should put the JoinOrCreateRoom into the OnJoinedLobby and the starting of the game into the OnJoinedRoom?

    Yes. However if you don't need rooms listing, no need to join a lobby at all.

    Remove JoinLobby call and put JoinOrCreateRoom in OnConnectedToMaster or as a result of a button or something else.