Manual:DIL Manual/on ... goto ...

From DikuMUD Wiki
< Manual:DIL Manual
Revision as of 13:08, 27 May 2020 by Nove (talk | contribs) (XML import)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

on n goto la, lb, ..., ln:
    This construct is an easy way of performing a goto operation
    based on the result of an integer. The integer value 'n' must
    be zero or positive and less than the number of labels specified
    in the label-list. If n is outside this range, the on-goto operation
    is skipped and execution continues at the next instruction.
    Based on the value of 'n' execution continues at the label
    corresponding to number 'n' in the list. I.e. if n is 0, then
    execution continues at the first specified label, if n is 1
    then at the second, etc. etc.

  Assume you have an integer 'i' larger than zero, which takes
  on 0, 1, 2, 3, 4 or 5. Based on each value you need to take a
  different action, this is how you can do it:

     on i goto grin, laugh, grin, nada, poke, laugh;
     log("Value was not in the range 0..5");

       exec("grin", self);
       goto ...;

       exec("cackle", self);
       goto ...;

       exec("cry", self);
       goto ...;

       exec("smirk", self);
       goto ...;
It is often used in this context

     on rnd(0,4) goto l1, l2, l3, l4, l5;