I'm currently learning how to make Lua work with C++ and i stumbled upon this problem. Here is my little app that I'm currently using:
#include <lua.5.2.3\src\lua.hpp>
#include <iostream>
#include <string>
int pluacall(lua_State *L){
std::cout << "Called from inside Lua." << std::endl;
return 0;
}
int main(){
std::string x;
lua_State *L = luaL_newstate();
luaL_openlibs(L);
luaL_Reg _funcs[] = { { "CppFunc", pluacall }, {} };
if (luaL_dofile(L, "test.lua")){
std::cout << "Error: " << lua_tostring(L, -1) << std::endl;
lua_pop(L, 1);
}
luaL_setfuncs(L, _funcs, 0);
lua_getglobal(L, "sup");
if (lua_pcall(L, 0, 0, 0)){
std::cout << "Error: " << lua_tostring(L, -1) << std::endl;
lua_pop(L, 1);
}
std::cout << "Test";
std::getline(std::cin, x);
lua_close(L);
return 0;
}
Everything worked flawlessly until I added the luaL_setfuncs(...)
.
Now the app crashes with the following error:
PANIC: Unprotected error in call to Lua API (attempt to index a nil value)
I'm gonna be completely honest I have absolutely no idea why it doesn't work or what this error even means (google wasn't my friend today). Any ideas?
It's probably also worth mentioning that I do not link the Lua library with my app, I compile them together (all Lua source added to project)
Edit: here's the Lua script I'm testing it with
function sup()
io.write("Hey.\n")
CppFunc()
return 1
end