-- helloLua.lua文件
myName = "beauty girl"
helloTable = {name = "mutou", IQ = 125}
function helloAdd(num1, num2)
return (num1 + num2)
end;
/* C++調用lua的函數 */
void HelloLua::demo3() {
lua_State* pL = lua_open();
luaopen_base(pL);
/* 執行腳本 */
luaL_dofile(pL, "helloLua.lua");
/* 把helloAdd函數對象放到棧中 */
lua_getglobal(pL, "helloAdd");
/* 把函數所需要的參數入棧 */
lua_pushnumber(pL, 10);
lua_pushnumber(pL, 5);
/*
執行函數,第一個參數表示函數的參數個數,第二個參數表示函數返回值個數 ,
Lua會先去堆棧取出參數,然后再取出函數對象,開始執行函數
*/
lua_call(pL, 2, 1);
int iResult = lua_tonumber(pL, -1);
CCLOG("iResult = %d", iResult);
}