I did a first small test with LuaBridge 1 week ago and it worked to get an int from the script.
Now I deleted this code and tried to include Lua script in my game engine but it is no longer working. I tried to go back to a basic code with this :
#include <iostream>
#include "lua5.2/lua.hpp"
#include "LuaBridge/LuaBridge.h"
using namespace luabridge;
int main()
{
lua_State* L;
L = luaL_newstate();
if(!luaL_loadfile(L, "../../script.lua"))
std::cout << "failed loading" << std::endl;
LuaRef s = getGlobal(L, "nmbr");
int luaInt = s.cast<int>();
std::cout << luaInt << std::endl;
return 0;
}
with this script
nmbr = 30
And it gives me :
PANIC: unprotected error in cell to Lua API (bad argument #2 (number expected, got nil)) Aborted (core dumped)
It the same when I tried to get a string or a function from the script and I have no idea what I'm doing wrong in this.
Thanks for your answers :)
From the documentation of luaL_loadfileex
:
As lua_load, this function only loads the chunk; it does not run it.
What that means is that the script is loaded, but it haven't been executed so there is really no variable nmbr
to get. You need to run the script first for the code to work (for eample by calling lua_call
).
This is shown very well in the first simple example in this LuaBridge tutorial.