Difference between revisions of "Manual:DIL Manual/unitptr"
Jump to navigation
Jump to search
(XML import) |
(checked against the compiler and the interpreter; example compiles) |
||
| Line 1: | Line 1: | ||
| − | + | = 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 [[Manual:DIL Manual/Fields|fields]] (<code>u.name</code>, <code>u.hp</code>, <code>u.outside</code>...), and hands the unit to functions and procedures. <code>u.type</code> says which kind of unit it is: <code>UNIT_ST_ROOM</code>, <code>UNIT_ST_OBJ</code>, <code>UNIT_ST_NPC</code> or <code>UNIT_ST_PC</code>. Which fields a unit has depends on that type. | |
| − | |||
| − | |||
| − | |||
| − | < | ||
| − | |||
| − | + | A unitptr is <code>null</code> or points to a unit. Built-in variables such as <code>self</code>, <code>activator</code>, <code>medium</code> and <code>target</code> are unitptrs, and so are the results of <code>findunit()</code>, <code>findroom()</code>, <code>findsymbolic()</code> and <code>load()</code>. | |
| − | + | ||
| − | + | == Notes == | |
| − | + | * '''A unitptr variable is cleared at every wait.''' When the program wakes from <code>wait</code>, <code>pause</code> or a heartbeat, every unitptr variable is null again, unless that unit is <code>secure</code>d. Find the unit again after waiting, or secure it. <code>self</code> is always the program's own unit. | |
| − | + | * A field of a null unit fails, and a failed value is false in every comparison: <code>u.level > 5</code> and <code>u.level <= 5</code> are both false when <code>u</code> is null. Test <code>u == null</code> (or <code>not u</code>) first, on its own: <code>and</code> doesn't stop early. | |
| − | + | * A field on a call's result is a syntax error: <code>findroom(s).title</code> doesn't compile. Put the unit in a variable first. | |
| − | + | * <code>==</code> and <code>!=</code> tell whether two unitptrs point to the same unit. | |
| − | + | * Units in a room (or carried, or in a container) form a list: <code>u.inside</code> is the first one, <code>.next</code> the next. When the loop moves or destroys units, use <code>foreach</code> instead. | |
| − | + | * unitptr variables are not saved with a unit: after loading they are null. | |
| − | + | ||
| − | + | == Example == | |
| − | + | <pre> | |
| − | + | 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 | |
| − | + | </pre> | |
| − | + | A command that counts the players, NPCs and objects in the room (<code>npcs</code> and <code>objs</code> are reserved words, so the variables have other names). | |
| − | + | ||
| − | + | == See also == | |
| − | + | * [[Manual:DIL Manual/Fields|Fields]] | |
| − | + | * [[Manual:DIL Manual/null|null]] | |
| − | + | * [[Manual:DIL Manual/self|self]] | |
| − | + | * [[Manual:DIL Manual/secure()|secure()]] | |
| − | + | * [[Manual:DIL Manual/foreach()|foreach()]] | |
| − | + | * [[Manual:DIL Manual/findunit()|findunit()]] | |
| − | + | * [[Manual:DIL Manual/type|type]] | |
| − | + | * [[Manual:DIL Manual/inside|inside]] | |
| − | + | * [[Manual:DIL Manual/next|next]] | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
Latest revision as of 23:27, 14 November 2026
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).