4

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?

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
Erik W
  • 2,590
  • 4
  • 20
  • 33

1 Answers1

3

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

lhf
  • 70,581
  • 9
  • 108
  • 149
  • Oh my god, I feel so stupid now. I literary tried to solve this for more than 40 minutes. Thanks alot – Erik W Mar 18 '15 at 21:04