I have a Lua script that I wrote and I have two functions inside it:
function CallbackServerStatus ()
print("Status exec")
end
function CallbackServerInit ()
print("Server initialized\n")
end
And this is how I am trying to call my Lua function in C++:
printf("LUA | Exec LUA: CallbackServerInit()\n");
luaL_dofile(LuaEngine::state, "loaders/test.lua");
lua_getglobal(LuaEngine::state, "CallbackServerInit");
lua_pcall(LuaEngine::state, 0, 0, 0);
But in the console "Server initialized\n" is no where to be seen. Am I doing something wrong here?? There isn't even an error, and I only see "Server initialized\n" when I remove the CallbackServerStatus() function.
Ok, I found out that there was a non printing character in my script that was causing the script to fail.
Thank you for the answers!
I think you may need to restructure your code too.
void execute(std::string szScript)
{
int nStatus = 0;
nStatus = luaL_loadfile(L, szScript.c_str());
if(nStatus == 0){ nStatus = lua_pcall(L, 0, LUA_MULTRET, 0); }
error(nStatus);
}
void callFunction(std::string szName)
{
int nStatus = 0;
lua_getglobal(L, szName.c_str());
nStatus = lua_pcall(L, 0, LUA_MULTRET, 0);
error(nStatus);
}
void error(int nStatus)
{
if(nStatus != 0)
{
std::string szError = lua_tostring(L, -1);
szError = "LUA:\n" + szError;
MessageBox(NULL, szError.c_str(), "Error", MB_OK | MB_ICONERROR);
lua_pop(L, 1);
}
}
I've wrote this for my application. You can use it too. This way you can observe any kind of error at compiling scripts or calling functions.
execute("C:\test.lua");
callFunction("MyFunc");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With