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

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="bpsec"></span>
+
= secure() =
−
  secure ( u : unitptr , label )
+
'''Procedure.''' Keeps a unit in a variable across waits, and names where to go when it leaves.
−
    u : Unit to secure.
+
 
−
    label : Label to jump to, on removal.
+
  secure(u : unitptr, label)
−
    result: Secures a unitptr, so that the program will go to 'label'
+
 
−
            if unit leaves local environment. If this happens, during
+
DIL clears every <code>unitptr</code> variable at each wake (after every <code>wait()</code>, <code>pause</code> and heartbeat), because the unit may have gone in the meantime. A secured unit is the exception: its variables keep it for as long as it stays in the program's surroundings.
−
            a call to another function/procedure, it will continue
+
 
−
            at that label when the function/procedure returns.
+
When the unit leaves them, or is destroyed, the program jumps to <code>label</code>, every variable holding the unit becomes null, and the secure ends. The surroundings are the room the program's unit is in (through whoever carries it), with everything in it and what the characters there carry, but not the inside of containers or of rooms within it, unless those have <code>UNIT_FL_TRANS</code> and are open. That includes the program's own unit walking away from <code>u</code>.
−
            If you perform some kind of call to a template, the removing
+
 
−
            of a unit from the local environment will not have affect.
+
== Parameters ==
−
            until the return from that function, as the program execution
+
{| class="wikitable"
−
            will continue at the designated label after a call. Should
+
! Name !! Type !! Meaning
−
            several secured units leave local environment, the last such
+
|-
−
            event will determine the point of execution upon return.
+
| u || unitptr || A variable holding the unit. It must be one of the program's own variables or parameters: <code>secure(activator, lost)</code> gives "Arg 1 of 'secure' not a user-variable". Copy the built-in into a variable first.
−
<span id="bpsec"></span>
+
|-
−
secure ( u : unitptr , label )
+
| label || label || The label to jump to, written without its colons: <code>secure(pc, lost);</code> for <code>:lost:</code>.
−
    u : Unit to secure.
+
|}
−
    label : Label to jump to, on removal.
+
 
−
    result: Secures a unitptr, so that the program will go to 'label'
+
== Notes ==
−
            if unit leaves local environment. If this happens, during
+
* The secure is tested at each wake, after [[Manual:DIL Manual/send()|send()]], [[Manual:DIL Manual/sendto()|sendto()]] and [[Manual:DIL Manual/exec()|exec()]], and after [[Manual:DIL Manual/destroy()|destroy()]] of the unit. A <code>destroy()</code> of a secured unit therefore jumps to the label at once and skips the rest: call [[Manual:DIL Manual/unsecure()|unsecure()]] first.
−
            a call to another function/procedure, it will continue
+
* A secure belongs to the template call that made it and ends when that call returns. When a called template returns, the caller's secures are tested again, so a unit that left during the call sends the caller to its label then.
−
            at that label when the function/procedure returns.
+
* Securing the same unit again only changes the label. A variable holding null secures nothing.
−
            If you perform some kind of call to a template, the removing
+
* [[Manual:DIL Manual/extraptr|extraptr]] variables can't be secured; they are null after every wake. Find the extra again.
−
            of a unit from the local environment will not have affect.
+
* [[Manual:DIL Manual/foreach()|foreach()]] secures the units it walks over by itself; you don't need to.
−
            until the return from that function, as the program execution
+
 
−
            will continue at the designated label after a call. Should
+
== Example ==
−
            several secured units leave local environment, the last such
+
<pre>
−
            event will determine the point of execution upon return.
+
dilbegin storyteller();
 +
var
 +
  pc : unitptr;
 +
code
 +
{
 +
  heartbeat := PULSE_SEC * 5;
 +
:start:
 +
  wait(SFB_CMD, command("bow") and (activator.type == UNIT_ST_PC));
 +
  pc := activator;
 +
  secure(pc, lost);
 +
  exec("say Welcome, " + pc.name + ". Sit down and listen.", self);
 +
  pause;
 +
  exec("say Long ago, a dragon slept under this hill...", self);
 +
  pause;
 +
  exec("say ...and it sleeps there still.", self);
 +
  unsecure(pc);
 +
  goto start;
 +
 
 +
:lost:
 +
  exec("say Some people have no patience.", self);
 +
  goto start;
 +
}
 +
dilend
 +
</pre>
 +
An NPC tells a story to the player who bowed. <code>pc</code> survives the pauses because it is secured; if the player walks out, the NPC grumbles instead.
 +
 
 +
== See also ==
 +
* [[Manual:DIL Manual/unsecure()|unsecure()]]
 +
* [[Manual:DIL Manual/foreach()|foreach()]]
 +
* [[Manual:DIL Manual/wait()|wait()]]
 +
* [[Manual:DIL Manual/destroy()|destroy()]]

Latest revision as of 23:16, 14 November 2026

secure()

Procedure. Keeps a unit in a variable across waits, and names where to go when it leaves.

secure(u : unitptr, label)

DIL clears every unitptr variable at each wake (after every wait(), pause and heartbeat), because the unit may have gone in the meantime. A secured unit is the exception: its variables keep it for as long as it stays in the program's surroundings.

When the unit leaves them, or is destroyed, the program jumps to label, every variable holding the unit becomes null, and the secure ends. The surroundings are the room the program's unit is in (through whoever carries it), with everything in it and what the characters there carry, but not the inside of containers or of rooms within it, unless those have UNIT_FL_TRANS and are open. That includes the program's own unit walking away from u.

Parameters

Name Type Meaning
u unitptr A variable holding the unit. It must be one of the program's own variables or parameters: secure(activator, lost) gives "Arg 1 of 'secure' not a user-variable". Copy the built-in into a variable first.
label label The label to jump to, written without its colons: secure(pc, lost); for :lost:.

Notes

  • The secure is tested at each wake, after send(), sendto() and exec(), and after destroy() of the unit. A destroy() of a secured unit therefore jumps to the label at once and skips the rest: call unsecure() first.
  • A secure belongs to the template call that made it and ends when that call returns. When a called template returns, the caller's secures are tested again, so a unit that left during the call sends the caller to its label then.
  • Securing the same unit again only changes the label. A variable holding null secures nothing.
  • extraptr variables can't be secured; they are null after every wake. Find the extra again.
  • foreach() secures the units it walks over by itself; you don't need to.

Example

dilbegin storyteller();
var
   pc : unitptr;
code
{
   heartbeat := PULSE_SEC * 5;
:start:
   wait(SFB_CMD, command("bow") and (activator.type == UNIT_ST_PC));
   pc := activator;
   secure(pc, lost);
   exec("say Welcome, " + pc.name + ". Sit down and listen.", self);
   pause;
   exec("say Long ago, a dragon slept under this hill...", self);
   pause;
   exec("say ...and it sleeps there still.", self);
   unsecure(pc);
   goto start;

:lost:
   exec("say Some people have no patience.", self);
   goto start;
}
dilend

An NPC tells a story to the player who bowed. pc survives the pauses because it is secured; if the player walks out, the NPC grumbles instead.

See also