Update a custom property photon
Options
How can I update a property already existing in Photon?
Let's say inside `PlayerLives` I already have a number (let's say the player has as the moment `2` lives), and in the following function I want to increase it with 1:
I also tried setting the custom property to null and then re-assign:
but in this case I get the error
**Exception in callback: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException: An item with the same key has already been added. Key: PlayerLives**
How can I update the custom property the right way? Thank you for your time!
Let's say inside `PlayerLives` I already have a number (let's say the player has as the moment `2` lives), and in the following function I want to increase it with 1:
public void update_lives() { PunHashtable _myCustomProperties = new PunHashtable(); int x = (int)PhotonNetwork.LocalPlayer.CustomProperties["PlayerLives"]; // this is 2 _myCustomProperties.Add("PlayerLives", x++); PhotonNetwork.LocalPlayer.SetCustomProperties(_myCustomProperties); if (PhotonNetwork.LocalPlayer.CustomProperties.ContainsKey("PlayerLives")) { int updated = (int)PhotonNetwork.LocalPlayer.CustomProperties["PlayerLives"]; print("AHOI " + updated); **// this is still 2 as the previous value** } }Why isn't the value updated?
I also tried setting the custom property to null and then re-assign:
public void update_lives() { PunHashtable _myCustomProperties = new PunHashtable(); int x = (int)PhotonNetwork.LocalPlayer.CustomProperties["PlayerLives"]; _myCustomProperties.Add("PlayerLives", null); _myCustomProperties.Add("PlayerLives", x++); PhotonNetwork.LocalPlayer.SetCustomProperties(_myCustomProperties); if (PhotonNetwork.LocalPlayer.CustomProperties.ContainsKey("PlayerLives")) { int updated = (int)PhotonNetwork.LocalPlayer.CustomProperties["PlayerLives"]; print("AHOI " + updated); } }
but in this case I get the error
**Exception in callback: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException: An item with the same key has already been added. Key: PlayerLives**
How can I update the custom property the right way? Thank you for your time!
0
Comments
-
Later edit: I saw that I need to use https://doc.photonengine.com/en-us/pun/current/getting-started/migration-notes#callbacks_changes OnPlayerPropertiesUpdated but I'm not very sure how to use it. Haven't found an example on the Internet yet.
Later edit2: here is my solution, I don't know if this is the right way, but at least it's working. If someone sees something wrong.. please don't hesitate to shout out.
I defined this override function OnPlayerPropertiesUpdate that comes from MonoBehaviourPunCallbackspublic override void OnPlayerPropertiesUpdate(Player targetPlayer, ExitGames.Client.Photon.Hashtable changedProps) { base.OnPlayerPropertiesUpdate(targetPlayer, changedProps); if (targetPlayer != null && targetPlayer == PhotonNetwork.LocalPlayer) { if (changedProps.ContainsKey("PlayerLives")) { SetPlayerLives(targetPlayer); } } }
Next, in the SetPlayerLives I update the custom property:public void SetPlayerLives() { int playerLives = (int)PhotonNetwork.LocalPlayer.CustomProperties["PlayerLives"]; playerLives++; PunHashtable _myCustomProperties = new PunHashtable(); _myCustomProperties["PlayerLives"] = playerLives; PhotonNetwork.SetPlayerCustomProperties(_myCustomProperties); setLivesHUDOnUpdates_Saloon(PhotonNetwork.LocalPlayer); }
And in the setLivesHUDOnUpdates I update the player score on HUDpublic void setLivesHUDOnUpdates(Player player) { int result; if (player.CustomProperties.ContainsKey("PlayerLives")) { result = (int)player.CustomProperties["PlayerLives"]; if (_HUDtext.tag == "Lives") { _HUDtext.text = "LIVES: " + result.ToString(); } } }
0 -
Hi @tenzolinho,
Thank you for choosing Photon!
FYI I replied here but your solution posted in this discussion is good.0