Difference between revisions of "Scope"

From Scriptwiki
Jump to: navigation, search
m
 
m (copy paste error...)
Line 33: Line 33:
 
Suppose the following was called using /test  
 
Suppose the following was called using /test  
 
  [[alias]] test {
 
  [[alias]] test {
   set %foo bar.
+
   var %foo = bar.
 
  ; %foo is available here
 
  ; %foo is available here
 
  test2 %foo
 
  test2 %foo

Revision as of 20:52, 3 October 2009

Scope is the term used to refer to a variable's "life-span". mIRC variables have only two scopes.

Global Scope

A variable with a global scope is accessable everywhere in mIRC where a variable can be used. This means in the command line, in every script file, alias file, popup, etc. These variables are saved in the Variables tab of the mIRC Scripts editor (alt+r) which are saved to a file (usually vars.ini).

  • A global scoped variable is created with set and destroyed with unset

Example

Suppose the following was called, /test and then /test2.

alias test {
 set %foo bar.
; %foo is available here
}
alias test2 {
 set %bar foo
 ; %foo is available here after being set with /test
}


%foo will now return bar EVERYWHERE.

Local Scope

A variable with a local scope is only available within the event, alias, or menu (popup) script in which it is used.

  • A local variable is created with var and is automatically destroyed after leaving scope (exiting the current alias, event or menu).

Example

Suppose the following was called using /test

alias test {
 var %foo = bar.
; %foo is available here
test2 %foo
}
alias test2 {
 set %bar foo
 ; %foo is NOT available here. We can, however, use $1 to refer to %foo because it was passed as a parameter to the alias.
}


See Also

  • var - Command used to create a local variables
  • Local Variables - Variables with limited scope
  • set - Command used to create global variables