WoW API < setglobal

getglobal and setglobal are deprecated and replaced with _G[][1]. Their functionality will be removed from the game at a future date. 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

Set a global variable, from a string.

setglobal( "globalName", value )

Parameters Edit

Arguments Edit

("globalName", value)
String - Name of the global you want to change.
Any - Value you want to set the global to.

Returns Edit

Always returns nil.

Example Edit

/script setglobal( "MyVariable", 1234 )

Result Edit

MyVariable = 1234

Details Edit

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.

Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.