MaZy
MaZy

Reputation: 91

It is possible to use Attributes to remember old value of a variable?

Like my question above I would like know it.

Want I want to do is to make my life easier if I am working with values where I need to check changes. And if there is many many variables so I have to make always duplicated variables like:

health = 100;
oldHealth = health; // this should be in the attribute class for example

this makes me crazy. So I tried to make attributes but I don't know really how I would know when a variables is changed.. I guess I need to loop all the time myself or it is there any events? Also need to read the current value before it would get changed.

Upvotes: 0

Views: 336

Answers (1)

Vampirasu
Vampirasu

Reputation: 202

when you only need to know if there is a change and in the case you need the "old" value. Just use the standard property implementation:

int _health;
int Health 
{
    get {return _health;
}
set {
    // check your "old" value here and do some stuff
    _health = value; // than asign the new value thats inside the keyword "value"
}

Upvotes: 3

Related Questions