How to add a string to a player custom properties

Options

I am trying to add a way for each player in the lobby to enter their certain text in an input field and later on an update to the player custom properties for each player. This was my code, unfortunately, I don't know how to call updates for the custom properties! I am really trying my best! Where did I go wrong?


public string StringWord;
public string saveWord;

public void GetWord()   
{    
    StringWord = PlayerPrefs.GetString("secret", "no secret");   
}   
public void SetWord()   
{    
    saveWord = InputSecret.text;
    PlayerPrefs.SetWord("save", saveWord);      
}
//added on click event of a button
public void OnClickMyWord()   
{     
    var hash = PhotonNetwork.LocalPlayer.CustomProperties;
    hash.Add("Word", saveWord);    
    PhotonNetwork.LocalPlayer.SetCustomProperties(hash);      
    if (!PhotonNetwork.IsMasterClient) return;    
    SetWord();    
    Debug.Log(PhotonNetwork.LocalPlayer.ToStringFull());   
}
public override void OnPlayerPropertiesUpdate(Player targetPlayer, ExitGames.Client.Photon.Hashtable changedProps)   
{    
    base.OnPlayerPropertiesUpdate(targetPlayer, changedProps);   
    if (!PhotonNetwork.IsMasterClient) return;     
    if (!changedProps.ContainsKey("Word")) return;      
 }


Answers

  • XSpitFire
    Options

    Hey,

    Maybe try to out a break point in the OnPlayerPropertiesUpdate and inspect the values coming in. Or add some debug messages.It looks like you just need to read the update values and do something with it.

    This is from memory, so not sure it is correct and will compile. Have a look.

    //setting the word
    ExitGames.Client.Photon.Hashtable p = PhotonNetwork.LocalPlayer.CustomProperties;
    p["Word"] = (string)"Word 1 2 3";
    PhotonNetwork.LocalPlayer.SetCustomProperties(p);
    
    public override void OnPlayerEnteredRoom(Player ps)
    {
        addPlayer(ps); // Keeping a list of players. This links their UI elements to the photon player.
    }
    
    public override void OnPlayerPropertiesUpdate(Player targetPlayer, ExitGames.Client.Photon.Hashtable changedProps)
    {
      base.OnPlayerPropertiesUpdate(targetPlayer, changedProps); 
      if (!changedProps.ContainsKey("Word")) return;        
     
      myPlayer = findPlayer(targetPlayer);  // get my UI element
      
      object Word;
      if (changedProps.TryGetValue("Word", out Word))
      {
        string ThePlayerWord = (string)Word;
        myPlayer.word = ThePlayerWord;
        Debug.Log("New word for "+targetPlayer.NickName+" is "+ThePlayerWord;
    
      }
    }