This is not about how to call a lua function from C++. I can already do that successfully. This is about the lua function "loadfile" no longer working when used in a function called by C++.
I'm trying to load a lua file into another lua file so that I can call its functions. If I move test() from functions.lua to game.lua, mystring receives "test" just fine.
However, when I add the init() function to the game.lua file and move test() to the functions.lua file:
- loadfile no longer seems to work, producing "error in error handling"
- calling test() doesn't work (probably due to the failed loadfile) which also results in a "error in error handling" message.
C++:
// Having loaded game.lua...
state["init"]();
std::string mystring = state["test"]();
game.lua:
function init()
loadfile("functions.lua")()
end
functions.lua:
function test()
return "test"
end
Error:
"Error in error handling"
When I use ZeroBrane to test this Lua script by simply doing:
init()
print(test())
It all works fine... so there's something wrong with how I'm either using C++ or using the lua file.
What am I doing wrong here? Why doesn't loadfile seem to work when used in this manner? Does lua not get initialized to the point of seeing its regular functions with started by C++? Really strange how loadfile works via ZeroBrane but not from C++.
I'm using Selene, a C++11 header-only bindings to Lua.