Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

[PUN] customProperties[key] where value is a string

Discussion in 'Scripting' started by renaiku, Nov 30, 2016.

  1. renaiku

    renaiku

    Joined:
    Sep 28, 2016
    Posts:
    13
    Hi !

    I got that code:

    Code (CSharp):
    1.     void DrawPartiesList(string search)
    2.     {
    3.         ClearPartiesList();
    4.         foreach (RoomInfo party in PhotonNetwork.GetRoomList())
    5.         {
    6.             string password = party.customProperties["pwd"];
    7.  
    8.             if (password != "")
    9.             {
    10.                 Image privateRoom = listItem.GetComponent<Image>();
    11.                 privateRoom.gameObject.SetActive(true);
    12.             }
    13.         }
    14.     }
    Everything works fine but
    says that I can't convert object to string ...

    When I create the custom property like:

    Code (CSharp):
    1.  
    2. options = new RoomOptions();
    3. options.MaxPlayers = 4;
    4. options.CustomRoomProperties = new ExitGames.Client.Photon.Hashtable() { { "pwd", partyPwd } };
    5. foreach (var key in options.CustomRoomProperties.Keys)
    6. {
    7.     Debug.Log(key + ": " + options.CustomRoomProperties[key] + " of type " + options.CustomRoomProperties[key].GetType());
    8. }
    the log returns:

    I don't get it !


    EDIT:

    Ok, I found out that
    is null ...

    But since i setted it I don't understand :(
     
    Last edited: Nov 30, 2016
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    On one you have customProperties and on the other you have CustomRoomProperties -- are you sure you're setting / checking the right fields?
     
    renaiku likes this.
  3. renaiku

    renaiku

    Joined:
    Sep 28, 2016
    Posts:
    13
    OMG Yes

    --- Applying changes ---

    Ok, theres still a problem:


    Code (CSharp):
    1. void Start () {
    2.         PhotonNetwork.autoJoinLobby = true;
    3.         partyName = GameObject.Find("Party Name InputField Text").GetComponent<Text>().text;
    4.         partyPwd = GameObject.Find("Party Password InputField Text").GetComponent<Text>().text;
    5.         options = new RoomOptions();
    6.         options.MaxPlayers = 4;
    7.         PhotonNetwork.ConnectUsingSettings(_gameVersion);
    8.     }  
    9.  
    10. void OnJoinedLobby()
    11.     {
    12.         PhotonNetwork.CreateRoom(partyName, options, null);
    13.     }
    14.  
    15. void OnCreatedRoom()
    16.     {
    17.         PhotonNetwork.room.SetCustomProperties(new ExitGames.Client.Photon.Hashtable() {{ "pwd", partyPwd }});
    18.     }

    And elsewhere:

    Code (CSharp):
    1. foreach (Room party in PhotonNetwork.GetRoomList())
    2.         {
    3.             ...
    4.  
    5.             string password = party.customProperties["pwd"];
    6.             ...
    7.          }
    8.  
    this last piece of code is still returning that party.customProperties["pwd"] is an object, when it's a string ... :'( (or at least the value is a string, i'm missing something here?)
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    I'm not familiar with the Photon hashtable, but it might be storing the value as an object since you never seem to declare the type. Try unboxing the result:

    Code (csharp):
    1.  
    2. string password = (string)party.customProperties["pwd"];
    3.  
     
  5. renaiku

    renaiku

    Joined:
    Sep 28, 2016
    Posts:
    13
    Nope, it must comes from elsewhere:


    Code (CSharp):
    1. // Script 1
    2. void OnJoinedLobby()
    3.     {
    4.         options = new RoomOptions();
    5.         options.MaxPlayers = 4;
    6.         options.CustomRoomProperties = new ExitGames.Client.Photon.Hashtable();
    7.         options.CustomRoomProperties.Add("pwd", partyPwd);
    8.         options.CustomRoomPropertiesForLobby = new string[] { "pwd" };
    9.         PhotonNetwork.CreateRoom(partyName, options, null);
    10.     }
    11.     void OnCreatedRoom()
    12.     {
    13.         Debug.Log("Host created a Room");
    14.         Debug.Log("WADAFUK: " + PhotonNetwork.room.customProperties["pwd"]); // The password is shown, ALL OK.
    15.     }
    16.  
    17.  
    18.  
    19. // Script 2
    20. void OnReceivedRoomListUpdate()
    21.     {
    22.         DrawPartiesList("");
    23.     }
    24.  
    25. void DrawPartiesList(string search)
    26.     {
    27.         foreach (RoomInfo party in PhotonNetwork.GetRoomList())
    28.         {
    29.             GameObject listItem = Instantiate(listItemPrefab) as GameObject;
    30.             listItem.transform.SetParent(partyListContainer.transform, false);
    31.             string name = party.name;
    32.             string players = party.playerCount.ToString() + "/" + party.maxPlayers;
    33.  
    34.             string password = (string)party.customProperties["pwd"];
    35.             Debug.Log("debug: " + password); // console shows "debug: " ...
    36.         }
    37.     }
     
  6. chrysu76

    chrysu76

    Joined:
    Nov 17, 2016
    Posts:
    2
    Did you get to resolve this? am stuck with this issue :(
     
  7. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    429
    chrysu76 likes this.
  8. chrysu76

    chrysu76

    Joined:
    Nov 17, 2016
    Posts:
    2
    THANKS!!