Quirrel4.41.0

debug.getlocals

Defined in module "debug"
from "debug" import getlocals
getlocals([level: int, include_internal: bool]): table

Returns a table of local variables at the given stack level

examples/debug/getlocals-basic.nut
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 = 45

Parameters

levelinthow many frames up from this call to read; defaults to 1, the caller of getlocals optional
include_internalboolwhen 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

examples/debug/getlocals.nut
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 table
Output:
getlocals() names = k, names, v, x, y
getlocals(1, true) has this = true
getlocals(100).len() = 0

See also

getstackinfosReturns call stack information for the given stack level
debugmodule index