Difference between revisions of "Manual:DIL Manual/self"

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="self"></span>
+
= self =
−
'self'
+
'''Variable''' (unitptr, read-only).
−
    This variable is a unitptr to the unit owning the DIL program.
 
−
    For C++ people, this is like the 'this' pointer.
 
−
    For example, if Mary has a program, then self.title equals
 
−
    "Mary the Lumberjack wife"
 
  
−
  ---~---~---~---~---~---~---~---~---
+
  self : unitptr
−
<span id="self"></span>
 
−
'self'
 
−
    This variable is a unitptr to the unit owning the DIL program.
 
−
    For C++ people, this is like the 'this' pointer.
 
−
    For example, if Mary has a program, then self.title equals
 
−
    "Mary the Lumberjack wife"
 
  
−
---~---~---~---~---~---~---~---~---
+
The unit the program runs on:
 +
* a template copied with <code>dilcopy</code>: the unit it was copied onto;
 +
* a <code>dilbegin</code> written inside a unit's definition: that unit;
 +
* a player command written in DIL (<code>func =</code> in commands.def): the character who typed it;
 +
* a spell's DIL: the caster;
 +
* a function called as <code>name@zone</code>: the caller's <code>self</code>, since it runs inside the caller's program.
 +
 
 +
== Notes ==
 +
* <code>self</code> is never cleared by a wait or a pause and needs no secure(). Unit variables are cleared at every wake; <code>self</code> isn't one.
 +
* It can't be assigned (<code>self := u</code> is "Illegal lvalue").
 +
* When the unit is destroyed, its programs end with it. A program that destroys its own unit should <code>quit</code> right after <code>destroy(self)</code>, as death.zon's rot_head does.
 +
* The usual tests: <code>self.position</code> (asleep, fighting), <code>self.outside</code> (where it is), <code>exec("say ...", self)</code> to make an NPC act.
 +
 
 +
== Example ==
 +
<pre>
 +
dilbegin crier();
 +
code
 +
{
 +
  heartbeat := PULSE_SEC * 60;
 +
 
 +
  :start:
 +
  wait(SFB_TICK, TRUE);
 +
  if (self.position != POSITION_STANDING)
 +
      goto start;
 +
  exec("say Hear ye! " + self.name + " brings news to " + self.outside.title + "!", self);
 +
  goto start;
 +
}
 +
dilend
 +
</pre>
 +
An NPC that, once a minute when standing, calls out its own name and the title of the room it is in.
 +
 
 +
== See also ==
 +
* [[Manual:DIL Manual/activator|activator]]
 +
* [[Manual:DIL Manual/exec()|exec()]]
 +
* [[Manual:DIL Manual/dilcopy()|dilcopy()]]
 +
* [[Manual:DIL Manual/Built-In Variables|Built-In Variables]]

Latest revision as of 23:18, 14 November 2026

self

Variable (unitptr, read-only).

self : unitptr

The unit the program runs on:

  • a template copied with dilcopy: the unit it was copied onto;
  • a dilbegin written inside a unit's definition: that unit;
  • a player command written in DIL (func = in commands.def): the character who typed it;
  • a spell's DIL: the caster;
  • a function called as name@zone: the caller's self, since it runs inside the caller's program.

Notes

  • self is never cleared by a wait or a pause and needs no secure(). Unit variables are cleared at every wake; self isn't one.
  • It can't be assigned (self := u is "Illegal lvalue").
  • When the unit is destroyed, its programs end with it. A program that destroys its own unit should quit right after destroy(self), as death.zon's rot_head does.
  • The usual tests: self.position (asleep, fighting), self.outside (where it is), exec("say ...", self) to make an NPC act.

Example

dilbegin crier();
code
{
   heartbeat := PULSE_SEC * 60;

   :start:
   wait(SFB_TICK, TRUE);
   if (self.position != POSITION_STANDING)
      goto start;
   exec("say Hear ye! " + self.name + " brings news to " + self.outside.title + "!", self);
   goto start;
}
dilend

An NPC that, once a minute when standing, calls out its own name and the title of the room it is in.

See also