Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. 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:
    141
    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:
    796
    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:
    141
    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:
    796
    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.