Difference between revisions of "Manual:DIL Manual/foreach()"
(XML import) |
(checked against the compiler and the interpreter; example compiles) |
||
| Line 1: | Line 1: | ||
| − | + | = foreach() = | |
| − | + | '''Instruction.''' Runs a statement once for each unit of some types around the program's unit. | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | + | foreach (types : integer, u : unitptr) | |
| − | + | statement | |
| − | + | When the loop starts, it lists the units around the program's own unit whose type is in <code>types</code> (<code>UNIT_ST_PC</code>, <code>UNIT_ST_NPC</code>, <code>UNIT_ST_OBJ</code>, <code>UNIT_ST_ROOM</code>, combined with <code>|</code>). Then it puts each in turn in <code>u</code> and runs the statement, usually a <code>{ ... }</code> block. | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | + | "Around" means the units in the unit that holds the program's unit: for an NPC or object in a room, the units in the room; for an object a character carries, the character's inventory and equipment; for a room, the units in it. It also lists the units inside transparent units there (an open container or room with <code>UNIT_FL_TRANS</code>), but not what characters carry. The program's own unit is in the list too when its type is one of <code>types</code>: test <code>u == self</code> to skip it. | |
| − | + | The list is a snapshot, and its units are watched like secured ones: a unit that has left or died before its turn is skipped. So the statement can move, kill or destroy units safely, which a <code>while</code> over <code>.next</code> can't do. | |
| − | |||
| − | |||
| − | < | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | + | == Parameters == | |
| − | + | {| class="wikitable" | |
| + | ! Name !! Type !! Meaning | ||
| + | |- | ||
| + | | types || integer || The <code>UNIT_ST_</code> types to list. 0 lists nothing. | ||
| + | |- | ||
| + | | u || unitptr || The variable that gets each unit. | ||
| + | |} | ||
| − | + | == Notes == | |
| − | + | * [[Manual:DIL Manual/break|break]] leaves the loop; [[Manual:DIL Manual/continue|continue]] goes on with the next unit. | |
| − | + | * The units come in no particular order. | |
| − | + | * One <code>foreach</code> can't be inside another in the same template (vmc: "'foreach' may not be nested!"). Use a <code>while</code> for the inner loop. | |
| − | + | * <code>u</code> must be a <code>unitptr</code> variable. vmc doesn't check it: with another type the program stops at run time with "Type error in foreach - next". | |
| + | * <code>u</code> itself isn't secured: after a <code>wait()</code> or <code>pause</code> inside the loop it is null, like any unit variable. The loop goes on with the next unit all the same. | ||
| − | + | == Example == | |
| + | <pre> | ||
| + | dilbegin closing_time(); | ||
| + | var | ||
| + | u : unitptr; | ||
| + | street : unitptr; | ||
| + | code | ||
| + | { | ||
| + | street := findroom("street@ex"); | ||
| + | if (street == null) | ||
| + | quit; | ||
| + | foreach (UNIT_ST_PC, u) | ||
| + | { | ||
| + | if (u.level >= IMMORTAL_LEVEL) | ||
| + | continue; | ||
| + | act("$1n throws you out.", A_ALWAYS, self, null, u, TO_VICT); | ||
| + | link(u, street); | ||
| + | exec("look", u); | ||
| + | } | ||
| + | quit; | ||
| + | } | ||
| + | dilend | ||
| + | </pre> | ||
| + | An innkeeper throws every mortal player out into the street. Moving the players inside the loop is safe because it is a <code>foreach</code>. | ||
| − | + | == See also == | |
| − | + | * [[Manual:DIL Manual/while()|while()]] | |
| − | + | * [[Manual:DIL Manual/break|break]] | |
| + | * [[Manual:DIL Manual/continue|continue]] | ||
| + | * [[Manual:DIL Manual/secure()|secure()]] | ||
Latest revision as of 23:16, 14 November 2026
foreach()
Instruction. Runs a statement once for each unit of some types around the program's unit.
foreach (types : integer, u : unitptr) statement
When the loop starts, it lists the units around the program's own unit whose type is in types (UNIT_ST_PC, UNIT_ST_NPC, UNIT_ST_OBJ, UNIT_ST_ROOM, combined with |). Then it puts each in turn in u and runs the statement, usually a { ... } block.
"Around" means the units in the unit that holds the program's unit: for an NPC or object in a room, the units in the room; for an object a character carries, the character's inventory and equipment; for a room, the units in it. It also lists the units inside transparent units there (an open container or room with UNIT_FL_TRANS), but not what characters carry. The program's own unit is in the list too when its type is one of types: test u == self to skip it.
The list is a snapshot, and its units are watched like secured ones: a unit that has left or died before its turn is skipped. So the statement can move, kill or destroy units safely, which a while over .next can't do.
Parameters
| Name | Type | Meaning |
|---|---|---|
| types | integer | The UNIT_ST_ types to list. 0 lists nothing.
|
| u | unitptr | The variable that gets each unit. |
Notes
- break leaves the loop; continue goes on with the next unit.
- The units come in no particular order.
- One
foreachcan't be inside another in the same template (vmc: "'foreach' may not be nested!"). Use awhilefor the inner loop. umust be aunitptrvariable. vmc doesn't check it: with another type the program stops at run time with "Type error in foreach - next".uitself isn't secured: after await()orpauseinside the loop it is null, like any unit variable. The loop goes on with the next unit all the same.
Example
dilbegin closing_time();
var
u : unitptr;
street : unitptr;
code
{
street := findroom("street@ex");
if (street == null)
quit;
foreach (UNIT_ST_PC, u)
{
if (u.level >= IMMORTAL_LEVEL)
continue;
act("$1n throws you out.", A_ALWAYS, self, null, u, TO_VICT);
link(u, street);
exec("look", u);
}
quit;
}
dilend
An innkeeper throws every mortal player out into the street. Moving the players inside the loop is safe because it is a foreach.