GetRunningScene() returns NULL... always!

Hello!

I want to get my current running scene but I always get NULL, I have a scene and a NotificationNode above the scene. Can this be the problem?

Game2* gameScene = dynamic_cast<Game2*>(Director::getInstance()->getRunningScene());

I even desperately checked if maybe the type does not match (since then dynamic_cast returns NULL) so I checked other options like this:

Game2_Scene1Layer* test1 = dynamic_cast<Game2_Scene1Layer*>(Director::getInstance()->getRunningScene());
    Game2_Scene1* test2 = dynamic_cast<Game2_Scene1*>(Director::getInstance()->getRunningScene());
    ISceneLoader* test3 = dynamic_cast<ISceneLoader*>(Director::getInstance()->getRunningScene());

But here I also get NULL everywhere :open_mouth: Is there a way to get the type of the current running scene to I can cast it to the correct type? :stuck_out_tongue:

Well what you have to remember is that getRunningScene will return the current scene but the scene you are trying to get i.e(Game2) this is actually a layer running on the scene.
To access this layer you will need to add a tag when creating your layer originally
Then when you have your scene do a scene->getChildByTag(1);
This will give you your custom “Scene” layer

MainMenu *layer = MainMenu::create();
layer->setTag(1);

Then getting your current layer “Scene”

CCScene* scene = CCDirector::sharedDirector()->getRunningScene();
MainMenu* mainLayer = (MainMenu*)scene->getChildByTag(1);
mainLayer->CallYourfunction();

This will give you the layer you want.

1 Like

@ChrisS91
Ok, but how do I set the layer Tag when I load the scene with ccbReader from a .ccbi file like this:

auto ccNodeLoaderLibrary = NodeLoaderLibrary::newDefaultNodeLoaderLibrary();
ccNodeLoaderLibrary->registerNodeLoader(className, loader);
auto ccbReader = new cocosbuilder::CCBReader(ccNodeLoaderLibrary);

Scene* scene =  ccbReader->createSceneWithNodeGraphFromFile(IGameManager::getInstance().getSceneName());
    
return scene;

?

I dont know about loading it from ccbReader but what you can try is instead of getting it by tag get the array of children attached to it then using the first one.
E.g just an example out of my head dont know if it will work

ccArray children = Scene->getChildren();
Game2 *MainLayer =  (Game2*)children->objectAtIndex(0);
mainLayer->runFunction();
1 Like

@ChrisS91
I also came up with the idea of children and I checked and it was “doable” :stuck_out_tongue: However this wouldn’t look nice in code because the children count of the scene was 2. The first child was the Camera* and the second child was the Game2* scene that I actually want. Both children with Tag == -1 I could iterate through the children and set them tags i.e setTag(i++); However I decided to experiment a bit with the ccbReader and in the end I’ve done something like this:

Scene* scene = Scene::create();

auto node = ccbReader->readNodeGraphFromFile(IGameManager::getInstance().getSceneName());
node->setTag(1);
scene->addChild(node);

return scene;

And then dynamic_cast fulfilled everything :stuck_out_tongue:

Thanks @ChrisS91! And hopefully someone else will need this in the future so above You have a working example :wink: