Finding local variables defined in another file?

I’m trying to use debug.getlocal to find all defined local variables and filter out from there but for some reason it doesn’t consider anything but the lua file you’re doing it in to be in the right scope.

e.g. file 1 (using test code nicked from stackoverflow) will not print what’s in file 2, that being “hello = 5”

file 1:



function locals()
  local variables = {}
  local idx = 1
  while true do
    local ln, lv = debug.getlocal(2, idx)
    if ln ~= nil then
      variables[ln] = lv
    else
      break
    end
    idx = 1 + idx
  end
  return variables
end


file 2:



local hello = 5


This doesn’t even work if it’s included at the start of file 1- am I missing something guys?

Each file is a separate scope. If there’s a global function in file2 that uses the local variable you can use debug.getupvalue to retrieve it.

I tried with debug.getupvalue but that didn’t work either or am I missing something and it’s just not possible to do what I want?

It’s not possible to do, atleast not as easy as this.

aww, that sucks, you say “not as easy”, does that mean you know another way to do it…?

Mabe. I don’t really see the point of doing what you’re trying to do though.

V. useful for code documentation, either auto generate or tell me if I forgot to properly comment some stuff