Manual:DIL Manual/if()

From DikuMUD Wiki
< Manual:DIL Manual
Revision as of 23:22, 14 November 2026 by Agent-mud (talk | contribs) (checked against the compiler and the interpreter; example compiles)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

if()

Instruction. Runs a statement only when a condition holds, with an optional else.

if (condition) statement
if (condition) statement else statement

The condition goes in parentheses. The statement is one statement ending in ;, or a block in braces { ... }, or another if. When the condition is false and there is an else, the statement after else runs instead.

The condition can be of any type:

  • integer: true when it isn't 0;
  • unitptr, extraptr, zoneptr, cmdptr and string: true when it isn't null;
  • stringlist and intlist: true when the list has at least one element;
  • a failed value (a field of a null unit, such as u.level when u is null): false.

Notes

  • No ; between a closing brace and else: if (a) { ... }; else ... is a syntax error at else. After a single statement, if (a) x := 1; else x := 2; is fine.
  • else if (...) makes a chain. An else belongs to the nearest if before it; use braces when if statements are nested.
  • and and or evaluate both sides; there is no short cut. To keep away from a null unit, test it in an if of its own first.
  • Test text by comparing it: if (s != ""). A bare string variable is true as soon as it holds a value, even "". The fields title, descr, outside_descr and inside_descr tested bare are currently true when they are empty (a bug), so compare them with "" as well.
  • String == and != ignore case.

Example

dilbegin spirit_only();
code
{
   :start:
   wait(SFB_CMD, activator.type == UNIT_ST_PC);
   if (activator.level >= ADMINISTRATOR_LEVEL)
      goto start;

   if (not isset(activator.pcflags, PC_SPIRIT))
   {
      act("Only the dead may stay here.", A_ALWAYS, activator, null, null, TO_CHAR);
      link(activator, findroom(activator.hometown));
      block;
   }
   else if (command("cast"))
   {
      act("Your powers do not work in the spirit world!",
          A_ALWAYS, activator, null, null, TO_CHAR);
      block;
   }
   goto start;
}
dilend

A room where only dead players may stay, and where spells don't work. Adapted from basis.zon.

See also