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?

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Oh my god, I feel so stupid now. I literary tried to solve this for more than 40 minutes. Thanks alot

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.