This repository has been archived by the owner on Jun 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Systems Communication
Maxime Cauvin edited this page Dec 12, 2017
·
1 revision
Here's an example of what a callback function should look like:
/**
* By convention, an event callback method is declared as a static method in the namespace of the System it belongs to.
* It takes a reference to an Event struct
*/
namespace nx {
class XxxSystem : public SystemTpl
{
public:
static void event_xxx(const nx::Event&);
};
}
void nx::XxxSystem::event_xxx(const nx::Event& e)
{
auto& engine = nx::Engine::Instance();
// We use the getSystemByName method to get a shared_ptr on the SystemTpl* instance of our choice.
// Then we cast it into the system of our choice
auto self = nx::XxxSystem::cast(engine.getSystemByName("xxx"));
// If the cast fails, our self variable is set to nullptr
if (!self) return;
// We can now use public member functions of the System
self->getName();
// As well as the public functions of the engine.
engine.ping();
// Finally we obviously also have access to the name and the data of the Event
nx::Log::inform(e.data);
}
How to create an EventLink?
uint32_t link_uid = system.connect("MY_CUSTOM_EVENT", nx::XxxSystem::event_xxx);
/**
* Where:
* "system" is an instance to a system class inherited from SystemTpl
* "MY_CUSTOM_EVENT" is a string which represent the name (akka the "key") of your Event
* "nx::XxxSystem::event_xxx" a callback method (see the previous example for more informations)
* "link_uid" is the unique ID for the created link, it's relevant to store it if you want to destroy your link later
*/
How to destroy an EventLink?
bool ret = system.disconnect(link_uid);
/**
* Where:
* "system" is an instance to a system class inherited from SystemTpl
* "link_uid" is the unique ID of a link which you get when you create an EventLink through the connect method
* "ret" is set to true if the EventLink has been successfully destroyed, false otherway
*/
Here's an example of "How to emit Events" thanks to the Engine.
engine.emit("EVENT_KEY", "EVENT_DATA");
This way, the event will be propaged to all registered EventLinks, in all systems.