Change value of custom property
The whole answer can be found below.
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).
Change value of custom property
RaycoSantana
2015-02-02 09:45:12
I made some custom properties for my players (one of them is its health) how do you change the value of a custom property? :oops:
Comments
I typically use a static extensions class. If you put this in a file you will then have extensions on the PhotonPlayer class.
using UnityEngine;
using System.Collections;
using Hashtable = ExitGames.Client.Photon.Hashtable;
static class PhotonPlayerExtensions {
public static void SetStringProperty(this PhotonPlayer player, string propertyName, string property)
{
Hashtable propertyChanges = new Hashtable(); // using PUN's implementation of Hashtable
propertyChanges[propertyName] = property;
player.SetCustomProperties(propertyChanges); // this locally sets the score and will sync it in-game asap.
}
public static string GetStringProperty(this PhotonPlayer player, string propertyName)
{
object obj;
if (player.customProperties.TryGetValue(propertyName, out obj))
{
return (string)obj;
}
return null;
}
public static void SetIntProperty(this PhotonPlayer player, string propertyName, int amount)
{
int current = player.GetIntProperty(propertyName);
current = amount;
Hashtable propertyChanges = new Hashtable(); // using PUN's implementation of Hashtable
propertyChanges[propertyName] = current;
player.SetCustomProperties(propertyChanges); // this locally sets the score and will sync it in-game asap.
}
public static int GetIntProperty(this PhotonPlayer player, string propertyName)
{
object obj;
if (player.customProperties.TryGetValue(propertyName, out obj))
{
return (int)obj;
}
return 0;
}
}
RaycoSantana
2015-02-04 09:08:21
Thank you, that worked. :D
EDIT: I got a problem though, I used this to add the Health property to the photonplayer but when I run my "hit" function the player only loses health on the player who is attacking and not in the player who is getting hit, also after like a second the player gets its health back, which I think it means its synchronizing with the player who is getting hit who is at full health
_PhotonPlayer = PhotonNetwork.player.Get(photonView.ownerId);
....
....
....
public void HitFunc(float Damage, int AttackerID) {
if (!IsShieldOn){
LastAttackerID = AttackerID;
_PhotonPlayer.SetFloatProperty("CurHealth", (float)_PhotonPlayer.customProperties["CurHealth"] - Damage);
Renderer[] ChildrenRenderers = gameObject.GetComponentsInChildren<Renderer>();
foreach (Renderer R in ChildrenRenderers) {
StartCoroutine(ChangeColor(R));
}
}
}
_PhotonPlayer is attacker? Then update health of hit player, not attacker.
RaycoSantana
2015-02-05 12:38:30
@vadim wrote:
_PhotonPlayer is attacker? Then update health of hit player, not attacker.
err no _photonPlayer is the player who got hit. You may be confused because of the variable "int AttackerID" but that is the ID of the person who attacked this player so when the player dies the last person who attacked this player gets the score. The attacker has nothing on this actually, the ID of the attacker and the damage are carried by the projectile, the projectiles also are the ones who trigger this function.
Make sure that you update health only once and always on same client for particular player (usually owner of this player) If hit detected on client other than owner, sen RPC to owner and update health there.
Back to top