I am new to lua, and I would like to make lua change variables in c++, say I have something like the following code, the hardest thing I have learning lua, is integrating it into c++.
extern "C"
{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
int main()
{
int s = 0;
int x;
lua_State *L = lua_open();
luaL_openlibs(L);
luaL_dofile(L,"file.lua"); // lua would set x to 5
printf("x is %d .",x); //how would I get lua to set x?
lua_close(L);
return 0;
}
Last edited by cndr on Mon Apr 26, 2010 5:09 pm, edited 1 time in total.
extern "C"
{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
int main()
{
int s = 0;
int x;
lua_State *L = lua_open();
luaL_openlibs(L);
luaL_dofile(L,"file.lua"); // lua would set x to 5
x=lua_getglobal(L,"x"); //assuming the variable in the lua script is also named x
printf("x is %d .",x); //how would I get lua to set x?
lua_close(L);
return 0;
}
You could also register a SetX function to lua but X would have to be global.
didn't work for me, maybe I should just stick with pure lua for a while, it seems getting the code to work with c++ is harder than I thought, when I ran the code I got the following error message:
extern "C"
{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
int main()
{
int s = 0;
int x;
lua_State *L = lua_open();
luaL_openlibs(L);
luaL_dofile(L,"file.lua"); // lua would set x to 5
lua_getglobal(L,"x"); //assuming the variable in the lua script is also named x
x=lua_tonumber(L,-1); //gets x from the lua stack
printf("x is %d .",x);
lua_close(L);
return 0;
}