Getting array value from index using Lua C Api
2 answers
Try the following:
lua_getglobal(L,"a")
lua_rawgeti(L,-1,1)
lua_rawgeti(L,-1,2)
After that, the value a[1][2]
will be at the top of the stack. The stack will also contain a
and a[1]
, which you might want to post when you're done (they will remain on the stack if you want multiple values).
+3
source to share
You can use the method lua_gettable
. However, there are a few important notes:
- Lua arrays start at index 1, not 0.
- You need to push the index onto the lua stack via
lua_pushinteger
. - The key is "replaced" by the indexed item.
+6
source to share