Difference between revisions of "Manual:DIL Manual/if()"

From DikuMUD Wiki
Jump to navigation Jump to search
(XML import)
 
(checked against the compiler and the interpreter; example compiles)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
 +
= if() =
 +
'''Instruction.''' Runs a statement only when a condition holds, with an optional <code>else</code>.
  
 +
if (condition) statement
 +
if (condition) statement else statement
  
−
<span id="dcif"></span>
+
The condition goes in parentheses. The statement is one statement ending in <code>;</code>, or a block in braces <code>{ ... }</code>, or another <code>if</code>. When the condition is false and there is an <code>else</code>, the statement after <code>else</code> runs instead.
−
'''if:'''
 
−
    The if statement is much like C. It takes any type as
 
−
    argument. Non integers are considered 'TRUE' if they are
 
−
    not null.
 
  
−
'''Example:'''
+
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 <code>u.level</code> when <code>u</code> is null): false.
  
−
<nowiki>
+
== Notes ==
 +
* No <code>;</code> between a closing brace and <code>else</code>: <code>if (a) { ... }; else ...</code> is a syntax error at <code>else</code>. After a single statement, <code>if (a) x := 1; else x := 2;</code> is fine.
 +
* <code>else if (...)</code> makes a chain. An <code>else</code> belongs to the nearest <code>if</code> before it; use braces when <code>if</code> statements are nested.
 +
* <code>and</code> and <code>or</code> evaluate both sides; there is no short cut. To keep away from a null unit, test it in an <code>if</code> of its own first.
 +
* Test text by comparing it: <code>if (s != "")</code>. A bare string variable is true as soon as it holds a value, even <code>""</code>. The fields <code>title</code>, <code>descr</code>, <code>outside_descr</code> and <code>inside_descr</code> tested bare are currently true when they are '''empty''' (a bug), so compare them with <code>""</code> as well.
 +
* String <code>==</code> and <code>!=</code> ignore case.
  
−
  dilbegin foo();
+
== Example ==
−
  code
+
<pre>
−
  {
+
dilbegin spirit_only();
−
    if (self.hp>10)
+
code
−
    {
+
{
−
      exec("say Hehe!",self);
+
  :start:
−
    }
+
  wait(SFB_CMD, activator.type == UNIT_ST_PC);
−
    else
+
  if (activator.level >= ADMINISTRATOR_LEVEL)
−
    {
+
      goto start;
−
      exec("say ouch!", self);
 
−
    }
 
−
  }
 
−
  dilend</nowiki>
 
−
 
 
−
'''Example:'''
 
  
−
<nowiki>
+
   if (not isset(activator.pcflags, PC_SPIRIT))
−
 
 
−
   dilbegin foo();
 
−
  code
 
 
   {
 
   {
−
    if (self.loaded>10)
+
      act("Only the dead may stay here.", A_ALWAYS, activator, null, null, TO_CHAR);
−
    {
+
      link(activator, findroom(activator.hometown));
−
      exec("say its getting crowded!",self);
+
      block;
−
    }
 
 
   }
 
   }
−
   dilend</nowiki>
+
   else if (command("cast"))
−
 
 
−
'''Example:'''
 
−
 
 
−
<nowiki>
 
−
 
 
−
  dilbegin foo();
 
−
  code
 
 
   {
 
   {
−
    if (self.loaded<10)
+
      act("Your powers do not work in the spirit world!",
−
      exec("say plenty of room!",self);
+
          A_ALWAYS, activator, null, null, TO_CHAR);
 +
      block;
 
   }
 
   }
−
   dilend</nowiki>
+
   goto start;
 +
}
 +
dilend
 +
</pre>
 +
A room where only dead players may stay, and where spells don't work. Adapted from basis.zon.
  
−
---~---~---~---~---~---~---~---~---
+
== See also ==
 +
* [[Manual:DIL Manual/else()|else()]]
 +
* [[Manual:DIL Manual/while()|while()]]
 +
* [[Manual:DIL Manual/goto|goto]]
 +
* [[Manual:DIL Manual/on()|on()]]
 +
* [[Manual:DIL Manual/Operators|Operators]]
 +
* [[Manual:DIL Manual/DIL Constructs|DIL Constructs]]

Latest revision as of 23:22, 14 November 2026

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