Difference between revisions of "Manual:DIL Manual/secure()"
(XML import) |
(checked against the compiler and the interpreter; example compiles) |
||
| Line 1: | Line 1: | ||
| − | + | = secure() = | |
| − | secure ( u : unitptr , label ) | + | '''Procedure.''' Keeps a unit in a variable across waits, and names where to go when it leaves. |
| − | + | ||
| − | + | secure(u : unitptr, label) | |
| − | + | ||
| − | + | 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. | |
| − | + | ||
| − | + | 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>. | |
| − | + | ||
| − | + | == Parameters == | |
| − | + | {| class="wikitable" | |
| − | + | ! Name !! Type !! Meaning | |
| − | + | |- | |
| − | + | | 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. | |
| − | + | |- | |
| − | + | | label || label || The label to jump to, written without its colons: <code>secure(pc, lost);</code> for <code>:lost:</code>. | |
| − | + | |} | |
| − | + | ||
| − | + | == Notes == | |
| − | + | * 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 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. | |
| − | + | * [[Manual:DIL Manual/extraptr|extraptr]] variables can't be secured; they are null after every wake. Find the extra again. | |
| − | + | * [[Manual:DIL Manual/foreach()|foreach()]] secures the units it walks over by itself; you don't need to. | |
| − | + | ||
| − | + | == Example == | |
| − | + | <pre> | |
| − | + | 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.