Skip to Content
Lua Quick Start Guide
book

Lua Quick Start Guide

by Gabor Szauer
July 2018
Beginner
202 pages
5h 42m
English
Packt Publishing
Content preview from Lua Quick Start Guide

Reading values from a table

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Beginning Lua Programming

Beginning Lua Programming

Kurt Jung, Aaron Brown
Vim Masterclass

Vim Masterclass

Jason Cannon

Publisher Resources

ISBN: 9781789343229Supplemental Content