Search Unity

Third Party Using SetCustomRoomProperty (Photon)

Discussion in 'Multiplayer' started by CyberInteractiveLLC, Jul 10, 2018.

  1. CyberInteractiveLLC

    CyberInteractiveLLC

    Joined:
    May 23, 2017
    Posts:
    307
    Is this the proper way to use SetcustomProperty? I Want to change them in-game, after each match, however, I don't know exactly how to do it properly, Thanks

    Code (CSharp):
    1.         RoomOptions roomOptions = new RoomOptions();
    2.  
    3.         string currentMap = (string)PhotonNetwork.room.CustomProperties[RoomCustomProperties.MapName];
    4.         currentMap = "New Map";
    5.  
    6.         roomOptions.CustomRoomProperties = new ExitGames.Client.Photon.Hashtable();
    7.         roomOptions.CustomRoomProperties.Add(RoomCustomProperties.MapName, currentMap);
    8.  
    9.         PhotonNetwork.room.SetCustomProperties(roomOptions.CustomRoomProperties);
     
  2. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    I wrote an extension method that handles setting room properties.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public static class PhotonRoomExtensions
    6. {
    7.     public static void SetCustomProperty(this Room room, string propName, object value)
    8.     {
    9.         ExitGames.Client.Photon.Hashtable prop = new ExitGames.Client.Photon.Hashtable();
    10.         prop.Add(propName, value);
    11.         room.SetCustomProperties(prop);
    12.     }
    13.  
    14.     public static void SetCustomProperty(this Room room, string propName, object value, object oldValue)
    15.     {
    16.         ExitGames.Client.Photon.Hashtable prop = new ExitGames.Client.Photon.Hashtable();
    17.         prop.Add(propName, value);
    18.         ExitGames.Client.Photon.Hashtable oldvalueProp = new ExitGames.Client.Photon.Hashtable();
    19.         oldvalueProp.Add(propName, oldValue);
    20.         room.SetCustomProperties(prop, oldvalueProp);
    21.     }
    22. }
    23.  

    call it using something like this
    PhotonNetwork.room.SetCustomProperty("mapIndex", 1);


    The 2nd variant of the function call is for if you need to do a CAS operation on the property.

    HTH
     
  3. CyberInteractiveLLC

    CyberInteractiveLLC

    Joined:
    May 23, 2017
    Posts:
    307
    Thank you very much, Just a question, the Hash and propName were already made when the room was created, is this the way to simply change the Value of the already created hashtable ? the .Add confuses me
     
  4. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    The hashtable is just used to tell the Photon system which property(s) to set each time you call SetCustomRoomProperties.

    If a property name in the hashtable doesn't exist in the room properties it is created and set to the value supplied, otherwise the existing property value is updated. So if you want to update the value of an existing room property, just call SetCustomRoomProperties with the name of the existing property and the new value.

    You can if you want, set multiple properties in one call by adding more than one value to the hashtable, although my extension methods don't support that.