4
votes

I am trying to get values from a Lua table. This is what I have written in Program.cpp:

lua_State* lua = luaL_newstate();
luaL_openlibs(lua);
luaL_dofile(program->getLuaState(), "Script.lua");

lua_getglobal(lua, "table");
lua_pushstring(lua, "x");
lua_gettable(lua, -2);
printf("%i", lua_tonumber(lua, -1));

And I wrote this in Script.lua:

table = {x = 12, y = 32}

The problem is that this only writes 0 in the console. I have checked that the lua file is loading correctly. What am I doing wrong?

1

1 Answers

3
votes

Change %i to %g. lua_tonumber returns a float or double, not an int.