Signals is a lightweight event-driven communication system that enables decoupled communication between different parts of your application. It allows you to define custom signals and subscribe to them with observers, making it easy to broadcast and receive messages without direct dependencies.
To subscribe an observer to a specific signal, use the Signals.Subscribe method. Provide the signal name and the observer that implements the IObserver<T> interface, where T is the type of signal data.
Signals.Subscribe("SignalName", new EventObserver<int>(OnSignalReceived));To unsubscribe an observer from a specific signal, use the Signals.Unsubscribe method. Provide the signal name and the observer that was previously subscribed.
Signals.Unsubscribe("SignalName", observer);To unsubscribe all observers from a specific signal, use the Signals.UnsubscribeAll method. Provide the signal name.
Signals.UnsubscribeAll("SignalName");To trigger a specific signal and notify all subscribed observers, use the Signals.Trigger method. Provide the signal name and the signal data.
Signals.Trigger("SignalName", signalData);To create a custom observer, implement the IObserver<T> interface, where T is the type of signal data.
public class CustomObserver : IObserver<int>
{
public void OnNotify(int signalData)
{
// Handle the signal data
}
}Here's an example of how to use the Signals class in your application:
// Subscribe an observer to a signal
Signals.Subscribe("PlayerDied", new EventObserver<PlayerData>(OnPlayerDied));
// Trigger the signal with signal data
PlayerData playerData = new PlayerData();
Signals.Trigger("PlayerDied", playerData);
// Unsubscribe all observers from the signal
Signals.UnsubscribeAll("PlayerDied");
// Custom observer implementation
void OnPlayerDied(PlayerData playerData)
{
// Handle the player death event
}This project is licensed under the MIT License.