From WikiChip
mirc/goto statements
< mirc
Revision as of 05:01, 30 November 2013 by David (talk | contribs) (Error Resetting)

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.

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 will execute this section. This gives you the ability to continue with the script regardless of the error. If an error occurs but mIRC cannot locate an error goto label in the current routine, mIRC will propagate back to any calling routine and look for one there.

Alias Example {
  /*
    Second parameters of the $rand identifier is missing.
    Deliberate Error
  */
  echo -a $rand(1,)
  return
  :error
  echo -a Error: $error
}

Error Resetting

Using the /reseterror you can continue on with the rest of the script. Not resetting the error will halt script execution (in such case, mIRC will keep looking for ":error" in all calling routines).

Alias Foo {
  echo -a $FooBar
}
Alias Bar { 
  return $mid($1)
}
Alias FooBar {
  return $Bar(Example)
  :error
  echo -a Error Using Value 1000 instead! ( $+ $error $+ )
  reseterror
  return 1000
}


Although the code might look a little confusing, bear with me. Calling alias /Foo will echo what alias FooBar returns. Alias foobar will return what alias bar returns. Because alias bar errors out (since $mid is missing a few parameters) , the "error" section of the foobar alias will get executed, echoing Error followed by the error message and returning 1000 to alias foo. The /reseterror command effectively prevents mIRC from halting the script and allows it to finish executing.