Is there a way to dynamically check for variable change ?

Hi guys .

I know how to check if a variable changes . One method is like this

        public class SomeClass
        {

            private Rect someRect;
            private Rect T;

            void OnEnable()
            {
                if (someRect == null)
                {
                } someRect = new Rect(0, 0, 100, 100);
            }

            bool RectChanged()
            {
                bool result = false;
                if (someRect != T)
                {
                    result = true;
                    T = someRect;
                }
                return result;
            }
            void OnGUI()
            {
if(RectChanged())
{
Debug.Log("ChangeMade");
}
            }
        }

However this method is really crude .

I need to be able to dynamically check for variable changes without having to do that kind o coding
I will give you an example. this wont actually work well , it is just an example of what i’m trying to do.

if i were to create instances of this class in non static classes and make the method and variable non static then it would work well. However I do have class that I need to keep static which need to use the changed function.

        public  class Changed
        {

            private  static object obj;

            public static bool changed(object T)
            {
                bool result = false;
                if(obj != T)
                {
                    obj = T;
                    result = true;
                }
                return result;
            }
        }

And use it this way

    public class TestClass
    {
        private static Rect someRect = new Rect(0, 0, 100, 100);

        public static void TestFunction()
        {

            if (Changed.changed(someRect))
            {
                Debug.Log("change made");
            }

        }

    }

I figure that there is a high change of this being not possible but if you know of a way to do this then please share.

I think you might be looking for events.

public class ClassA : MonoBehaviour {
public delegate void ChangeEvent (int numberOfApples); //I do declare!
public static event ChangeEvent changeEvent;  // create an event variable 
public int appleCount = 0;
// trigger some change and call the event for all subscribers

public void  MoreApples(){
  appleCount ++;
  if(changeEvent!=null) // checking if anyone is on the other line.
        changeEvent(appleCount);
}

}


// Then you subscribe to the even from any class. and if the event is triggered (changed) then your other class will be notified.

public class ClassB : MonoBehaviour{
  void OnEnable(){
     ClassA.changeEvent += ApplesChanged; // subscribing to the event. 
  }

  void ApplesChanged(int newAppleCount){
     Debug.log("newAppleCount now = " + newAppleCount);  // This will trigger anytime you call MoreApples() on ClassA
  }
}
}

WUCC, check for errors.

If you’re not feeling events – A “wrapper” class that uses properties might be what you’re looking for.

The class looks like:

public class ChangeTrackingWrapper<T>
    {
        private T _value;
        private bool _hasChanged;

        public ChangeTrackingWrapper()
        {
        }

        public ChangeTrackingWrapper(T initialValue)
        {
            _value = initialValue;
        }

        /// <summary>
        /// Gets or sets the wrapped value.
        /// </summary>
        public T Value
        {
            get { return _value; }
            set
            {
                if (Equals(_value, value)) return;
                _value = value;
                _hasChanged = true;
            }
        }

        /// <summary>
        /// Gets a flag indicating whether the wrapped value has changed.
        /// </summary>
        public bool HasChanged
        {
            get { return _hasChanged; }
        }

        /// <summary>
        /// Resets the changed flag.
        /// </summary>
        public void ResetChangedFlag()
        {
            _hasChanged = false;
        }
    }

Notes:

I think this generalizes your original example.

The magic happens in the Value setter. If the value isn’t different, the setter doesn’t do anything. But if the value is different, it will set the HasChanged flag to true.

You can initialize the class with an initial value, or let it be the default value (which is default(T)). It uses the generic for type safety, and isn’t static, so you can use it in place of any field in a class that you’d like (see usage below). I wasn’t sure if or when you wanted to “reset” that your object has changed, but I included a ResetChangedFlag() method on the class.

Usage:

public class SampleClass
    {
        private ChangeTrackingWrapper<Rect> _myChangeTrackingWrapper;

        void OnEnable()
        {
            if (_myChangeTrackingWrapper == null)
            {
                _myChangeTrackingWrapper = new ChangeTrackingWrapper<Rect>(new Rect(0, 0, 100, 100));
            }
        }

        void OnGUI()
        {
            if (_myChangeTrackingWrapper.HasChanged)
            {
                Debug.Log("ChangeMade");
                _myChangeTrackingWrapper.ResetChangedFlag();
            }

        }
    }

Static Usage

public class StaticSampleClass
    {
        public static ChangeTrackingWrapper<object> MyStaticChangeTrackingWrapper = new ChangeTrackingWrapper<object>();

        public void MyMethod()
        {
            StaticSampleClass.MyStaticChangeTrackingWrapper.Value = new object();
            var hasChangedAsString = StaticSampleClass.MyStaticChangeTrackingWrapper.HasChanged.ToString();
            Debug.Log(hasChangedAsString); //Should log "true"
        }
    }