Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    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. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    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:
    200
    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:
    933
    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:
    200
    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:
    933
    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.