[C++] EventBus - aka CCNotificationCenter

[C++] EventBus - aka CCNotificationCenter
0

Hello there!

I would like to share with you guys my library which I’m developing since cocos2d-x v2 (ofc not all the time :D)
I wan’t to give it time to grow up and i needed to test it in real world projects.

I would like to hear you comments :slight_smile: Thx!

Also I can mention that my company is looking for cocos2d-x developers :wink:

Recent update of syntax (example):

  1. Store bus
// store it in controller / singleton / std::sharted_ptr whenever you want
Dexode::EventBus bus;
  1. Define events
namespace Event // optional namespace
{
	struct Gold
	{
		int goldReceived = 0;
	};
	
	struct OK {}; // Simple event when user press "OK" button
}
  1. Subscribe
// ...
bus.listen<Event::Gold>
			([](const auto& event) // listen with lambda
			{
				std::cout << "I received gold: " << event.goldReceived << "!" << std::endl;
			});
			
HudLayer* hudLayer;
// Hud layer will receive info about gold
bus.listen<Event::Gold>(std::bind(&HudLayer::onGoldReceived,hudLayer,std::placeholders::_1));
  1. Notify
//Inform listeners about event
bus.notify(Event::Gold{12}); // 1 way

bus.notify<Event::Gold>({12}); // 2 way

Event::Gold myGold{12};
bus.notify(myGold); // 3 way

Lambda listener:

struct SampleEvent {};
Dexode::EventBus bus;
//...
int token = bus.listen<SampleEvent>([](const SampleEvent& event) // register listener
{
});

//If we want unlisten exact listener we can use token for it
bus.unlistenAll(token);

Listener is identified by token. Token is returned from EventBus::listen methods.
We can register multiple listeners on one token:

Dexode::EventBus bus;
struct SampleEvent {};
//...
int token = bus.listen<SimpleEvent>([](const auto& event) // register listener
{
});

bus.listen<SimpleEvent>(token, [](const auto& event) // another listener
{
});

bus.unlistenAll(token);//Now those two lambdas will be removed from listeners

If you don’t want to handle manually with token you can use EventCollector class.
It is useful when we want to have multiple listeners in one class. So above example could look like this:

Dexode::EventBus bus;
struct SampleEvent {};
Dexode::EventCollector collector{&bus};
//...
collector.listen<SampleEvent>([](const SampleEvent& event) // register listener
{
});

collector.listen<SampleEvent>([](const SampleEvent& event) // another listener
{
});

collector.unlistenAll();//Now those two lambdas will be removed from listeners

Or as component of class:

class Example
{
public:
	Example(const std::shared_ptr<Dexode::EventBus>& bus)
			: _collector{bus}
	{
		_collector.listen<SimpleEvent>(std::bind(&Example::onEvent1, this, std::placeholders::_1));
		_collector.listen<OtherEvent>(std::bind(&Example::onEvent2, this, std::placeholders::_1));
	}

	void onEvent1(const SimpleEvent& event)
	{
	}

	void onEvent2(const OtherEvent& event)
	{
	}

private:
	Dexode::EventCollector _collector;// use RAII
};

//EventCollector sample
std::shared_ptr<Dexode::EventBus> bus;
Example ex{bus};
//...
bus.notify<int>("event1", 2);
2 Likes

Hello there I just updated EventBus (2.4.0) and now it supports async events also now it is written in more mordern Cmake :slight_smile:

Checkout: https://github.com/gelldur/EventBus/blob/master/lib/include/eventbus/AsyncEventBus.h

It would be nice to have some opinions from “fresh developers” and more advanced ones.
My main questions: (you don’t have to answer if you don’t want to)

  • How it generally look ? Is it OK ? ( I mean your first impression, main README)
  • Is it easy to use ?
  • Does documentation explaining what it needs (in clear way), especially if you didn’t previously use event bus pattern.
  • What is missing for you ?
  • Why you don’t want to use it ? (eg. license, minimum C++11, etc.)
  • Are you using other implementation, if yes pls mention :slight_smile:

https://github.com/gelldur/EventBus

1 Like

I think cocos2d-x v3 already has these features, doesn’t it?

Yeap but different approach. Also this doesn’t have any dependencies so maybe someone would like to use it in raw game logic layer instead of mixing game logic with cocos2d-x. In past I was working on project where we move game from one game engine to cocos2d-x. It was nice that pure game logic doesn’t have to many dependencies with engine :slight_smile:

1 Like

true! nice work!

1 Like