where E2 : AtomEvent
where F : AtomFunction
where CO : IGetValue
where L : IGetValue
{
///
/// Getter for retrieving the in memory runtime variable.
///
public V Variable { get => _inMemoryCopy; }
///
/// Getter for retrieving the value of the in memory runtime variable.
///
public T Value { get => _inMemoryCopy.Value; set => _inMemoryCopy.Value = value; }
[SerializeField]
[ReadOnly]
private V _inMemoryCopy;
///
/// The variable that the in memory copy will be based on when created at runtime.
///
[SerializeField]
private V _base = null;
///
/// If assigned the in memory copy variable will be added to the collection on Start using the gameObject's instance id as key. The value will also be removed from the collection OnDestroy.
///
[SerializeField]
private CO _syncToCollection = default(CO);
///
/// If assigned the in memory copy variable will be added to the list on Start. The value will also be removed from the list OnDestroy.
///
[SerializeField]
private L _syncToList = default(L);
private void OnEnable()
{
Assert.IsNotNull(_base);
_inMemoryCopy = Instantiate(_base);
if (_base.Changed != null)
{
_inMemoryCopy.Changed = Instantiate(_base.Changed);
}
if (_base.ChangedWithHistory != null)
{
_inMemoryCopy.ChangedWithHistory = Instantiate(_base.ChangedWithHistory);
}
}
void Start()
{
// Adding to the collection on Start instead of OnEnable because of timing issues that otherwise occurs when listeners register themselves OnEnable.
// This is an issue when a Game Object has a Variable Instancer attached to it when the scene starts and at the same time their is an AtomBaseListener listening to the associated Added event to a Collection.
if (_syncToCollection != null)
{
_syncToCollection.GetValue().Add(GetInstanceID().ToString(), _inMemoryCopy);
}
if (_syncToList != null)
{
_syncToList.GetValue().Add(_inMemoryCopy);
}
}
private void OnDestroy()
{
if (_syncToCollection != null)
{
_syncToCollection.GetValue().Remove(GetInstanceID().ToString());
}
if (_syncToList != null)
{
_syncToList.GetValue().Remove(_inMemoryCopy);
}
}
///
/// Get event by type.
///
///
/// The event.
public E GetEvent() where E : AtomEventBase
{
if (typeof(E) == typeof(E1))
return (_inMemoryCopy.Changed as E);
if (typeof(E) == typeof(E2))
return (_inMemoryCopy.ChangedWithHistory as E);
throw new Exception($"Event type {typeof(E)} not supported! Use {typeof(E1)} or {typeof(E2)}.");
}
///
/// Set event by type.
///
/// The new event value.
///
public void SetEvent(E e) where E : AtomEventBase
{
if (typeof(E) == typeof(E1))
{
_inMemoryCopy.Changed = (e as E1);
return;
}
if (typeof(E) == typeof(E2))
{
_inMemoryCopy.ChangedWithHistory = (e as E2);
return;
}
throw new Exception($"Event type {typeof(E)} not supported! Use {typeof(E1)} or {typeof(E2)}.");
}
}
}