Manual:DIL Manual/unitptr
Jump to navigation
Jump to search
Contents
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,pauseor a heartbeat, every unitptr variable is null again, unless that unit issecured. Find the unit again after waiting, or secure it.selfis always the program's own unit. - A field of a null unit fails, and a failed value is false in every comparison:
u.level > 5andu.level <= 5are both false whenuis null. Testu == null(ornot u) first, on its own:anddoesn't stop early. - A field on a call's result is a syntax error:
findroom(s).titledoesn'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.insideis the first one,.nextthe next. When the loop moves or destroys units, useforeachinstead. - 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).