SetCustom property for room (a Totally NOOBish question)

Options
Hello dear Photon users! I am new to networking and I would like to ask for your help.

I've created a simple server-browser. Now it displays a server name (read from RoomInfo) and connects it a button is pressed. I now want to be able to say which map is currently running in what room, while being in the lobby. As I understood it is done with the help of the "Room" and not in the "RoomInfo" class.

Could you please list steps I need to follow in order to have the script working?

As I understand:
1. we need to declare a Room variable, responsible for a property we want to change.
2. we need to create a Hashtable variable (because PUN API says something about hashtables) which will contain all properties and their indices.
3. When asked, we need to run the function which compares the index from the hashtable, retrieves the corresponding property and fetches through Room class.

Am I correct?
=========================
public Room mapName;
public Hashtable mapNameslist;

void Start(){
mapNameslist = new Hashtable();
mapNameslist.Add(1, "ourfirstmap");
mapNameslist.Add(2,"oursecondmap");
}

void Update(){
Debug.Log(PhotonNetwork.room.customProperties.ToString);
}

public void OnJoinedRoom(){
if (Input.GetKeyDown (KeyCode.Q)) {
mapName.SetCustomProperties ( mapNameslist, 1);
}
if (Input.GetKeyDown (KeyCode.Q)) {
mapName.SetCustomProperties ( mapNameslist, 2);
}
}


Thank you very much!

API:
http://doc-api.photonengine.com/en/pun/current/class_room.html#details
http://docs.unity3d.com/ScriptReference/Hashtable.Add.html

Answers

  • any ideas? :)
  • Tobias
    Options
    Read "Custom Properties" here:
    https://doc.photonengine.com/en/pun/current/tutorials/synchronization-and-state

    and "Not so random matchmaking" on this page:
    https://doc.photonengine.com/en/pun/current/tutorials/matchmaking-and-lobby

    You need to use Custom Properties for the room (to store the map) and define that Custom Property as "room properties shown in the lobby" when you create rooms. That makes it show up in the lobby, which means you should find it in the Hashtable "customProperties" (per RoomInfo).
  • Opfjgnweiniwenf
    edited November 2015
    Options
    Thanks for a quick reply, but I still can't make the script work.
    Could you have a look?
    It debugs.log the following: only room name and zero custom property count.

    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;
    using System.Collections.Generic;


    public class sendcustominfoscript : MonoBehaviour {

    public RoomInfo[] availableRooms = null;
    public GameObject temp;
    public GameObject mybutton;
    public Transform panel_target;


    public string chosenLevel = "Some Level";
    public string gameType = "TDM";
    public ExitGames.Client.Photon.Hashtable customPropertiesToSet;

    // public RoomOptions customRoomPropertiesForLobby;

    // Use this for initialization
    void Start () {

    //add custom properties to Hashtable

    // customPropertiesToSet = new Hashtable();
    // customPropertiesToSet.Add(1, chosenLevel);
    // customPropertiesToSet.Add(2, gameType);


    ExitGames.Client.Photon.Hashtable customPropertiesToSet = new ExitGames.Client.Photon.Hashtable();
    customPropertiesToSet.Add("1", chosenLevel);
    customPropertiesToSet.Add("2", gameType);

    //set which custom properties will be sent to the lobby
    string[] customPropertiesForLobby = new string[2];
    customPropertiesForLobby [0] = "1";
    customPropertiesForLobby [1] = "2";


    //string[] customPropertiesForLobby = new string[2];
    // customPropertiesForLobby[0] = "l";
    // customPropertiesForLobby[1] = "t";

    }

    // Update is called once per frame
    void Update () {

    //if in lobby - update serverlist, get server name and get server custom properties
    if (Input.GetKeyDown (KeyCode.B)) {
    availableRooms = PhotonNetwork.GetRoomList();
    if (availableRooms != null && availableRooms.Length > 0){
    Debug.Log("Number of available rooms: " + availableRooms.Length); // Length will get you the number of items in the array
    for (int i = 0; i < availableRooms.Length; i++){
    Debug.Log("Available Room #" + i+1 + " - Room name: " + availableRooms[i].name);
    Debug.Log("custom properties:" + availableRooms[i].customProperties.Count);
    temp = (GameObject)Instantiate(mybutton);
    temp.name = availableRooms[i].name;
    temp.transform.parent = panel_target;
    }
    }
    }

    //get server properties
    if (Input.GetKeyDown (KeyCode.M)) {
    foreach (RoomInfo roomInfo in PhotonNetwork.GetRoomList()) {
    Debug.Log(roomInfo.name + " " + roomInfo.customProperties [1] + roomInfo.customProperties ["1"] + roomInfo.customProperties [chosenLevel]);
    // Debug.Log(roomInfo.name + " " + roomInfo.customProperties);
    }
    }


    //if in room - set custom properties declared in Start()
    if (Input.GetKeyDown (KeyCode.D)) {
    PhotonNetwork.room.SetCustomProperties (customPropertiesToSet);
    }

    }
    }
  • vadim
    Options
    As Tobias said, when creating the room, pass list of properties names as propsToListInLobby parameter.
    There is no CreateRoom call in your code at all. So impossible to say what's wrong with it.