WoW:API getglobal: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
|||
Line 1: | Line 1: | ||
{{wowapi}} | {{wowapi}} | ||
Get a global variable, from a string. | Get a global variable, from a string. | ||
local targetGlobal = getglobal("globalName") | |||
getglobal("globalName") | |||
== Parameters == | == Parameters == | ||
=== Arguments === | === Arguments === | ||
:("globalName") | :("globalName") | ||
Line 18: | Line 11: | ||
=== Returns === | === Returns === | ||
:;The object from the given string, or nil if not found | |||
:;The object from the given string | |||
== Details == | == Details == | ||
This function is used to get an object if you generate the name of the object manually in your script: | This function is used to get an object if you generate the name of the object manually in your script: | ||
-- i = some_dynamic_number | |||
i = some_dynamic_number | |||
local curFrame = getglobal("myFrame"..i) | local curFrame = getglobal("myFrame"..i) | ||
As of the introduction of [[API getfenv|getfenv]]() into the API, setglobal() and [[API getglobal|getglobal]]() are somewhat superfluous. You can always do something along the lines of: | |||
local _G = getfenv() | |||
local prevval = _G["MyVariable"] ''-- "getglobal()"'' | |||
_G["MyVariable"] = 1234 ''-- "setglobal()"'' | |||
Function calls always cost some overhead, so if a large number of getglobal calls are being made, the getfenv table will be a faster route. Note that for few or infrequent calls the performance gain of this method is negligible. | |||
:''<code>getglobal()</code> does a little more than <code>_G[globalName]</code>. It also finds <code>_G[anything][globalName]</code> or <code>_G[anything][deeper][globalName]</code> or any deeper hidden child object.'' | |||
getglobal() | |||
It |
Revision as of 09:09, 18 July 2007
Get a global variable, from a string.
local targetGlobal = getglobal("globalName")
Parameters
Arguments
- ("globalName")
- globalName
- String - Name of the global you want to get.
Returns
- The object from the given string, or nil if not found
Details
This function is used to get an object if you generate the name of the object manually in your script:
-- i = some_dynamic_number local curFrame = getglobal("myFrame"..i)
As of the introduction of getfenv() into the API, setglobal() and getglobal() are somewhat superfluous. You can always do something along the lines of:
local _G = getfenv() local prevval = _G["MyVariable"] -- "getglobal()" _G["MyVariable"] = 1234 -- "setglobal()"
Function calls always cost some overhead, so if a large number of getglobal calls are being made, the getfenv table will be a faster route. Note that for few or infrequent calls the performance gain of this method is negligible.
getglobal()
does a little more than_G[globalName]
. It also finds_G[anything][globalName]
or_G[anything][deeper][globalName]
or any deeper hidden child object.