WoW:API getglobal: Difference between revisions

From AddOn Studio
Jump to navigation Jump to search
mNo edit summary
Line 1: Line 1:
{{wowapi}} __NOTOC__
{{wowapi}}  
 
 
<!-- Describe the purpose of the function, exhausting detail can be saved for a later section -->
Get a global variable, from a string.
Get a global variable, from a string.
 
  local targetGlobal = getglobal("globalName")
<!-- List return values and arguments as well as function name, follow Blizzard usage convention for args -->
  getglobal("globalName")
 


== Parameters ==
== Parameters ==


=== Arguments ===
=== Arguments ===
<!-- List each argument, together with its type -->
:("globalName")
:("globalName")


Line 18: Line 11:


=== Returns ===
=== Returns ===
<!-- List each return value, together with its type -->
:;The object from the given string, or nil if not found
<!-- :isTrue, retVal1, retVal2-->  <!-- remove this line if it's just one value -->
 
:;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)


You can immitate getglobal() by using the following script:
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()
_G = getfenv()         -- set _G to the global environment once at the beginning of your script
  local prevval = _G["MyVariable"]  ''-- "getglobal()"''
   ...
   _G["MyVariable"] = 1234            ''-- "setglobal()"''
myvar = _G[globalName] -- this is what getglobal() does
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.''
Remark from Airlag:
 
getglobal() is doing a little more than _G[globalName]
 
It allso finds _G[anything][globalName] or _G[anything][deeper][globalName] or any deeper hidden child object.

Revision as of 09:09, 18 July 2007

WoW API < getglobal

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.