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?
Change %i
to %g
. lua_tonumber
returns a float or double, not an int.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.