Struggling to create a room with custom Room Options and then connect players together

Hey guys, new here. So basically what I'm trying to achieve is to connect X (2-5) players together into a game.
There are 2 type of GameModes and within each type there is 2-5 player modes (all modes are pre set MaxPlayers)
How it would work logically is:
public const string GAME_MODE_PROP_KEY = "gm"; //0 boss, 1 = quest (2 modes)
    public const string PLAYERCOUNT_PROP_KEY = "pc"; //2-5
- Player selects game mode where they want to Fight a boss (bosses or quests) and also choses 2 Player (2-5 player).
Button click code:
if (PhotonNetwork.IsConnected)
            JoinRandomRoom(0, 2, 2); // (GameMode, PlayerCount, MaxPlayers)
        else
            PhotonNetwork.ConnectUsingSettings();
Then within joinRandomRoom it will either join or create:
private void JoinRandomRoom(byte gameMode, byte playerCount, byte expectedMaxPlayers)
    {
        Hashtable expectedCustomRoomProperties = new ExitGames.Client.Photon.Hashtable { { GAME_MODE_PROP_KEY, gameMode },
            { PLAYERCOUNT_PROP_KEY, playerCount } };
        PhotonNetwork.JoinRandomRoom(expectedCustomRoomProperties, expectedMaxPlayers);
    }
If it fails to join random:
public override void OnJoinRandomFailed(short returnCode, string message)
    {
        CreateRoom_Boss_2Player();
    }
private void CreateRoom_Boss_2Player()
    {
        RoomOptions roomOptions = new RoomOptions();
        string[] lobby = { GAME_MODE_PROP_KEY, PLAYERCOUNT_PROP_KEY };
        roomOptions.CustomRoomPropertiesForLobby = lobby;
        roomOptions.MaxPlayers = (byte)2;
        roomOptions.CustomRoomProperties = new ExitGames.Client.Photon.Hashtable { { GAME_MODE_PROP_KEY, 0 }, { PLAYERCOUNT_PROP_KEY, 2 } };
        PhotonNetwork.CreateRoom(null, roomOptions, null);
    }
Then to see if the room is now full I check:
public override void OnPlayerEnteredRoom(Player newPlayer)
    {
        if (PhotonNetwork.IsMasterClient)
        {
            if (PhotonNetwork.CurrentRoom.PlayerCount == PhotonNetwork.CurrentRoom.MaxPlayers)
            {
                PhotonNetwork.CurrentRoom.IsOpen = false;
                PhotonNetwork.CurrentRoom.IsVisible = false;

                PhotonNetwork.LoadLevel("Multiplayer_Boss_2P_1");
            }
        }
}
I followed the documentation on photons website for PUN to try and do this on my own, and no errors are thrown when I test it.
However, the results of the test are: Client 1 clicks button, fails to join, so it creates room (good thats as expected),
Client 2 then clicks button (expected to join room), but instead it too creates a room and OnPlayerEnteredRoom is never called (I removed all my Debug.Log lines to keep it shorter).
I'm not too sure if the CustomRoomProperty im using "pc" for PlayerCount is needed, or if I can achieve that just by finding the room with the MaxPlayers value.

Any help to put me in the right direction would very much be appreciated!
Thanks,
Connor

