Event-driven programming

Event driven programming is when the application reacts to actions rather than polling. Events are used frequently throughout Chilli Source and come in 2 main flavours.

Single Listener Events (Delegates)

Some events only allow one interested party to subscribe; generally by setting a callback delegate (an std::function).

httpRequestSystem->MakeGetRequest(“www.myurl.com”, [](const HttpRequest* in_request, const HttpResponse& in_response))
{
});

In the above example the delegate is called when the http request completes and thereafter is never notified. Usually single listener events are short lived.

Multiple Listener Events (Events)

For events that are more persistent (i.e. fire more often over a longer period of time) and that are of interest to multiple parties Chilli Source has Connectable Events:

m_connection = entity->GetTransform().GetTransformChangedEvent().OpenConnection([]()
{ 
});

Listeners subscribe to events by opening a connection. An event can have multiple connections opened and therefore multiple listeners. Whenever the event occurs then it is broadcast to all open connections.

The nature of these events is that they are often long lived and therefore use scoping as a safety mechanism to prevent connections remaining open to dead objects. In the above example the m_connection is a member variable that holds the connection open. Once the listener is destroyed and the m_connection goes out of scope then the connection is closed automatically.

CSCore::EventConnectionUPtr m_connection;

Now if you come across either event type in Chilli Source you should understand how to use them.