Custom properties are known by Masterclient only

Options
The MasterClient says via RPC that a special menu will be enabled for all players - what already works. At every player's screen a text should be set, which tells him to which team he belongs. It works on the MasterClient, but not on the another clients. The clients get a null pointer exception at "string team = playerInfo.CustomProperties["Teamnr"].ToString();" Does anybody know what the problem is? It looks like the custom properties are deleted after the masterClient read it. PS.: Setting the team number for every player's custom properties is already working.

//If Button clicked
public void OnClick_StartGame()
{
if (PhotonNetwork.IsMasterClient)
{
if(PhotonNetwork.CurrentRoom.PlayerCount != PhotonNetwork.CurrentRoom.MaxPlayers)
{
info.text = "Not enough Players to start";
return;
}
info.text = "";

PhotonNetwork.CurrentRoom.IsOpen = false;
PhotonNetwork.CurrentRoom.IsVisible = false;

setTeam = new SetTeam();
setTeam.SetPlayerToTeam();

base.photonView.RPC("EnableAbilityMenu", RpcTarget.All, true);
}

}

[PunRPC]
private void EnableAbilityMenu(bool b)
{
if(b == true)
{
//This is working for all Clients
createOrJoinRoomMenu.SetActive(false);
currentRoomCanvasMenu.SetActive(false);
abilityMenu.SetActive(true);

//This is working only at masterClient
foreach (var playerInfo in PhotonNetwork.PlayerList)
{
string team = playerInfo.CustomProperties["Teamnr"].ToString(); //Here appears the null
reference exception at the clients

if (team.Equals("1"))
{
abilityMenuSubheading.text = "You're in Team: Attacking";
}
else
{
abilityMenuSubheading.text = "You're in Team: Defending";
}

}

}
}

Comments

  • Tobias
    Options
    You have to use SetCustomProperties() to actually set values and get them synced.
    This will send the new key values to the server and after receiving the confirmation from there, the values update locally.
  • J3ramy
    Options
    Do you mean like in this code? This works already for all players but only the master client can see the values. It does'nt make sense to me ^^

    public ExitGames.Client.Photon.Hashtable customProperties;

    //Method called by master client
    public void SetPlayerToTeam()
    {
    customProperties = new ExitGames.Client.Photon.Hashtable();
    Debug.Log("Hashtable davor");
    for (int j = 0; j < PhotonNetwork.CurrentRoom.PlayerCount; j++)
    {
    int team = Random.Range(0, PhotonNetwork.CurrentRoom.PlayerCount);
    //Debug.Log("Playercount: " + PhotonNetwork.CurrentRoom.PlayerCount);
    if (!available.Contains(team))
    {
    available.Add(team);
    //j++;
    }
    else
    {
    j--;
    }
    }

    int i = 0;
    foreach(KeyValuePair<int, Player> playerInfo in PhotonNetwork.CurrentRoom.Players)
    {
    if (available < PhotonNetwork.CurrentRoom.PlayerCount / 2)
    {
    playerInfo.Value.CustomProperties["Teamnr"] = 1;
    playerInfo.Value.SetCustomProperties(customProperties);
    }
    else
    {
    playerInfo.Value.CustomProperties["Teamnr"] = 2;
    playerInfo.Value.SetCustomProperties(customProperties);
    }

    //correct output for every player
    Debug.Log("Teamnummer: " + playerInfo.Value.CustomProperties["Teamnr"]);
    i++;
    }
    }

    @Tobias
  • J3ramy
    Options
    I fixed the problem. I just forgot to use the "customProperties.Add("Teamnr", 0);"-Function

    private void SetTeam()
    {
    customProperties = new ExitGames.Client.Photon.Hashtable();
    customProperties.Add("Teamnr", 0);

    //Ansatz für random Teamaufteilung; Fehler: RoomProperties kann nicht in List<int> gecastet werden
    //List<int> availableTeamnr = (List<int>)PhotonNetwork.CurrentRoom.CustomProperties["AvailableTeam"];

    //if (availableTeamnr[PhotonNetwork.LocalPlayer.ActorNumber - 1] <= PhotonNetwork.CurrentRoom.PlayerCount / 2)
    if (PhotonNetwork.LocalPlayer.ActorNumber <= PhotonNetwork.CurrentRoom.PlayerCount / 2)
    {
    PhotonNetwork.LocalPlayer.CustomProperties["Teamnr"] = 1;
    PhotonNetwork.LocalPlayer.SetCustomProperties(customProperties);
    }
    else
    {
    PhotonNetwork.LocalPlayer.CustomProperties["Teamnr"] = 2;
    PhotonNetwork.LocalPlayer.SetCustomProperties(customProperties);
    }
    Debug.Log("Teamnummer: " + PhotonNetwork.LocalPlayer.CustomProperties["Teamnr"]);
    }