Cocos2d-x CC_CALLBACK_X causes lint error in Android Studio

Cocos2d-x CC_CALLBACK_X causes lint error in Android Studio
0

So I have returned to playing with cocos2d-x after a few months and created a new project (I am using Android Studio 3.0.1 and cocos2d-x-3.17). To be clear from the start, the project builds successfully and runs successfully. By that I mean that I navigate to de desired Scene and I am getting successful prints of touch events etc. The problem is that I am seeing a lot of annoying red error lint underlines when I use CC_CALLBACKS binding macros (with any of the 4 available).

For instance:

auto playItem = MenuItemImage::create("assets/res/MainMenuScene/img_button_play.png",
                                      "assets/res/MainMenuScene/img_button_play.png",
                                      CC_CALLBACK_1(MainMenuScene::goToGameScene, this));

Where this represents the MainMenuScene.cpp and the MainMenuScene::goToGameScene is declared in the MainMenuScene.h:

void goToGameScene(cocos2d::Ref *pSender);

and implemented as follows:

void MainMenuScene::goToGameScene(cocos2d::Ref *pSender) {
    auto scene = GameScene::createScene();
    Director::getInstance()->replaceScene(TransitionFade::create(1.0, scene));
}

The lint error I am getting is No matching function on MenuItemImage::create(...

This was the most basic example to share, but I am getting other errors with touch event listeners while assigning onTouch listeners via CC_CALLBACKs

If I replace the CC_CALLBACK macro with a lambda the lint error is gone. The question finally is, Has anyone experienced this in the past and knows how to fix this?

I’m pretty sure the lint errors can be suppressed somehow but I don’t think that is the goal here, but rather to see why Android Studio is complaining and address that issue.

Yeah, the linter/checker is buggy (based on CLion, afaict).
There’s probably a way to disable specific “inspections” or maybe just disable the linter altogether?

See:

https://youtrack.jetbrains.com/issue/CPP-10105#u=1498146400928
I think this is related to your specific issue since CC_CALLBACK uses std::bind.

https://youtrack.jetbrains.com/issue/CPP-5758#u=1454575544687

using std::string;
using std::vector;
vector<string> s; // Inspector false issue

Thanks for the effort and quick reply. Sadly as is, looks like a lot of people have this issue and just “deal” with it.

Same problem facing. Still not solution found?

@slackmoehrle I was talking about this.

OS: Win 10 64 Bit
Cocos2D-X: 3.17
NDK: r16b

Menu.h
void toggleSound(Ref* sender, Widget::TouchEventType eventType);

Menu.cpp

auto soundButton = Button::create(soundMenuImage->getCString(), soundMenuPressedImage->getCString(), "", Widget::TextureResType::PLIST);
soundButton->setScale(scaleFactor);
soundButton->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2);
this->addChild(soundButton, 1);
soundButton->addTouchEventListener(CC_CALLBACK_2(Menu::toggleSound, this)); // This line is showing red underline.

Error:

Error after macro substitution: Parameter type mismatch: Class ‘__bind<void (Menu::*)(Ref *,Widget::TouchEventType), Menu *, const placeholders::__ph<1> &, const placeholders::__ph<2> &>’ is not compatible with class ‘const Widget::ccWidgetTouchCallback’

Though the last line is showing error, the code is getting compiled and running successfully.

Just ignore it. Its false error. OR you can use Lambda Function

If it is compiling and working as expected then perhaps Android has a linting issue with it.

1 Like

Ok. Thanks.