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

From DikuMUD Wiki
Jump to navigation Jump to search
(XML import)
(checked against the compiler and the interpreter; example compiles)
 
Line 1: Line 1:
−
<span id="forea"></span>
+
= foreach() =
−
'''foreach:'''
+
'''Instruction.''' Runs a statement once for each unit of some types around the program's unit.
−
  Foreach is an easy way to process all the units in the
 
−
  local environment relative to the 'self' executing the
 
−
  foreach. Foreach takes care of creating a list of local
 
−
  units, and of securing them. You can use both break and
 
−
  continue in a foreach statement. The unit executing the
 
−
  foreach ('self') is always a part of the foreach.
 
  
−
      It is important to understand that the units in the local
+
foreach (types : integer, u : unitptr)
−
      environment are relative to the 'self' executing the foreach.
+
    statement
  
−
'''Example:'''
+
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.
−
    This foreach is copied onto the spell caster, and hence all units
 
−
    relative to the spell caster (i.e. self) are processed in the foreach.
 
−
    Assume that it was executed on the spell caster's staff, then all units
 
−
    found would be relative to the staff, i.e. the spell caster's inventory.
 
  
−
<nowiki>
+
"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.
−
    foreach (UNIT_ST_PC|UNIT_ST_NPC, u)
 
−
...</nowiki>
 
−
<span id="forea"></span>
 
−
'''foreach:'''
 
−
  Foreach is an easy way to process all the units in the
 
−
  local environment relative to the 'self' executing the
 
−
  foreach. Foreach takes care of creating a list of local
 
−
  units, and of securing them. You can use both break and
 
−
  continue in a foreach statement. The unit executing the
 
−
  foreach ('self') is always a part of the foreach.
 
  
−
      It is important to understand that the units in the local
+
== Parameters ==
−
      environment are relative to the 'self' executing the foreach.
+
{| 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.
 +
|}
  
−
'''Example:'''
+
== Notes ==
−
    This foreach is copied onto the spell caster, and hence all units
+
* [[Manual:DIL Manual/break|break]] leaves the loop; [[Manual:DIL Manual/continue|continue]] goes on with the next unit.
−
    relative to the spell caster (i.e. self) are processed in the foreach.
+
* The units come in no particular order.
−
    Assume that it was executed on the spell caster's staff, then all units
+
* 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.
−
    found would be relative to the staff, i.e. the spell caster's inventory.
+
* <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.
  
−
<nowiki>
+
== 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 ==
−
    foreach (UNIT_ST_PC|UNIT_ST_NPC, u)
+
* [[Manual:DIL Manual/while()|while()]]
−
...</nowiki>
+
* [[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 foreach can't be inside another in the same template (vmc: "'foreach' may not be nested!"). Use a while for the inner loop.
  • u must be a unitptr variable. vmc doesn't check it: with another type the program stops at run time with "Type error in foreach - next".
  • u itself isn't secured: after a wait() or pause inside 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.

See also