July 2018
Beginner
202 pages
5h 42m
English
Fields from a table can be retrieved with the lua_gettable (lua_State*, int) function. This function returns nothing; its first argument is the Lua state to work on. Typically, accessing a table in Lua involves both the table and a key, for example: tbl[key]. Using lua_gettable, the table (tbl) is expected to be at the index specified by the second variable. The key (key) is expected to be on the top of the stack. The following code demonstrates how to retrieve the value of the key x from a table named vector:
lua_getglobal(L, "vector");lua_pushstring(L, "x");lua_gettable(L, -2);
Since retrieving a variable is so common, Lua provides a handy shortcut function, lua_getfield (lua_State*, int, const char*). This helper ...