using System.Collections.Generic; using Godot; namespace Babushka.scripts.CSharp.Low_Code.Variables; /// /// A value wrapper resource used to store state values and notify ingame scripts. /// [GlobalClass] public partial class VariableResource : Resource { /// /// Log into console when this event resource is adding or removing listeners, and when it's raised. /// [Export] private bool _showLog; /// /// Public property that manages the access to the payload. /// Triggers the ValueChange-function when set to a new value. /// [Export] public Variant Payload { get { return _payload; } set { if (!_payload.Equals(value)) { _lastPayload = _payload; _payload = value; ValueChangeHandler(); } } } private Variant _payload; private Variant _lastPayload; private List _varListeners = new (); /// /// Adds an EventListener to the calling list for this event. /// /// public void RegisterListener(VariableListener listener) { if(_showLog) GD.Print("Registering listener " + listener); _varListeners.Add(listener); } /// /// Removes an Eventlistener from the calling list for this event. /// /// public void UnregisterListener(VariableListener listener) { if(_showLog) GD.Print("Unregistering listener " + listener); _varListeners.Remove(listener); } /// /// Called when the Payload value changed. /// public void ValueChangeHandler() { if(_showLog) GD.Print($"Event payload changed from {_lastPayload} to {_payload} on event resource: " + ResourcePath.GetFile().TrimSuffix(".tres")); foreach (var eventListener in _varListeners) { eventListener.EventPayloadChanged(_payload, _lastPayload); } } }