Manual:DIL Manual/obj head()

From DikuMUD Wiki
Jump to navigation Jump to search

obj_head()

Function (unitptr). The first object in the game's global unit list.

obj_head()

The global list holds every unit in the game, kept in this order: all players (the one who entered the game last first), then all NPCs, then all objects, then all rooms. Each unit's gnext is the next one in the list, and null after the last room.

obj_head() returns the first object, the one loaded last, or null when there are no objects. Follow gnext while the unit is an object to visit them all.

Returns

Unitptr: the first object, or null if there is none.

Notes

  • Stop at the first unit that isn't an object: while (u.type == UNIT_ST_OBJ). After the last object come the rooms.
  • A program runs at most 52,000 instructions between two waits (WAITCMD_MAXINST in vme/src/dil.h); past that it is stopped with "had endless loop" in vme.log. The game holds many thousands of objects, NPCs and rooms, so a loop of a few instructions per unit over a whole section can run into that limit. You can't pause half-way either: unit variables are cleared at every wait unless secured.
  • To find a particular object, findsymbolic("name@zone") is much cheaper than a walk.

Example

dilbegin integer obj_count(zn : string);
var
   u : unitptr;
   n : integer;
code
{
   u := obj_head();
   while (u.type == UNIT_ST_OBJ)
   {
      if (u.zoneidx == zn)
         n := n + 1;
      u := u.gnext;
   }
   return (n);
}
dilend

Counts the objects in the game that come from zone zn. In a big game, mind the instruction limit above.

See also