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

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="bfghead"></span>
+
= ghead() =
−
unitptr ghead ( )
+
'''Function''' (unitptr). The first unit in the game's global unit list.
  
−
  return the first unit in the global list which will be the last
+
  ghead()
−
char to have logged on.
 
  
−
---~---~---~---~---~---~---~---~---
+
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 <code>gnext</code> is the next one in the list, and null after the last room.
−
<span id="bfghead"></span>
 
−
unitptr ghead ( )
 
  
−
return the first unit in the global list which will be the last
+
<code>ghead()</code> is the start of that list: the player who entered the game last, or the first NPC when no players are on. It is mostly used to walk through the players, stopping at the first unit that isn't one.
−
char to have logged on.
 
  
−
---~---~---~---~---~---~---~---~---
+
== Returns ==
 +
Unitptr: the first unit of the list.
 +
 
 +
== Notes ==
 +
* The walk for one kind stops at the first unit of another kind: <code>while (u.type == UNIT_ST_PC)</code>. A field of null fails, and a failed comparison is false, so the loop also ends cleanly at the end of the list.
 +
* The other sections start at <code>pc_head()</code>, <code>npc_head()</code>, <code>obj_head()</code> and <code>room_head()</code>. <code>global_head()</code> is another name for <code>ghead()</code>.
 +
* To find a player by exact name, walk the players like this. <code>findunit()</code> matches partial names and only finds what the searcher can see.
 +
* A program runs at most 52,000 instructions between two waits (<code>WAITCMD_MAXINST</code> in <code>vme/src/dil.h</code>); past that it is stopped with "had endless loop" in <code>vme.log</code>. 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 <code>secure</code>d.
 +
 
 +
== Example ==
 +
<pre>
 +
dilbegin unitptr find_player(pname : string);
 +
var
 +
  pc : unitptr;
 +
code
 +
{
 +
  pc := ghead();
 +
  while ((pc.type == UNIT_ST_PC) and (pc.name != pname))
 +
      pc := pc.gnext;
 +
  if (pc.type != UNIT_ST_PC)
 +
      pc := null;
 +
  return (pc);
 +
}
 +
dilend
 +
</pre>
 +
Finds a player in the game by name (the comparison ignores case), or returns null.
 +
 
 +
== See also ==
 +
* [[Manual:DIL Manual/gnext|gnext]]
 +
* [[Manual:DIL Manual/gprevious|gprevious]]
 +
* [[Manual:DIL Manual/pc head()|pc_head()]]
 +
* [[Manual:DIL Manual/npc head()|npc_head()]]
 +
* [[Manual:DIL Manual/obj head()|obj_head()]]
 +
* [[Manual:DIL Manual/room head()|room_head()]]
 +
* [[Manual:DIL Manual/global head()|global_head()]]
 +
* [[Manual:DIL Manual/findunit()|findunit()]]

Latest revision as of 23:15, 14 November 2026

ghead()

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

ghead()

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.

ghead() is the start of that list: the player who entered the game last, or the first NPC when no players are on. It is mostly used to walk through the players, stopping at the first unit that isn't one.

Returns

Unitptr: the first unit of the list.

Notes

  • The walk for one kind stops at the first unit of another kind: while (u.type == UNIT_ST_PC). A field of null fails, and a failed comparison is false, so the loop also ends cleanly at the end of the list.
  • The other sections start at pc_head(), npc_head(), obj_head() and room_head(). global_head() is another name for ghead().
  • To find a player by exact name, walk the players like this. findunit() matches partial names and only finds what the searcher can see.
  • 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.

Example

dilbegin unitptr find_player(pname : string);
var
   pc : unitptr;
code
{
   pc := ghead();
   while ((pc.type == UNIT_ST_PC) and (pc.name != pname))
      pc := pc.gnext;
   if (pc.type != UNIT_ST_PC)
      pc := null;
   return (pc);
}
dilend

Finds a player in the game by name (the comparison ignores case), or returns null.

See also