July 2018
Beginner
202 pages
5h 42m
English
The local variables of any function can be inspected with the debug.getlocal function. This function takes two arguments: first a stack level (the same as debug.getinfo), and then a variable index. Variables are indexed in the order they appear. If either argument is out of range, the debug.getlocal function will return nil. Otherwise, it will return the name and value of the current variable.
For example, the following code prints the local variables of its calling function:
function DebugLocals() local info = debug.getinfo(2, "n") print ("Local variables of: " .. info.name) local idx = 1 while true do local name, val = debug.getlocal(2, idx) if name == nil then break end print (" " .. name .. " = " .. tostring(val)) idx ...