How to add a string to a player custom properties

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

How to add a string to a player custom properties

shemtom
2021-11-11 17:51:19

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;          
 }    

Comments

XSpitFire
2021-11-29 15:58:33

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;    

  }    
}    
Back to top