You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
CrowdControl/Assets/3rd/D2D_Scripts/Gameplay/TrackableValue.cs

53 lines
1.2 KiB
C#

3 months ago
using System;
namespace D2D.Common
{
/// <summary>
/// The value which have on change event, so other scripts will know
/// when value change
/// </summary>
public class TrackableValue<T>
{
public event Action<T> 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<T> _firstGet;
// Mostly for money save to db
private bool _firstGetExecuted;
public TrackableValue(T value, Func<T> firstGet = null)
{
_value = value;
_firstGet = firstGet;
}
}
}