im trying to call a lua function from C++ where function is in subtable of global table. Im using lua version 5.2.* compiled from source.
Lua function
function globaltable.subtable.hello()
-- do stuff here
end
C++ code
lua_getglobal(L, "globaltable");
lua_getfield(L, -1, "subtable");
lua_getfield(L, -1, "hello");
if(!lua_isfunction(L,-1)) return;
lua_pushnumber(L, x);
lua_pushnumber(L, y);
lua_call(L, 2, 0);
However im unable to call it, i always get an error
PANIC: unprotected error in call to Lua API (attempt to index a nil value)
On line #3: lua_getfield(L, -1, "hello");
What am I missing?
Side question: I would love to know also how to call function deeper than this - like globaltable.subtable.subsubtable.hello() etc.
Thank you!
This is what im using to create the globaltable:
int lib_id;
lua_createtable(L, 0, 0);
lib_id = lua_gettop(L);
luaL_newmetatable(L, "globaltable");
lua_setmetatable(L, lib_id);
lua_setglobal(L, "globaltable");
how do i create the globaltable.subtable?