5
我对Lua很新。 我一直在寻找一些示例代码来说明如何从C++调用Lua函数,但示例代码使用5.1,并且我试图让它与5.2一起工作。从C++调用Lua 5.2函数
这里是有问题的代码示例有我的评语:
lua_State *luaState = luaL_newstate();
luaopen_io(luaState);
luaL_loadfile(luaState, "myLuaScript.lua");
lua_pcall(luaState, 0, LUA_MULTRET, 0);
//the code below needs to be rewritten i suppose
lua_pushstring(luaState, "myLuaFunction");
//the line of code below does not work in 5.2
lua_gettable(luaState, LUA_GLOBALSINDEX);
lua_pcall(luaState, 0, 0, 0);
我已经在5.2参考曼努埃尔阅读(http://www.lua.org/manual/5.2/manual.html#8.3)一个需要从注册表(而不是lua_gettable获得全球环境上面的声明),但我无法确定我需要做出哪些更改才能实现此功能。我试过,例如:
lua_pushglobaltable(luaState);
lua_pushstring(luaState, "myLuaFunction");
lua_gettable(luaState, -2);
lua_pcall(luaState, 0, 0, 0);
另请参阅http://stackoverflow.com/questions/11093189/lua-updating-from-5-1-lua-globalsindex-problems – lhf 2013-03-05 13:08:56