Working with Lua table entries
A table entry is the key-value pair for a table element. Lua table keys can be of many data types – for example, of function type. For practical reasons, especially when integrating with C++, we only consider string keys and integer keys.
In script.lua
, add a simple table as follows:
position = { x = 0, y = 0 }
position
is indexed by strings. We will learn how to read from and write to it in C++.
Getting a table entry value
Up until now, in C++ code, we have only used one piece of information to locate a value in Lua. Consider how we implemented LuaExecutor::getGlobal
and LuaExecutor::call
. To locate a global variable or to call a function, we pass the name of the variable or the function to a Lua library method.
To work with a table entry, we would need two pieces of information – the table and the table entry key. First, we need to locate the table; after that, we can use the entry key to work on the entry value.
The...