I managed to get almost all variables from my Lua application with this function, but not the ones that are local to a .lua files...
Example at the root of the main.lua : local test = 10
This cannot be found.
Any idea?
Note: I'm running this in a Love2D app, and this function is called from a Love callback (love.draw).
function getvarvalue (name)
local value, found
-- try local variables
local exit = false
local level = 0
while debug.getinfo(level) ~= nil do
local i = 1
while true do
local n, v = debug.getlocal(level, i)
if n then
print("local "..i.." "..level.." name:"..n)
end
if not n then break end
print(n)
if n == name then
value = v
found = true
end
i = i + 1
end
if found then return value end
level = level + 1
end
-- try upvalues
level = 0
while debug.getinfo(level) ~= nil do
local func = debug.getinfo(level).func
i = 1
while true do
local n, v = debug.getupvalue(func, i)
if n~= nil then print("up "..i.." "..level.." "..n) end
if not n then break end
if n == name then return v end
i = i + 1
end
if getfenv(func)[name] ~= nil then
return getfenv(func)[name]
end
level = level + 1
end
-- not found
return "?"
end