debug.getlocals
Defined in module
"debug"from "debug" import getlocalsReturns a table of local variables at the given stack level
from "debug" import getlocals
function aimTurret() {
let turretAngle = 45
let frame = getlocals()
println("\"turretAngle\" in getlocals() =", "turretAngle" in frame, "value =", frame.turretAngle)
}
aimTurret()Output:
"turretAngle" in getlocals() = true value = 45Parameters
level | int | how many frames up from this call to read; defaults to 1, the caller of getlocals optional |
include_internal | bool | when true, also include this, vargv, and any name starting with @; defaults to false optional |
Return value
A table of local variables, keyed by name, holding whatever has been declared at level up to the point of this call. A level this VM has no frame for gives an empty table, not an error.
Notes
The default level is 1, not 0: level 0 would be this call to getlocals itself, and a native function has no script locals of its own to report.
Example
from "debug" import getlocals
function inner(x) {
let y = 2
let names = []
foreach (k, v in getlocals()) names.append(k) // default level: inner's own frame
names.sort()
println("getlocals() names =", ", ".join(names))
let withThis = []
foreach (k, v in getlocals(1, true)) withThis.append(k)
println("getlocals(1, true) has this =", withThis.contains("this"))
}
inner(5)
println("getlocals(100).len() =", getlocals(100).len()) // no such level, but still a tableOutput:
getlocals() names = k, names, v, x, y
getlocals(1, true) has this = true
getlocals(100).len() = 0See also
getstackinfos | Returns call stack information for the given stack level |
| debug | module index |