#Modifying a value when another one gets modified
6 messages · Page 1 of 1 (latest)
(Texture Progress bar is a child of Player)
Why not use an if statement before the function you want to use, checking if the new value is different from the previous one? You could create it using 2 variables, a new one in the ready, which would be modified every time you call the function and another that will simply save the previous information, I don't know if I explain myself (google translator)
@undone glacier A property does not hold a value by itself. You can think of it as defining a GetValue and a SetValue method, just that it is treated like a regular field. What you have right now is essentially just this pair of functions:
public int GetHP()
{
//this will lead to a stack overflow
return GetHP();
}
public void SetHP(int val)
{
//print stuff
Value = PlayerHP;
}
If you just want to execute some code whenever HP is set, you need to keep a field called something like _hp to hold the data and a property HP with a getter and setter that return or modify _hp.
However, in this situation I would highly advise you just make Player aware of its HealthBar and directly update the value with a public method on HealthBar whenever player gets hurt/healed. That way HealthBar doesn't even need to know about Player.