Home > Mobile >  Room's custom properties don't get updated | Photon PUN2 | Unity
Room's custom properties don't get updated | Photon PUN2 | Unity

Time:04-09

I have a system where a player can create a room and then other players can join that room. At any point the master client can change scenes for everyone in the room and they can start playing but other players can still join the room and will instantly get there scenes synced. However i would like to show the players that are choosing a room to join wether or not the game in that room has already bagun. I decided to do that by using the customproperties of a room to save an integer which can be either 0 (players are still waiting) or 1 (players are in game). I set the custom properties after getting a callback on the master client that the room was created, however the custom properties don't change after i use SetCustomProperties.

    public override void OnCreatedRoom()
{
    base.OnCreatedRoom();
    ExitGames.Client.Photon.Hashtable roomProps = new ExitGames.Client.Photon.Hashtable();
    roomProps["inGame"] = 0;
    PhotonNetwork.CurrentRoom.SetCustomProperties(roomProps);
}

    public void StartLobbyGame() {
    ExitGames.Client.Photon.Hashtable roomProps = new ExitGames.Client.Photon.Hashtable();
    roomProps["inGame"] = 1;
    PhotonNetwork.CurrentRoom.SetCustomProperties(roomProps);
    PhotonNetwork.LoadLevel("InGame");
}

Any idea why that is ?

CodePudding user response:

You are creating an empty hashtable, so it doesn't contains the key "inGame". You need to add it first before accessing it using roomProps["inGame"] = 0;

You can add keys like that :

ExitGames.Client.Photon.Hashtable roomProps = new ExitGames.Client.Photon.Hashtable() { {"inGame", 0} };

or

roomProps.Add("inGame", 0);

CodePudding user response:

I fixed my issue by adding the custom property to the room options before creating the room with those options. After that in my start game method i change the value of the property from 0 to 1.

 public void CreateRoom(string name)
{
    RoomOptions ropts = new RoomOptions() { IsOpen = true, IsVisible = true, MaxPlayers = 8 };
    ExitGames.Client.Photon.Hashtable roomProps = new ExitGames.Client.Photon.Hashtable();
    roomProps.Add("inGame", 0);
    ropts.CustomRoomProperties = roomProps;
    PhotonNetwork.CreateRoom(name, ropts);
}


    public void StartLobbyGame() {
    PhotonNetwork.CurrentRoom.CustomProperties["inGame"] = 1;
    PhotonNetwork.LoadLevel("InGame");
}
  • Related