Messaging - simple-entertainment/simplicity GitHub Wiki

Communication between the various different engines and scripts running in simplicity is performed using a mediated messaging system.

Sending Messages

Sending messages is very easy. You just need to provide a subject and a message. The subject determines who will receive the message. The message can be a pointer to any relevant information you want to send.

Entity* badGuyEntity = // Some entity...
Messages::send(MySubjects::BAD_GUY_KILLED, badGuyEntity);

Receiving Messages

Receiving messages is a little trickier, but not much. You need to create a function that matches the Recipient function signature e.g.

void onBadGuyKilled(const void* message)
{
    const Entity* badGuyEntity = static_cast<const Entity*>(message);
    // Do stuff...
}

It returns void and takes a const void* parameter - pretty simple. You have to cast the message parameter so be sure to always send the same type whenever you send a message with a particular subject.

Next, you need to register your function as a recipient for a particular subject:

Messages::registerRecipient(MySubjects::BAD_GUY_KILLED, onBadGuyKilled);
// Hint: You can use member functions by utilizing std::bind.

If you're getting sick of these silly messages, just deregister in the same way:

Messages::deregisterRecipient(MySubjects::BAD_GUY_KILLED, onBadGuyKilled);