Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. On February 28th the Feedback website will shut down and be redirected to the Unity forums. See the full post for more information.
    Dismiss Notice
  5. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  6. Unity 2018.3 is now released.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Using SetCustomRoomProperty (Photon)

Discussion in 'Connected Games' started by Smart-hawk1, Jul 10, 2018.

  1. Smart-hawk1

    Smart-hawk1

    Joined:
    May 23, 2017
    Posts:
    232
    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,050
    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. Smart-hawk1

    Smart-hawk1

    Joined:
    May 23, 2017
    Posts:
    232
    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,050
    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.