Garry's Mod Wiki

Revision Difference

debug.getlocal#513386

<function name="getlocal" parent="debug" type="libraryfunc">⤶ <description>⤶ Gets the name and value of a local variable indexed from the level⤶ <warning>When a function has a tailcall return, you cannot access the locals of this function</warning>⤶ </description>⤶ <realm>Shared and Menu</realm>⤶ <args>⤶ <arg name="thread" type="thread" default="Current thread">The thread</arg>⤶ <arg name="level" type="number">The level above the thread. &#xA;0 = the function that was called (most always this function)&#x27;s arguments&#xA;&#xA;1 = the thread that had called this function.&#xA;&#xA;2 = the thread that had called the function that started the thread that called this function.&#xA;&#xA;A function defined in Lua can also be passed as the level. The index will specify the parameter&#x27;s name to be returned (a parameter will have a value of nil).</arg>⤶ <arg name="index" type="number">The variable&#x27;s index you want to get.&#xA;&#xA;1 = the first local defined in the thread&#xA;&#xA;2 = the second local defined in the thread&#xA;&#xA;etc...</arg>⤶ </args>⤶ <rets>⤶ <ret name="" type="string">The name of the variable⤶ ⤶ Sometimes this will be "(*temporary)" if the local variable had no name. ⤶ ⤶ <note>Variables with names starting with **(** are **internal variables**.</note></ret>⤶ <ret name="" type="any">The value of the local variable.</ret>⤶ </rets>⤶ </function>⤶ ⤶ <example>⤶ <description>Gets all the local variables of the current thread and stores them in a table.</description>⤶ <code>⤶ local name, value;⤶ local NIL = {} -- to represent nil variables⤶ local locals = {}⤶ local i = 1⤶ while( true ) do⤶ name, value = debug.getlocal( 1, i )⤶ if ( name == nil ) then break end⤶ locals[ name ] = value == nil and NIL or value⤶ i = i + 1⤶ end⤶ ⤶ for k,v in pairs( locals ) do ⤶ print( k, v )⤶ end⤶ </code>⤶ <output>⤶ ⤶ ```⤶ locals table: 0x274a8dc0⤶ value table: 0x274a8d98⤶ NIL table: 0x274a8d98⤶ name table: 0x274a8d98⤶ i 5⤶ ```⤶ ⤶ </output>⤶ ⤶ </example>⤶ ⤶ ⤶ <example>⤶ <description>Prints the parameter names for <page>hook.Add</page></description>⤶ <code>⤶ local print = print⤶ local getlocal = debug.getlocal⤶ ⤶ local function PrintFunctionParameters(func)⤶ local k = 2⤶ local param = getlocal(func, 1)⤶ while param ~= nil do⤶ print(param)⤶ param = getlocal(func, k)⤶ k = k + 1⤶ end⤶ end⤶ ⤶ PrintFunctionParameters(hook.Add)⤶ </code>⤶ <output>⤶ ⤶ ```⤶ event_name⤶ name⤶ func⤶ ```⤶ ⤶ </output>⤶ ⤶ </example>