How can I return from Lua created Scene?

Hey,
I set up a Lua script which is called from a regular CCControlButton within my c++ file.
I call it like this
c++

void HelloWorld::executeLua(){

    CCLuaEngine* pEngine = CCLuaEngine::defaultEngine();
    CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine);

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    CCString* pstrFileContent = CCString::createWithContentsOfFile("hello.lua");
    if (pstrFileContent)
    {
        pEngine->executeString(pstrFileContent->getCString());
    }
#else
    std::string path = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("hello.lua");
    pEngine->addSearchPath(path.substr(0, path.find_last_of("/")).c_str());
    pEngine->executeScriptFile(path.c_str());


    lua_State *L = pEngine->getLuaState();

    lua_getglobal(L, "main");
    lua_call(L, 0, 0);

#endif
}

The hello.lua file looks like this

function main()

    local visibleSize = CCDirector:sharedDirector():getVisibleSize()
    local origin = CCDirector:sharedDirector():getVisibleOrigin()

    local cclog = function(...)
        print(string.format(...))
    end


    -- create layer
    local function createLuaSceneLayer()
        local luaSceneLayer = CCLayer:create()

             local function menuCallbackBack()
                CCDirector:sharedDirector():popScene()
            end

            local size = CCDirector:sharedDirector():getWinSize();

            local bg = CCLayerColor:create(ccc4(255,255,255,255))
            luaSceneLayer:addChild(bg)



            local backButton = CCMenuItemImage:create("arrow_left.png", "arrow_left.png")
            backButton:setPosition(ccp(20, CCDirector:sharedDirector():getWinSize().height - 20) )
            backButton:registerScriptTapHandler(menuCallbackBack)


            local menu = CCMenu:createWithItem(backButton)
            menu:setPosition(ccp(0, 0))
            luaSceneLayer:addChild(menu,3)


            local textLabel = CCLabelTTF:create("I am a Label created with Lua", "Thonburi", 20)
            textLabel:setPosition(150,150)
            textLabel:setColor(ccc3(0,0,0))
            luaSceneLayer:addChild(textLabel)



        return luaSceneLayer
    end


    -- run
    local luaScene = CCScene:create()
    luaScene:addChild(createLuaSceneLayer())

    CCDirector:sharedDirector():pushScene(luaScene)
end

xpcall(main, __G__TRACKBACK__)

Everything works fine unit I try to go back. The HelloWorld scene appears, freezes and then crashes.
Can someone please help me with this? :slight_smile:

Here is an example

This scenetest is the equivalent of the one in c++.
Maybe this will help, maybe not.
Andre

Thanks.
Problem was not in the Lua file but in the way I called it.
In my demo app I had two buttons, each executing different lua scripts.
Had to use a global variable CCLuaEngine* pEngine; instead of calling

CCLuaEngine * pEngine = CCLuaEngine::defaultEngine();
CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine);

each time I press one of the buttons.

Hello! I am a beginner who learns cocos2d-x, I have encountered the same problem too. Have you resolved it yet?

Carl Zhong wrote:

Hello! I am a beginner who learns cocos2d-x, I have encountered the same problem too. Have you resolved it yet?

I answered it already