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

Writing values to a table

Lua offers the lua_settable (lua_State*, int) function to set fields in a table. The function returns nothing. Its first argument is the Lua state to work on and the second argument is the index of a table on the stack.

The value being set is expected to be on top of the stack, and the key to set it to is expected to be just below that. lua_settable will pop both the key and value off the stack, but it will leave the table on the stack.

For example, the Lua code vector["y"] = 7 could be written with this API as follows:

// Push vector onto the stacklua_gettable(L, "vector");// Push y onto the stacklua_pushstring(L, "y");// Push 7 onto the stacklua_pushnumber(L, 7);// The stack has three new variables on it// The ...
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