Difference between revisions of "Manual:DIL Manual/goto"

From DikuMUD Wiki
Jump to navigation Jump to search
(XML import)
(checked against the compiler and the interpreter; example compiles)
 
Line 1: Line 1:
−
<span id="dcgoto"></span>
+
= goto =
−
'''goto:'''
+
'''Instruction.''' Jumps to a label.
−
    The goto statement lets you jump about in the code.
 
−
    Labels in your DIL programs, for 'goto' or interrupts
 
−
    are defined within ':'. For an example, see the program below.
 
  
−
  '''Example:'''
+
  goto name;
 +
...
 +
:name:
 +
statement
  
−
<nowiki>
+
A label is a name between colons in front of a statement. <code>goto</code> carries on from there. It can jump forwards and backwards, and out of loops and blocks. The usual DIL program is a loop: a label, a <code>wait</code> or <code>pause</code>, the work, and <code>goto</code> back to the label.
  
−
dilbegin foo();
+
== Notes ==
−
  code
+
* A label must be followed by a statement: <code>:done: }</code> is a syntax error. Write <code>:done: quit;</code> or <code>:done: return;</code>.
−
dilend</nowiki>
+
* Labels belong to one template (<code>dilbegin ... dilend</code>); a goto can't leave it.
 +
* vmc refuses a label that is never defined ("Undefined label: ...") and one defined twice ("Redefinition of label"). Names ignore case: <code>:Start:</code> and <code>:start:</code> are the same label.
 +
* Several labels may stand in front of one statement.
 +
* A loop that never waits (no <code>wait</code>, <code>pause</code> or other wait) is stopped by the server: it logs "DIL ... had endless loop." in the zone's log and removes the program.
 +
* Labels are also used by <code>on ... goto</code>, <code>interrupt()</code>, <code>on_activation()</code> and <code>secure()</code>.
  
−
---~---~---~---~---~---~---~---~---
+
== Example ==
−
<span id="dcgoto"></span>
+
<pre>
−
'''goto:'''
+
dilbegin fish_monger();
−
    The goto statement lets you jump about in the code.
+
code
−
    Labels in your DIL programs, for 'goto' or interrupts
+
{
−
    are defined within ':'. For an example, see the program below.
+
  heartbeat := PULSE_SEC * 60;
  
−
'''Example:'''
+
  :loop:
 +
  pause;
 +
  if (self.position < POSITION_STANDING)
 +
      goto loop;
 +
  exec("say Fresh fish! Fresh fish!", self);
 +
  goto loop;
 +
}
 +
dilend
 +
</pre>
 +
An NPC that calls out once a minute while standing.
  
−
<nowiki>
+
== See also ==
−
 
+
* [[Manual:DIL Manual/on()|on()]]
−
dilbegin foo();
+
* [[Manual:DIL Manual/if()|if()]]
−
  code
+
* [[Manual:DIL Manual/while()|while()]]
−
dilend</nowiki>
+
* [[Manual:DIL Manual/wait()|wait()]]
−
 
+
* [[Manual:DIL Manual/secure()|secure()]]
−
---~---~---~---~---~---~---~---~---
+
* [[Manual:DIL Manual/interrupt()|interrupt()]]
 +
* [[Manual:DIL Manual/DIL Constructs|DIL Constructs]]

Latest revision as of 23:23, 14 November 2026

goto

Instruction. Jumps to a label.

goto name;
...
:name:
statement

A label is a name between colons in front of a statement. goto carries on from there. It can jump forwards and backwards, and out of loops and blocks. The usual DIL program is a loop: a label, a wait or pause, the work, and goto back to the label.

Notes

  • A label must be followed by a statement: :done: } is a syntax error. Write :done: quit; or :done: return;.
  • Labels belong to one template (dilbegin ... dilend); a goto can't leave it.
  • vmc refuses a label that is never defined ("Undefined label: ...") and one defined twice ("Redefinition of label"). Names ignore case: :Start: and :start: are the same label.
  • Several labels may stand in front of one statement.
  • A loop that never waits (no wait, pause or other wait) is stopped by the server: it logs "DIL ... had endless loop." in the zone's log and removes the program.
  • Labels are also used by on ... goto, interrupt(), on_activation() and secure().

Example

dilbegin fish_monger();
code
{
   heartbeat := PULSE_SEC * 60;

   :loop:
   pause;
   if (self.position < POSITION_STANDING)
      goto loop;
   exec("say Fresh fish! Fresh fish!", self);
   goto loop;
}
dilend

An NPC that calls out once a minute while standing.

See also