EDIT: Solved. It wasn’t anything about Cocos2d-x / events getting lost, but fault of a bug that I introduced in my own codebase. Sorry about that
And thank you anyway, great Cocos2d-x community! 
Hello,
I have a problem with Cocos2d-x (3.9) custom events (cocos2d::EventCustom and cocos2d::EventListenerCustom). In the game I am currently working on, I’ve just implemented a missions feature: Get 50 gems, defeat 10 enemies, etc. So I thought the best approach to this was to send custom events each time the player collects a gem or defeats an enemy, and a Mission class will listen to all these custom events and keep a counter to determine when the mission is accomplished.
For example, let’s say the mission is “get 20 gems” (I’ll adapt the code to be more readable and with hardcoded values instead of generic so it is more readable). First, I need to register a EventListenerCustom of type "gemCollected":
// Somewhere in Missions.cpp file
// Register a EventListenerCustom to track gemCollected events
_gemCollectedListener = cocos2d::EventListenerCustom::create("gemCollected", [this](cocos2d::EventCustom* event) {
_collectedGems++;
CCLOG("gemCollected listener callback n. %d", _collectedGems);
// Determine if the mission has been completed
if (_collectedGems >= 20)
CCLOG("Mission complete!");
});
cocos2d::Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_gemCollectedListener, 1);
Then I dispatch a "gemCollected" custom event each time the player collects a gem:
// Somewhere in the game, where the collision detection between the gem and the player is checked
cocos2d::Director::getInstance()->getEventDispatcher()->dispatchCustomEvent("gemCollected");
The problem is that if I play a game in which I collect 20 gems (the mission requires 20 to be completed), the callback above should have been called each time I collected a gem and have an output like this:
gemCollected listener callback n. 1
gemCollected listener callback n. 2
gemCollected listener callback n. 3
...
gemCollected listener callback n. 20
Mission complete!
But instead the callback is called, say, 17 times. Or 15. Or 19. And that feels weird because when the Game Over screen appears you can see the amount of gems you got, and if you have a mission about collecting gems it also tells you how many gems you collected out of a total to complete the mission, and there you clearly see that both values doesn’t match.
It’s like some events are getting lost, or not being dispatched / listened. And I can’t see a pattern. I also tried to dive into the event dispatcher code, but I don’t know where to look at.
Any clue? Thank you in advance!




