Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Third Party [PUN] JoinRandomRoom not working with custom properties.

Discussion in 'Multiplayer' started by Alekxss, Nov 10, 2015.

  1. Alekxss

    Alekxss

    Joined:
    Mar 25, 2013
    Posts:
    170
    Hi to all.
    I have simple multiplayer connection:

    1. Select level (scene)
    2. Try to join random room with same scene.
    3. If rooms not exist - create new room and wait for players.

    As docs says - i need to use custom properties when creating room and join random, heres code:

    in top:
    Code (CSharp):
    1. using Hashtable = ExitGames.Client.Photon.Hashtable;
    global variable
    Code (CSharp):
    1. int selectedLevelIndex = 0;
    next, on select level:
    Code (CSharp):
    1. void OnLevelSelected(int levelIndex)
    2. {
    3.     selectedLevelIndex = levelIndex;
    4.  
    5.     PhotonNetwork.autoJoinLobby = false;
    6.     PhotonNetwork.ConnectToRegion( CloudRegionCode.eu, "1.0");
    7.  
    8. .........
    9. }
    next, on connection (it works fine)
    Code (CSharp):
    1.     void OnConnectedToMaster()
    2.     {
    3.         StartRandomRoom();
    4.     }
    5.  
    6.     void StartRandomRoom()
    7.     {
    8.         PhotonNetwork.JoinRandomRoom(CreateRoomProperties(), 4, ExitGames.Client.Photon.MatchmakingMode.FillRoom, null, null);
    9.     }
    10.  
    11.     Hashtable CreateRoomProperties()
    12.     {
    13.         return new Hashtable
    14.         {
    15.             {"levelIndex", selectedLevelIndex}
    16.         };
    17.     }
    next, if room not exist, create new with same custom properties:
    Code (CSharp):
    1.     void OnPhotonRandomJoinFailed()
    2.     {
    3.         Debug.Log("Random join failed");
    4.  
    5.         var o = new RoomOptions();
    6.         o.isVisible = true;
    7.         o.isOpen = true;
    8.         o.maxPlayers = 4;
    9.         o.customRoomProperties = CreateRoomProperties();
    10.         o.customRoomPropertiesForLobby = null;
    11.  
    12.         PhotonNetwork.CreateRoom(null, o, null);
    13.     }
    as you can see, im using always same method for creating customRoomProperties, but cant connect to existing room, always new room starting on clients.

    if i change
    Code (CSharp):
    1.     Hashtable CreateRoomProperties()
    2.     {
    3.         return null;
    4.     }
    game works fine in default 1st scene.
    selectedLevelIndex is same on both sides (checked), event when hardcode it to zero, still cant join :(

    Maybe anyone know what am i doing wrong?
    Thank you in advance.
     
  2. awaterpistol

    awaterpistol

    Joined:
    Sep 3, 2015
    Posts:
    25
    I'm still learning Photon myself so my answer may not be best practice but I know it works (it's what I use).

    You don't need to add custom properties when joining a random room. You can just call
    Code (csharp):
    1. PhotonNetwork.JoinRandomRoom();
    to join a random room. Then, in
    Code (csharp):
    1. OnPhotonRandomJoinFailed()
    (which is called if there is no random room to join or it fails for a particular reason) create a room with
    Code (csharp):
    1. PhotonNetwork.CreateRoom(null);
    . Unless you're showing room names somewhere, null doesn't matter.

    Steps:

    1. Player tries to join random room.
    1.1 He'll either join any random room, or he'll fail.
    1.2 Exit here and don't continue.
    2. Player will create himself a room which others can join.
    2.1 Exit here and don't continue.

    Referencing the Marco Polo tutorial: https://doc.photonengine.com/en/pun/current/tutorials/tutorial-marco-polo
     
  3. Alekxss

    Alekxss

    Joined:
    Mar 25, 2013
    Posts:
    170
    yes, with nulls works fine, but i have rooms with different scenes and trying to connect to same scene as selected.
    For example, 1 and 2 players start to play in different scenes, they started as hosts and waiting another players to play.
    3d player select same scene as player 1, and he must to connect to player 1, but he creating new room as player 1 and waiting.
    Filter not working, or i setup it not correct.
    Anyway, thanks for answer ;)
     
    Last edited: Nov 11, 2015
  4. awaterpistol

    awaterpistol

    Joined:
    Sep 3, 2015
    Posts:
    25
    http://forum.photonengine.com/discussion/5146/rooms-list-map-selection-rotation Maybe this will help?
     
  5. Alekxss

    Alekxss

    Joined:
    Mar 25, 2013
    Posts:
    170
    Thanks again, yes, i will do rooms list and select from it if not find solution with JoinRandomRoom
     
  6. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,057
    This seems to be at the core of your problems.

    The server that's doing the matchmaking (Master Server) will only get select room properties, which are defined when you create the room. With customRoomPropertiesForLobby, you define which key-values are relevant for matchmaking.

    In your case, have the "levelIndex" in that array.

    You don't need to join the lobby, to do the server's work on the client. Unless you really want to show a list of rooms to players, this is not recommended.

    See: Room Properties and the Lobby.
     
    hassan7451000 and sonal_dhole like this.
  7. Alekxss

    Alekxss

    Joined:
    Mar 25, 2013
    Posts:
    170
    Many thanks, now works fine.

    Code (CSharp):
    1.     EXTHashtable CreateRoomProperties()
    2.     {
    3.         return new EXTHashtable
    4.         {
    5.             {"levelIndex", 0}
    6.         };
    7.     }
    8.  
    9.     string[] CreateRoomPropertiesForLobby()
    10.     {
    11.         return new string[]
    12.         {
    13.             "levelIndex"
    14.         };
    15.     }
    16.  
    17.     void OnPhotonRandomJoinFailed()
    18.     {
    19.         Debug.Log("Random join failed");
    20.  
    21.         var o = new RoomOptions();
    22.         o.isVisible = true;
    23.         o.isOpen = true;
    24.         o.maxPlayers = 4;
    25.         o.customRoomProperties = CreateRoomProperties();
    26.         o.customRoomPropertiesForLobby = CreateRoomPropertiesForLobby();
    27.  
    28.         PhotonNetwork.CreateRoom(null, o, null);
    29.     }
     
  8. ThatoMogale

    ThatoMogale

    Joined:
    Feb 5, 2018
    Posts:
    4
    Hi, which join RandomRoom function did you use?
     
  9. IneptStudio

    IneptStudio

    Joined:
    Mar 7, 2022
    Posts:
    12
    Link not working
     
  10. IneptStudio

    IneptStudio

    Joined:
    Mar 7, 2022
    Posts:
    12
    Okay guys , after 3 days i was finally able to do it :

    using Hashtable = ExitGames.Client.Photon.Hashtable;


    public void CreateRoom()
    {

    RoomOptions o = new RoomOptions();
    o.IsVisible = true;
    o.IsOpen = true;
    o.MaxPlayers = 4;
    o.CustomRoomProperties = new Hashtable
    {
    {"levelIndex", 0}
    };
    o.CustomRoomPropertiesForLobby = new string[]
    {
    "levelIndex"
    };

    PhotonNetwork.CreateRoom(null, o, null);
    }


    public void JoinRandomRoom()
    {
    Hashtable expectedCustomRoomProperties = new Hashtable { { "levelIndex", 0 } };
    PhotonNetwork.JoinRandomRoom(expectedCustomRoomProperties, 4);
    }


    This code will join player with levelIndex = 0.