System Events - NocturnalWisp/Tynted-Engine GitHub Wiki
System events are a huge functional use in the game. Often you would find yourself in a predicament where one system needs to send a message to another system. With events, you can directly create, or subscribe to them resulting in automatic data transfer between systems. Events can sometimes be excessive, so be frugal with them.
To create events in a system, you first need to access the override function, and then actually create the events using the ECSManager class. You should save the resulting TyntedEvent objects for later when you actually invoke them.
//override this function in your system
public override void CreateEvents()
{
//Create whatever events you require
TyntedEvent event = ECSManager.CreateEvent(eventName);
//You can also create events with one or two arguments
TyntedEvent<object> event1arg = ECSManager.CreateEvent1Arg(eventName1Arg);
}
You also need to override another function for subscribing. Within this function you actually need to apply a method call via a lambda or delegate expression. This is placed in the system you can to receive events when it is called. The simplest way is displayed below:
public override void SubscribeEvents()
{
ECSManager.SubscribeEvent(eventName, delegate { EventRun(); });
}
private void EventRun()
{
//Do Something when the event is called.
}
To actually call the event from the system that created it, you need to do something that is called "Invoke." This is a simple function, and will call each and every subscriber no matter where they are. Place this whenever you want to call the event:
event.Invoke();
//In the case of having parameters
event.Invoke(param1);
Overall, events are incredibly useful. One thing to keep in mind is that they are easy to overuse, and often results in confusing code. Just make sure to be frugal with them, and make sure they are named well.