mIRC ScriptBox
Basics: Introduction into Varables

Written by Psionic

Table Of Contents

  1. What are Variables?
  2. Setting Variables
  3. Increasing and Decreasing
  4. Unsetting Variables
  5. Binary Variables
  6. Sockets
  7. Example: Using Variables as timers

1. What are Variables?  Back to Top

Variables are a function in mIRC that allow you to set values to words (or anything you wish on that matter) that begin with a % (and in some cases a &.) They can be used to store information for later use in a script.

This could be for a number of things, such as Control Codes (Bold, Underline, Colour, Reverse, Off) Kicks, and AutoJoin Scripts, etc...


2. Setting Variables  Back to Top

The most basic command used for variable is the /set command. This assigns a value to a variable.
Example: /set %anything-you-want www.dotcom.com
The variable %anything-you-want is now set so www.dotcom.com

Another way to set variables is in a script, such as an alias, use a = sign,
Example: %anything-you-want = >value<
Once a variable, such as %anything-you-want is set, you can use "//echo -a >variable name<" to find out what has been stored in that variable.
With our example, typing " //echo -a %anything-you-want " would give you: www.dotcom.com (in the Active window)
If you try to echo a %variable that has not been set, its value is $null (which means 'nothing').
If you tried this, mIRC would give you:
* /echo: insufficient parameters
This basically means you tried to //echo something that isn't there.

In mIRC 5.6, Local Variables were added. Local Variables are just like regular variables, except for they only exist until the script that set them finishes processing. They are also different in the way that they are set, using /var %name >stuff<. If you try to use /var %name something in an editbox with no other commands, you will receive and Unknown Command error.
They can be /echo
You can also type a " %variable " or " $identifier " in the editbox and press " TAB ". This will change the %variable or $identifier that you typed into what it is set to, or would return.


3. Increasing and Decreasing  Back to Top

Command: /inc

The next commands, /inc and /dec are very similar in syntax. /inc INCreases the value of a variable that is set to a number.

So lets say we were making a # of people joined Counter, first we would " /set %joined 0 " Then, we would use remotes, and the /INC command in the following way:

On 1:JOIN:#helpdesk:{
  /INC %joined 
  /notice $nick You are person $chr(35) $+ %joined to join this channel.
}

This line makes sure that every time somebody joins the channel, it will execute the command " /INC %joined " This adds 1 to the value already stored in %joined Then, the script will notice the person who joined the channel in question. You are person #<number> to join this channel. The script will follow these steps every time someone enters the channel.

Another feature of /INC is the -uN argument. What this does is /INC %variable for a certain amount of time, and them completely /unset it.

Here is what " /inc -u10 %test " would do: Lets say %test is currently set to the number 12 If I had performed the command, %test would have gone up by 1, which will make it 13. Then, within 10 seconds, %test would be $null. Remember that we said that " $null " means 'nothing'

Command: /dec

Now, lets switch gears and think about /dec. This command DECreases a variable by 1. Let's say we had %test set to the value 13. If we would " /dec %test ", it would drop the variable value to 12.

As I said, /inc and /dec are very similar in syntax. Now, an example of the -uN argument. Let's say %test is set to 12 again. If we " /dec -u10 %test ", %test would become 11, then within 10 seconds, it would be unset (meaning $null).


4. Unsetting Variables  Back to Top

Command: /unset

/unset is just the opposite of the /set command. Instead of giving a value to a variable, it removes it.
Example:
%anything-you-want was set to www.dotcom.com using /set command. Typing /unset %anything-you-want would take all value from it. *sob* hehehe There's also another powerful command: /unsetall This unsets ALL Variables, so be careful when using this.

Math

Variables can also use the = sign, as we have mentioned above, to perform math operations.
Example: % = 5 + %test
This would set % as 5 plus whatever %test is set to. If %test was 4, then % would be 9. It cannot be used to perform multiple operations. Use $calc for that.


5. Binary Variables  Back to Top

Binary variables start with a & instead of a %, and are different in a few ways. "Binary variables cannot be printed, assigned or edited." They can only be access by a few commands, and are destroyed after their script finished processing, somewhat like Local %Variables, in that way. Binary variables can have a maximum size of 4096 bytes.
Binary variables do not have $crlf's (cr lf,a new line) at the end, so when they are written, they do not have separate lines for each variable. Because of this, they can be used to upload and download files from webservers, and you can use them to work with files on your computer.

