Template:mIRC Guide Goto is a command that allows you to jump unconditionally to a specific location within a procedure. Gotos can 'jump' forward or backward within a script but they may not leave the alias or event itself (they cannot jump to any calling routine as well). The goto command tells mIRC to jump to another line which matches a label.
Although in many cases the use of gotos can often lead to spaghetti-code. and can usually be replaced with easier to read and follow while statements and if statements, it is still important to understand this command and have it in your toolbox.
Contents
Syntax
The goto command has the following syntax:
goto label
A line is labeled using the following syntax:
:label
The label has to start with the colon symbol (:) and must be a single word.</p>
Conditional transfer of control
Many times you only want a script to go to a specific location according to a certain condition. You can use an if statement to achieve this. A simple syntax looks like this:
if (<condition>) { goto label }
Goto Loops
Goto statements can also be used to create a loop by jumping back to a previous location in the script. The example below is a simple loop counting from 1 to 10. If for any reason you mistakenly caused your script to execute endlessly, you manually break the loop using the Ctrl+Break keys.
Alias Count { :loop var %c $calc(%c + 1) echo -a %c if (%c <= 9) goto loop }
Random Name Example
Sometimes a goto can result in smaller code if we need a do-while style loop like other languages. A practical example of this is a random nickname generator which excludes us from the list. Such alias might look like this:
; precondition: $nick($chan, 0) > 1 alias rnd { :retry if ($nick($chan, $rand(1, $nick($chan, 0))) == $me) goto retry return $v1 }
Error Handling
Although it is rare to use this feature, the "error" label is a designated goto section for error checking.
If there is an error in the script, mIRC try to find an :error label in the current routine. If no :error label is found in the current routine, mIRC will propagate the error backward to any calling routine and look for an :error label in those routines.
This gives you the ability to continue with the script regardless of the error if you want.
After catching an error with :error, you must check for an error by checking $error with a typical /if statement to make sure you reached the :error part because of an error and not because the script is just reaching that point.
If you want to keep going with the code, use /reseterror as soon as possible, this reset the error (and therefore $error's value), mIRC leaves the error state and you can safely execute anything you want. If you try to execute anything else than /reseterror after reaching an :error label because of an error, mIRC can broke (see examples). Indeed, here mIRC is in an error state, yet it must still process your code somehow to allow you to *at least* check for error and reset it, so it's unclear what you can do and what you can't do besides an if statement on $error followed by /reseterror. You would think that since mIRC is somehow forced to allow you to use the scripting engine to check for error and reset it, you could be able to do anything even without calling /reseterror first (still after checking for $error), this is not true, the exact list of features that are working in this situation are unknown, it's recommended not to do anything before using /reseterror in this situation.
Alias Example { /* Second parameters of the $rand identifier is missing. Deliberate Error */ echo -a $rand(1,) return :error echo -a Error: $error } ;In this case, we try to execute something else than /reseterror after reaching :error, for some reasons, that $regsubex gets mIRC to silentely halts while inside a routine, you should get the first echo 'ok' and an echo '> $str(ab,2000)' is expected but you shouldn't see it. alias testgoto { if :error echo -a ok | echo -a > $regsubex($str(ab,2000),/(a*)b*(a*)*b/,) }