WoW:API getglobal: Difference between revisions

m
no edit summary
No edit summary
mNo edit summary
Line 1: Line 1:
{{wowapi}}  
{{wowapi}}  
{{removedfromgame|patch=4.0.1}}
Get a global variable, from a string.
Get a global variable, from a string.
  local targetGlobal = getglobal("globalName")
  local targetGlobal = getglobal("globalName")
Line 24: Line 25:
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).
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).


''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:
''Note:'' Accessing the global table through the function getfenv() was common within addons presumably because the global '[[Dump_G|_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 _G = _G  -- a local speeds up access to [[Dump_G|_G]] slightly. negligible for infrequent calls.
   local prevval = _G["MyVariable"]  ''-- "getglobal()"''
   local prevval = _G["MyVariable"]  ''-- "getglobal()"''
   _G["MyVariable"] = 1234            ''-- "setglobal()"''
   _G["MyVariable"] = 1234            ''-- "setglobal()"''
Anonymous user