Manual:DIL Manual/unitptr

From DikuMUD Wiki
< Manual:DIL Manual
Revision as of 23:27, 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

unitptr

Type. A pointer to a unit: a room, an object, an NPC or a player.

var
   u : unitptr;

Through a unitptr a program reads and changes a unit's fields (u.name, u.hp, u.outside...), and hands the unit to functions and procedures. u.type says which kind of unit it is: UNIT_ST_ROOM, UNIT_ST_OBJ, UNIT_ST_NPC or UNIT_ST_PC. Which fields a unit has depends on that type.

A unitptr is null or points to a unit. Built-in variables such as self, activator, medium and target are unitptrs, and so are the results of findunit(), findroom(), findsymbolic() and load().

Notes

  • A unitptr variable is cleared at every wait. When the program wakes from wait, pause or a heartbeat, every unitptr variable is null again, unless that unit is secured. Find the unit again after waiting, or secure it. self is always the program's own unit.
  • A field of a null unit fails, and a failed value is false in every comparison: u.level > 5 and u.level <= 5 are both false when u is null. Test u == null (or not u) first, on its own: and doesn't stop early.
  • A field on a call's result is a syntax error: findroom(s).title doesn't compile. Put the unit in a variable first.
  • == and != tell whether two unitptrs point to the same unit.
  • Units in a room (or carried, or in a container) form a list: u.inside is the first one, .next the next. When the loop moves or destroys units, use foreach instead.
  • unitptr variables are not saved with a unit: after loading they are null.

Example

dilbegin do_count(arg : string);
var
   u : unitptr;
   pcs, others, things : integer;
code
{
   u := self.outside.inside;
   while (u != null)
   {
      if (u.type == UNIT_ST_PC)
         pcs := pcs + 1;
      else if (u.type == UNIT_ST_NPC)
         others := others + 1;
      else if (u.type == UNIT_ST_OBJ)
         things := things + 1;
      u := u.next;
   }
   sendtext("Here: " + itoa(pcs) + " players, " + itoa(others) + " others and "
            + itoa(things) + " things.<br/>", self);
   quit;
}
dilend

A command that counts the players, NPCs and objects in the room (npcs and objs are reserved words, so the variables have other names).

See also