Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can't say <code>PrintVariable(MyVariable)</code>, because Lua gives you no way of determining which variable (if any; a constant could have been used) was used to pass an argument to your function. However, you <em>can</em> say <code>PrintVariable('MyVariable')</code> then used the debug API to look for a local variable in the caller's scope which has that name:</p> <pre><code>function PrintVariable(name) -- default to showing the global with that name, if any local value = _G[name] -- see if we can find a local in the caller's scope with that name for i=1,math.huge do local localname, localvalue = debug.getlocal(2,i,1) if not localname then break -- no more locals to check elseif localname == name then value = localvalue end end if value then print(string.format("%s = %s", name, tostring(value))) else print(string.format("No variable named '%s' found.", name)) end end </code></pre> <p>Now you can say:</p> <pre><code>PrintVariable('MyVariable') </code></pre> <p>While in this case will print "MyVariable = 4".</p> <hr> <p>Not, if you <em>really</em> want to do this without the quotes, you could check the caller's locals for variables that have a supplied value, but that's occasionally going to give you the wrong variable name if there is more than one variable in the caller's scope with a given value. With that said, here's how you'd do that:</p> <pre><code>function PrintVariable(value) local name -- see if we can find a local in the caller's scope with the given value for i=1,math.huge do local localname, localvalue = debug.getlocal(2,i,1) if not localname then break elseif localvalue == value then name = localname end end -- if we couldn't find a local, check globals if not name then for globalname, globalvalue in pairs(_G) do if globalvalue == value then name = globalname end end end if name then print(string.format("%s = %s", name, tostring(value))) else print(string.format("No variable found for the value '%s'.", tostring(value))) end end </code></pre> <p>Now you can say <code>PrintVariable(MyVariable)</code>, but if there happened to be another variable in the caller's scope with the value <code>4</code>, and it occurred before <code>MyVariable</code>, it's <em>that</em> variable name that will be printed.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload