mIRC Scripting

mIRC: Variables

mIRC variables are items which can hold temporary data to be used at a later time. You can create, edit, or delete them at any time. All mIRC variables must be prefixed with % sign (example %var or %cookies).

The basic way to create a variable:

set %MyVar My Saved Text

Variable Scope: Global vs. Local

Scope refers to the visibility of variables in your code. In other word, what scripts can 'see' that variable.

Local Variables are given local scope. They are created for the duration of the script that made them and they can only be accessed from that script. Once the script is done the variable is deleted.

Syntax:

;single variable
var %temp = value

;multiple variables
var %a = value, %b = second value, %c = and so on and so on

Practical use: a local variable is good for temporary string manipulations and short term uses. (Custom Text for Events, Birthday, Clone Scan, Music Player, etc...)

Example:

Alias Reverse_Text {
  /*
  The following variables are only
  needed for the duration of the script
  */

  var %input $1-, %output, %counter 1
  while (%counter <= $len(%input)) {
    var %output $mid(%input,$v1,1) $+ %output
    inc %counter
  }
  return %output
}

A Global variable is a variable that is accessible in every scope. They can be created and edited from every script. They are not deleted unless they were purposely unset.

Syntax:

;create variable
set %var value
;unset variable
unset %var

Because the /set command can only set a single variable at a time, you can use the /var command in conjunction with –g switch to set multiple global variables.

var -g %var foo, %var2 bar, %var3 foobar

Practical use: a global variable is good for storing variables that you will need to use in the future from another script or at different time. (Login System, Away System, Sockets, Etc…)

Example:

/*
Example:
.setbday August 7
.bday
*/

On 1:INPUT:#:{
  if ($1 == .setbday) {
    set %MyBirthDay $2-
  }
  elseif ($1 == .bday) {
    say $duration($calc($ctime(%MyBirthDay $time(yyyy) 00:00:00) - $ctime))
  }
}

Dynamic Variables

Dynamic variables are variables that are named dynamically. A dynamic variable is typically made of two parts: a static part, which always remain the same, and a dynamic parts, that changes as needed.

set %var $+ Dynamic_Part value
set %var $+ %var2 value

Let’s assume we have a small greeting system. Each user can set his own greet. Our variables might look like this:

%Greet.*!*@foo.comcast.net = Cool Matt is here!
%Greet.*!*@bar.example.com = Blah Blah Blah
%Greet.*!*@host.example.com = Hello Everyone!

Our variable is made of a static part “%Greet.” and a dynamic part “$address($nick,2)”.

We will use the following statement to retrieve the variable’s value:

%Greet. [ $+ [ $address($nick,2) ] ]

We use the evaluation brackets to evaluate the $address($nick,2) before everything else. mIRC will then attach ($+) it to the “%Greet.” before evaluating the final variable.

On 1:Join:#MyChannel:{
  if (%Greet. [ $+ [ $address($nick,2) ] ]) {
    msg $chan $+([,$nick,]) $v1
  }
}