Does anyone know what is the difference between CC_CALLBACK_0 and CC_CALLBACK_1 or CC_CALLBACK_2?

As mentioned in subject

The number means how many parameters the callback function has.
CC_CALLBACK_0 means callback function with no input parameter, such as MyClass::doSomething()
CC_CALLBACK_1 means thereā€™s only 1 input parameter, e.g., MyClass::doSomething(int a)

2 Likes

Ah, thanks :slight_smile:

Zhe Wang wrote:

ā€¦

Why wonā€™t use use variadic templates instead of those verbose macros; just to make everyoneā€™s life easier? I mean something like this: https://gist.github.com/kuhar/8163865

Hey, please go over this with me.

For this method:

void checkGameOver(const TurnInfo turnInfo);

If I try to do CC_CALLBACK_1:

CallFunc::create(CC_CALLBACK_1(GameScene::checkGameOver, this, turnInfo));

I get a compilation error:
No matching function for call to ā€˜createā€™.

But if I do CC_CALLBACK_0:

CallFunc::create(CC_CALLBACK_0(GameScene::checkGameOver, this, turnInfo))

Even though checkGameOver takes 1 parameter, the error goes away and it works.

Clearly the explanation:

Doesnā€™t quite make sense, am I not understanding something very basic here? Every version of cocos (starting at 3.0) that Iā€™ve used has behaved this way.

Just use CC_CALLBACK_0. You can pass it as many parameters as you want.
CC_CALLBACK_1 and CC_CALLBACK_2 have placeholders, so after you define your CC_CALLBACK_X function, you can pass variables to that function.

1 Like

CC_CALLBACK_1 just wraps std::bind and adds a std::placeholder::_1 to the bind. You arenā€™t putting in a placeholder but an actual value.

As the previous poster has pointed out, you can use CC_CALLBACK_0 as it has a VA_ARGS parameter in the macro that just places the arguments after ā€œthisā€ as bound parameters:

    // new callbacks based on C++11
#define CC_CALLBACK_0(__selector__,__target__, ...) std::bind(&__selector__,__target__, ##__VA_ARGS__)
#define CC_CALLBACK_1(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, ##__VA_ARGS__)
#define CC_CALLBACK_2(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, ##__VA_ARGS__)
#define CC_CALLBACK_3(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, ##__VA_ARGS__)
1 Like

Thanks, I use CC_CALLBACK_0 all the time, and have looked at the macro definitions. Just thought the explanation originally given was confusing.