WoW:API getglobal: Difference between revisions

no edit summary
No edit summary
Line 11: Line 11:


=== Returns ===
=== Returns ===
:;The object from the given string, or nil if not found
:;The value from the given string, or nil if not found


== Details ==
== Details ==
Line 18: Line 18:
  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:
As of the introduction of [[API getfenv|getfenv]]() into the API (as well as direct access to the table _G as a global), setglobal() and [[API getglobal|getglobal]]() are somewhat superfluous. You can always do something along the lines of:
   local _G = getfenv()
   local _G = getfenv()
   local prevval = _G["MyVariable"]  ''-- "getglobal()"''
   local prevval = _G["MyVariable"]  ''-- "getglobal()"''
   _G["MyVariable"] = 1234            ''-- "setglobal()"''
   _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.
Function calls always cost some overhead, so if a large number of getglobal calls are being made, the global table will be a faster route.  Note that for few or infrequent calls the performance gain of this method is negligible (but given the lack of any loss of functionality, recommended).
:''<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.''
 
''Note:'' Accessing the global table through the function getfenv() was common within addons presumably because the global '_G' (the direct global table) was hidden from the WoW environment. It seems to have since been uncovered, and now calling it directly for global access is possible:
  local _G = _G  -- a local speeds up access to _G slightly. negligible for infrequent calls.
  local prevval = _G["MyVariable"]   ''-- "getglobal()"''
  _G["MyVariable"] = 1234            ''-- "setglobal()"''
Anonymous user