WoW:API setglobal: Difference between revisions

From AddOn Studio
Jump to navigation Jump to search
m (Added a "/" before "script" in the Example section)
No edit summary
Line 1: Line 1:
{{wowapi}} __NOTOC__
{{wowapi}}
 
 
<!-- Describe the purpose of the function, exhausting detail can be saved for a later section -->
Set a global variable, from a string.
Set a global variable, from a string.
 
  setglobal( "globalName", value )
<!-- List return values and arguments as well as function name, follow Blizzard usage convention for args -->
  setglobal( "globalName", value );
 


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


Line 19: Line 11:


=== Returns ===
=== Returns ===
<!-- List each return value, together with its type -->
<!-- :isTrue, retVal1, retVal2-->  <!-- remove this line if it's just one value -->
:;Always returns nil.
:;Always returns nil.


== Example ==
== Example ==
  /script setglobal( "MyVariable", 1234 );
  /script setglobal( "MyVariable", 1234 )


=== Result ===
=== Result ===
  MyVariable = 1234
  MyVariable = 1234


== Details ==
== 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:
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 globalenv = getfenv();
   local _G = getfenv()
   local prevval = globalenv["MyVariable"];   ''-- "getglobal()"''
   local prevval = _G["MyVariable"]  ''-- "getglobal()"''
   globalenv["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.
Since calling a function always requires some extra overhead, it's most likely a better idea to not use setglobal and getglobal, but to use the global environment (as above) instead.

Revision as of 08:55, 18 July 2007

WoW API < setglobal

Set a global variable, from a string.

setglobal( "globalName", value )

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.