#Modifying a value when another one gets modified

6 messages · Page 1 of 1 (latest)

undone glacier
#

im trying to modify a Value every time another one gets modified, So I thought of using a getter setter for C#, I didn't know how to use it so I just used the default one, I'm not sure how to execute it though, any ideas?

undone glacier
turbid mural
#

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)

fossil pewter
#

@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.

undone glacier
#

Thank you so much I didn't realise there was a simple solution

#

even though I took a month