Is this safe to modify Director like this?

Is this safe to modify Director like this?
0

Hi, I’m using custom eventlistener Director::EVENT_RESET to do some clean up, and I noticed that Director is holding on to the scenes in _scenesStack. This causes the running scene to not be cleared until the next line.

So I modified to move the clearing other held stack before the event listener is fired. Is that a safe modification? I don’t quite understand what the notificationNode is used for.

void Director::reset()
{
#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
    auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
    
    if (_runningScene)
    {
#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
        if (sEngine)
        {
            sEngine->releaseScriptObject(this, _runningScene);
        }
#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
        _runningScene->onExit();
        _runningScene->cleanup();
        _runningScene->release();
    }
    
    _runningScene = nullptr;
    _nextScene = nullptr;

	
//MODIFIED I move this here, is it safe?
// This releases the scene when EVENT_RESET is fired
	while (!_scenesStack.empty())
        _scenesStack.popBack();

    if (_eventDispatcher)
        _eventDispatcher->dispatchEvent(_eventResetDirector);
    
    // cleanup scheduler
    getScheduler()->unscheduleAll();
    
    // Remove all events
    if (_eventDispatcher)
        _eventDispatcher->removeAllEventListeners();
    
    if(_notificationNode)
    {
        _notificationNode->onExit();
        _notificationNode->cleanup();
        _notificationNode->release();
    }
    
    _notificationNode = nullptr;
    
    // remove all objects, but don't release it.
    // runWithScene might be executed after 'end'.
#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
    if (sEngine)
    {
        for (const auto &scene : _scenesStack)
        {
            if (scene)
                sEngine->releaseScriptObject(this, scene);
        }
    }
#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS

// Director still holds the current running scene here
    //while (!_scenesStack.empty())
    //    _scenesStack.popBack();

Notification Node is a special Node that responds to every frame always. Iirc.

Yup, I see it in drawScene, but I don’t really see anywhere in the engine setting it, so where/what is it for?

It’s in Director. I see people use it for alerts, responding to events, and lots of other purposes.

@zhangxm what’s the real use of NotificationNode?

@overcocos if you don’t use NotificationNode, then you can ignore it. It is just a place that can do something after rendering a scene.