WoW:API setglobal: Difference between revisions
No edit summary |
m (Move page script moved page API setglobal to API setglobal without leaving a redirect) |
||
| (6 intermediate revisions by 6 users not shown) | |||
| Line 1: | Line 1: | ||
{{wowapi}} | {{wowapi|toc=0}} | ||
Set a global variable, from a string. | |||
setglobal( "globalName", value ) | |||
< | == Deprecation == | ||
Both [[API_getglobal|getglobal]] and setglobal are deprecated and replaced with the '_G[]' <ref>http://forums.worldofwarcraft.com/thread.html?topicId=25626580975&sid=1</ref> the global table. Their functionality will be removed from the game at a future date. | |||
Replace all references of 'getglobal': | |||
var = getglobal(varName) --getglobal deprecated | |||
var = _G[varName] --new syntax to get a global | |||
Replace all references of 'setglobal': | |||
setglobal(otherName, otherVar) --setglobal deprecated | |||
_G[otherName] = otherVar --new syntax to set a global | |||
== Parameters == | == Parameters == | ||
=== Arguments === | === Arguments === | ||
:("globalName", value) | :("globalName", value) | ||
| Line 19: | Line 22: | ||
=== Returns === | === Returns === | ||
:;Always returns nil. | :;Always returns nil. | ||
== Example == | == Example == | ||
/script setglobal( "MyVariable", 1234 ) | |||
script setglobal( "MyVariable", 1234 ) | |||
=== Result === | |||
MyVariable = 1234 | MyVariable = 1234 | ||
== Details == | |||
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() | |||
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. | |||
== References == | |||
{{Reflist}} | |||
Latest revision as of 04:47, 15 August 2023
Set a global variable, from a string.
setglobal( "globalName", value )
Deprecation
Both getglobal and setglobal are deprecated and replaced with the '_G[]' [1] the global table. Their functionality will be removed from the game at a future date.
Replace all references of 'getglobal':
var = getglobal(varName) --getglobal deprecated var = _G[varName] --new syntax to get a global
Replace all references of 'setglobal':
setglobal(otherName, otherVar) --setglobal deprecated _G[otherName] = otherVar --new syntax to set a global
Parameters
Arguments
- ("globalName", value)
- globalName
- String - Name of the global you want to change.
- value
- Any - Value you want to set the global to.
Returns
- Always returns nil.
Example
/script setglobal( "MyVariable", 1234 )
Result
MyVariable = 1234
Details
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.