Manual:DIL Manual/if()
Jump to navigation
Jump to search
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.