I am very new to Lua. I was looking for sample code to call a Lua function from C ++, but the sample code uses 5.1, and I'm trying to get this to work with 5.2.
Here is an example of the code in question with my comments:
lua_State *luaState = luaL_newstate();
luaopen_io(luaState);
luaL_loadfile(luaState, "myLuaScript.lua");
lua_pcall(luaState, 0, LUA_MULTRET, 0);
lua_pushstring(luaState, "myLuaFunction");
lua_gettable(luaState, LUA_GLOBALSINDEX);
lua_pcall(luaState, 0, 0, 0);
I read in the manuel 5.2 reference ( http://www.lua.org/manual/5.2/manual.html#8.3 ) that you need to get the global environment from the registry (instead of the lua_gettable expression above), but I canโt decide what changes I need to do to make this work. I tried for example:
lua_pushglobaltable(luaState);
lua_pushstring(luaState, "myLuaFunction");
lua_gettable(luaState, -2);
lua_pcall(luaState, 0, 0, 0);
source
share