WoW:API getglobal: Difference between revisions

From AddOn Studio
Jump to navigation Jump to search
(okay?)
 
m (Move page script moved page API getglobal to API getglobal without leaving a redirect)
 
(7 intermediate revisions by 7 users not shown)
Line 1: Line 1:
{{wowapi}} __NOTOC__
{{wowapi}}  


''Note: getglobal and [[API_setglobal|setglobal]] are deprecated and replaced with _G[]<ref>http://forums.worldofwarcraft.com/thread.html?topicId=25626580975&sid=1</ref>. Change all references of getglobal and setglobal:''


<!-- Describe the purpose of the function, exhausting detail can be saved for a later section -->
  var = getglobal(varName)        --getglobal deprecated
Get a global variable, from a string.
  var = _G[varName]              --new syntax to get a global


<!-- List return values and arguments as well as function name, follow Blizzard usage convention for args -->
  setglobal(otherName, otherVar)  --setglobal deprecated
getglobal("globalName")
  _G[otherName] = otherVar        --new syntax to set a global


== Description ==
Get a global variable, from a string.
local targetGlobal = getglobal("globalName")


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


:;globalName : String - Name of the global you want to get.
:;globalName : String - Name of the global you want to get.


=== Returns ===
== Returns ==
<!-- List each return value, together with its type -->
:;The value 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:
-- i = some_dynamic_number
local curFrame = getglobal("myFrame"..i)


This function is used to get an object if you generate the name of the object manually in your script:
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 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 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).


i = some_dynamic_number;
''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 curFrame = getglobal("myFrame"..i)
  local _G = _G  -- a local speeds up access to [[Dump_G|_G]] slightly. negligible for infrequent calls.
  local prevval = _G["MyVariable"]  ''-- "getglobal()"''
  _G["MyVariable"] = 1234            ''-- "setglobal()"''

Latest revision as of 04:45, 15 August 2023

WoW API < getglobal

Note: getglobal and setglobal are deprecated and replaced with _G[][1]. Change all references of getglobal and setglobal:

 var = getglobal(varName)        --getglobal deprecated
 var = _G[varName]               --new syntax to get a global
 setglobal(otherName, otherVar)  --setglobal deprecated
 _G[otherName] = otherVar        --new syntax to set a global

Description[edit]

Get a global variable, from a string.

local targetGlobal = getglobal("globalName")

Arguments[edit]

("globalName")
globalName
String - Name of the global you want to get.

Returns[edit]

The value from the given string, or nil if not found

Details[edit]

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 (as well as direct access to the table _G as a global), 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 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:

 local _G = _G   -- a local speeds up access to _G slightly. negligible for infrequent calls.
 local prevval = _G["MyVariable"]   -- "getglobal()"
 _G["MyVariable"] = 1234            -- "setglobal()"