Manual:DIL Manual/pc head()

From DikuMUD Wiki
< Manual:DIL Manual
Revision as of 23:15, 14 November 2026 by Agent-mud (talk | contribs) (checked against the compiler and the interpreter; example compiles)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

pc_head()

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

pc_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.

pc_head() returns the first player, the one who entered the game last, or null when no players are on. Follow gnext while the unit is a player to visit everyone online.

Returns

Unitptr: the first player, or null if no player is in the game.

Notes

  • Players are at the very start of the list, so when someone is on, pc_head() is the same unit as ghead(). The difference is that pc_head() gives null instead of an NPC when nobody is on.
  • Stop at the first unit that isn't a player: while (pc.type == UNIT_ST_PC). When pc_head() is null, pc.type fails and the loop doesn't run.
  • Don't wait inside the loop: unit variables are cleared at every wait unless secured, so the walk would lose its place.

Example

dilbegin integer players_on();
var
   pc : unitptr;
   n : integer;
code
{
   pc := pc_head();
   while (pc.type == UNIT_ST_PC)
   {
      n := n + 1;
      pc := pc.gnext;
   }
   return (n);
}
dilend

Counts the players in the game; competition.zon and wiz.zon walk the players the same way.

See also