Command: /bread (mmm...bread)

Usage: /bread filename.ext S N &binvar
This command reads N bytes starting at the S byte position and stores it in the specified &binvar.

Command: /bwrite

Usage: /bwrite filename.ext S N &binvar|text|%var

This command will write N bytes of the specified &binvar or text or %var into filename.ext starting at the S byte position. If you specify -1 as the S position, the bytes will be appended to the end of the file, and if you specify N as -1, the entire &/% variable or text will be written to the file. If the file doesn't exist, mIRC will create one.

*** Note: when /bwrite'ing to a file, it is good to use -1 or start at the 0 byte position.

Command: /bset

Usage: /bset [-t] &binvar N asciivalue [asciivalue ...]

This command sets they Nth byte of the specified &binvar to the specified ascii value(s). If you /bset a &binvar, and you specify a byte that doesn't exist (other than 1) mIRC will fill in the other bytes with 0's.
If you specify more than one asciivalue, mIRC will write all of them starting at the Nth byte position. If you specify -t, mIRC will treat the specified ascii values as normal text, so you could use %3A and have mIRC NOT interpret it as a %variable.

Command: /breplace

Usage: /breplace &binvar oldvalue newvalue [oldvalue newvalue ....]
This replaces ascii 'oldvalues' with the new ascii values.

Identifier: $bvar

Usage: $bvar(&binvar,n[-n2])[.text]
This is used to return ascii numbers and text of the specified bytes of &binvar. the " -n2 " is optional, and so is the " .text ".

Example:
This is an example alias that uses most of the above Binary Variable commands and identifiers.
This can go into remotes (ALT+R):


alias mIRC-Binary-stuff {
  set %bytes 4096
  bread mirc $+ $bits $+ .exe 0 %bytes &mIRC-binvar
  bwrite mirc.txt -1 -1 &mIRC-binvar
  echo 12 -a These are the last 265 asciivalues of mIRC:
  echo -a $bvar(&mIRC-binvar,3831-4096)
  ;to see just how many bytes are 0, use /breplace and turn them into 127's and write them to a file
  breplace &mIRC-binvar 0 127 | bwrite mirc.txt -1 -1 This many bytes are 0s (all the 127's)
  write mirc.txt -
  write mirc.txt -  
  bwrite mirc.txt -1 -1 &mIRC-binvar
  echo 12 -a Look at notepad.  The First section of bytes is the first 4096 bytes.
  echo 12 -a The second is the first 4096 with 0s replaced with 127's. 
  run -n mirc.txt
  ;once mIRC passes this last bracket, all &binvars will be destroyed
  .timer1 1 2  .remove mirc.txt
}

Then just type /mIRC-Binary-Stuff


6. Sockets  Back to Top

This file is NOT about sockets, but since this area isn't covered in the Sockets Tutorial, i decided to add a little about this area.

With /Sockwrite, you can /sockwrite -b sockname <number of bytes> <&binvar>. You can also /sockwrite -t sockname to force mIRC NOT to send text beginning with a & as a &binvar, but as normal Text.

With /Sockread, you can /sockread [numbytes] &binvar. If you don't specify a binvar, mIRC will default to 4096. You can /Sockread -f and make mIRC force whatever text is in the sockets receive buffer into the specified variable. You Can /Sockread -n to read a CRLF terminated line into a &binvar. For more info, /help is the best thing out there


7. Example: Using Variables as timers  Back to Top

Variables can be used as timers and counters at the same time! We will show you how to use variables as timers...
Lets say you want to stop someone from running an alias 5 times in 20 seconds.
Here's a sample alias we will use:

alias VarTimer {
	IF (%timer == $null) 
	{ 
		SET -u20 %timer 0 
	}
	ELSE 
	{ 
		INC %timer		
	}
	IF (%timer > 5) 
	{ 
		echo -a $chr(3) $+ 5 Error: Cannot use this alias more than 5 times in 20 seconds.
		HALT
	}
	echo -a $chr(3) $+ 3 $+ Timer is currently at count: %timer
}

;And for speed purposes
alias F6 { vartimer }
Now, once you have loaded this into your remotes section, hit F6 a few times. You'll see that you cannot run the alias more than 5 times in 20 seconds.