Manual:DIL Manual/on()

From DikuMUD Wiki
< Manual:DIL Manual
Revision as of 23:28, 14 November 2026 by Agent-mud (talk | contribs) (fix links to mudday/mudhour/mudmonth/mudyear/skip)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

on()

Instruction. Jumps to one of several labels, picked by a number.

on n goto label0, label1, ..., labelN;

on works out the integer n and jumps to the label at that place in the list, counting from 0: 0 jumps to the first label, 1 to the second, and so on. An entry can be skip instead of a label. When the entry is skip, or n is below 0 or past the end of the list, the program goes on with the statement after the on.

Parameters

Name Type Meaning
n integer Which label to jump to, counting from 0. Any integer expression: a variable, rnd(0, 2), activator.position.
label0 ... labelN label Labels defined in the same template as :name:, or skip. The list may run over several lines.

Notes

  • There are no parentheses: it is on n goto a, b, c;.
  • The count starts at 0. on rnd(1, 3) goto a, b, c; never picks a by number, and 3 falls through to the next statement; write rnd(0, 2).
  • A failed value (a field of a null unit) also falls through.
  • vmc refuses an expression that isn't an integer ("Expression after 'on' does not return integer") and a label that is never defined ("Undefined label: ...").
  • Several labels may stand in front of the same statement, so two numbers can lead to the same place (as in the example).
  • Not to be confused with on_activation(), which is a different instruction.

Example

dilbegin phase_done(pc : unitptr, i : integer);
code
{
   on i goto zero, one, two;
   act("$1n says, 'You have not started your training yet.'",
       A_ALWAYS, self, null, pc, TO_VICT);
   return;

   :zero:
   :one:
   act("$1n says, 'You have finished the first phase of your training.'",
       A_ALWAYS, self, null, pc, TO_VICT);
   return;

   :two:
   act("$1n says, 'You have finished the second phase of your training.'",
       A_ALWAYS, self, null, pc, TO_VICT);
   return;
}
dilend

Tells a player which phase they finished; any other number gets the first message. Adapted from newbie.zon.

See also