Difference between revisions of "Manual:DIL Manual/if()"
Jump to navigation
Jump to search
(XML import) |
(checked against the compiler and the interpreter; example compiles) |
||
| 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 | ||
| − | + | 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. | |
| − | + | 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. | ||
| − | + | == 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. | ||
| − | + | == Example == | |
| + | <pre> | ||
| + | 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)) | |
| − | + | { | |
| − | dilend</ | + | 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 | ||
| + | </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
Contents
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.levelwhenuis null): false.
Notes
- No
;between a closing brace andelse:if (a) { ... }; else ...is a syntax error atelse. After a single statement,if (a) x := 1; else x := 2;is fine. else if (...)makes a chain. Anelsebelongs to the nearestifbefore it; use braces whenifstatements are nested.andandorevaluate both sides; there is no short cut. To keep away from a null unit, test it in anifof 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 fieldstitle,descr,outside_descrandinside_descrtested 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.