using System;
namespace D2D.Common
{
///
/// The value which have on change event, so other scripts will know
/// when value change
///
public class TrackableValue
{
public event Action Changed;
public virtual T Value
{
get
{
if (_firstGet != null && !_firstGetExecuted)
{
_value = _firstGet();
_firstGetExecuted = true;
}
return _value;
}
set
{
if (_firstGet != null && !_firstGetExecuted)
{
_value = _firstGet();
_firstGetExecuted = true;
}
if (!_value.Equals(value))
{
_value = value;
Changed?.Invoke(value);
}
}
}
private T _value;
private Func _firstGet;
// Mostly for money save to db
private bool _firstGetExecuted;
public TrackableValue(T value, Func firstGet = null)
{
_value = value;
_firstGet = firstGet;
}
}
}