Comments

  • Solved it, in case anyone else is looking for a solution to this as well, documentation that works is:
    https://answers.unity.com/questions/1718924/photon-network-wont-join-random-room-with-a-custom.html

    the custom properties of roomOptions needs to be declared like so:
    ExitGames.Client.Photon.Hashtable customPropreties = new ExitGames.Client.Photon.Hashtable();
            customPropreties[GAME_MODE_PROP_KEY] = 0;
            RoomOptions roomOptions = new RoomOptions()
            {
                CustomRoomProperties = customPropreties, IsVisible = true, IsOpen = true, MaxPlayers = (byte)2, CleanupCacheOnLeave = false
            };
            roomOptions.CustomRoomPropertiesForLobby = new string[]
            {
                 GAME_MODE_PROP_KEY
            };
            PhotonNetwork.CreateRoom(null, roomOptions);
    
  • Hello @LeeroiJenkiins I am trying to do something similar, when I press my button I am creating a room with a max of 2 players and if the room is full or it doesn't exist I want to create a new room, please some help or advice with my script please?
    using Photon.Pun;
    using Photon.Realtime;
    using UnityEngine;
    using UnityEngine.UI;
    
    namespace PhotonMenu.Menus {
    
        public class MainMenu : MonoBehaviourPunCallbacks
        {
            [SerializeField] private GameObject playerNameInput = null;
            [SerializeField] private GameObject findOpponentPanel = null;
            [SerializeField] private GameObject waitingStatusPanel = null;
            [SerializeField] private Text waitingStatusText = null;
    
            private bool isConnecting = false;
    
            private const string GameVersion = "0.1";
            private const int MaxPlayerPerRoom = 2;
    
            private void Awake() => PhotonNetwork.AutomaticallySyncScene = true;
    
            public void FindOpponent() {
    
                findOpponentPanel.SetActive(false);
                waitingStatusPanel.SetActive(true);
    
                isConnecting = true;
    
                if (PhotonNetwork.IsConnected)
                    {
                        PhotonNetwork.JoinRandomRoom();
                    }
                    else
                    {
                        PhotonNetwork.GameVersion = GameVersion;
                        PhotonNetwork.ConnectUsingSettings();
                    }
                
                //waitingStatusText.text = "Entrando a la Sala...";
            }
    
    
            public override void OnConnectedToMaster()
            {
                Debug.Log("Connected To Master");
             
                    if (isConnecting) {
                    PhotonNetwork.JoinRandomRoom();
                        
                    }
                
            }
    
            public override void OnDisconnected(DisconnectCause cause)
            {
                waitingStatusPanel.SetActive(false);
                findOpponentPanel.SetActive(false);
                playerNameInput.SetActive(true);
    
                Debug.Log($"Disconnected due to: {cause}");
            }
    
            public override void OnJoinRandomFailed(short returnCode, string message)
            {
                
                Debug.Log("No clients are waiting for opponent, creating a new room");
                PhotonNetwork.JoinOrCreateRoom("Global", new RoomOptions { MaxPlayers = MaxPlayerPerRoom }, TypedLobby.Default);
                
            }
    
            public override void OnJoinedRoom()
            {
                
                Debug.Log("Client successfully joined a room");
                int playerCount = PhotonNetwork.CurrentRoom.PlayerCount;
                PhotonNetwork.LoadLevel("Scene_Main");
          
                //if (playerCount != MaxPlayerPerRoom)
                //{
                //    waitingStatusText.text = "Waiting for Opponent";
                //    Debug.Log("Client is waiting for an opponent");
                //}
                //else
                //{
                //    waitingStatusText.text = "Opponent Found";
                //    Debug.Log("Match is ready to begin");
                //}
            }
            public override void OnPlayerEnteredRoom(Player newPlayer)
            {
                if (PhotonNetwork.IsMasterClient)
                {
                    if (PhotonNetwork.CurrentRoom.PlayerCount == PhotonNetwork.CurrentRoom.MaxPlayers)
                    {
                        PhotonNetwork.CurrentRoom.IsOpen = false;
                        PhotonNetwork.CurrentRoom.IsVisible = false;
    
                        PhotonNetwork.LoadLevel("Scene_Main");
                    }
                }
            }
            //public override void OnPlayerEnteredRoom(Player newPlayer)
            //{
            //    //if (PhotonNetwork.CurrentRoom.PlayerCount >= 1) {
            //    //    //PhotonNetwork.CurrentRoom.IsOpen = false;
    
            //    //    waitingStatusText.text = "Opponent Found";
            //    //    Debug.Log("Match is ready to begin");
    
            //        PhotonNetwork.LoadLevel("Scene_Main");
            //    //}
            //}
    
        }
    }
    
  • Hello there friend,
    heres what I ended up using that works:
    public void OnBossButtonClicked_2Player()
        {
            isConnecting = true;
            if (PhotonNetwork.IsConnected && isConnectedToMultiplayer)
                JoinRandomRoom(0, 2);
            else
                ConnectToMultiplayer();
        }
    
    private void JoinRandomRoom(byte gameMode, byte expectedMaxPlayers)
        {
            gmValue = gameMode;
            pcValue = expectedMaxPlayers;
            ExitGames.Client.Photon.Hashtable customPropreties = new ExitGames.Client.Photon.Hashtable();
            customPropreties[GAME_MODE_PROP_KEY] = gameMode;
            PhotonNetwork.JoinRandomRoom(customPropreties, expectedMaxPlayers);
        }
    
    public override void OnJoinedRoom()
        {
            Debug.Log("Connected to 2Player boss room, waiting for other player");
            PlayerPrefs.SetInt("Paused", 1);
            Hashtable props = new Hashtable
            {
                {"ll", false}, //loading level
                {"gl", 0 } //game level
            };
            PhotonNetwork.LocalPlayer.SetCustomProperties(props);
            player_camera_instance = Instantiate(player_camera, player_camera.transform.position, player_camera.transform.rotation);
            DontDestroyOnLoad(player_camera_instance);
            //load the waiting for player / loading game panel
        }
    
    public override void OnPlayerEnteredRoom(Player newPlayer)
        {
            Debug.Log("Other player entered room");
            if (PhotonNetwork.IsMasterClient)
            {
                if (PhotonNetwork.CurrentRoom.PlayerCount == PhotonNetwork.CurrentRoom.MaxPlayers)
                {
                    PhotonNetwork.CurrentRoom.IsOpen = false;
                    PhotonNetwork.CurrentRoom.IsVisible = false;
                    StartMultiplayerGame();
                }
            }
        }
    
    public override void OnConnectedToMaster()
        {
            isConnectedToMultiplayer = true;
            Debug.Log("Connected to master");
            if(isConnecting)
            {
                JoinRandomRoom(0, 2);
            }
        }
    private void CreateMultiplayerRoom()
        {
            Debug.Log("Could not find room to join, Creating Room");
            ExitGames.Client.Photon.Hashtable customPropreties = new ExitGames.Client.Photon.Hashtable();
            customPropreties[GAME_MODE_PROP_KEY] = gmValue;
            RoomOptions roomOptions = new RoomOptions()
            {
                CustomRoomProperties = customPropreties, IsVisible = true, IsOpen = true, MaxPlayers = pcValue, CleanupCacheOnLeave = false
            };
            roomOptions.CustomRoomPropertiesForLobby = new string[]
            {
                 GAME_MODE_PROP_KEY
            };
            PhotonNetwork.CreateRoom(null, roomOptions);
        }
    
        private void StartMultiplayerGame()
        {
            if (PhotonNetwork.IsMasterClient)
            {
                string gameMode = gmValue == 0 ? "Boss" : "Quest";
                string playerCount = "" + pcValue + "P";
                PhotonNetwork.LoadLevel("Multiplayer_" + gameMode + "_" + playerCount + "_1");
            }
        }
    
        private void ConnectToMultiplayer()
        {
            PhotonNetwork.AutomaticallySyncScene = true;
            PhotonNetwork.ConnectUsingSettings();
        }
    

    hope that helps :)