Difference between revisions of "Manual:DIL Manual/interrupt()"
(XML import) |
(checked against the compiler and the interpreter; example compiles) |
||
| Line 1: | Line 1: | ||
| − | + | = interrupt() = | |
| − | + | '''Function''' (integer). Sets up a jump to a label when a certain event wakes the program, whatever it is waiting for. | |
| − | + | interrupt(flags : integer, condition, label) : integer | |
| − | |||
| − | + | Registers an interrupt for the running template. From then on, every time the program is woken by an event of a kind in <code>flags</code>, the <code>condition</code> is evaluated before anything else; when it is true, the program leaves whatever it was doing (a <code>wait</code>, a <code>pause</code>) and goes on at <code>label</code>. The program is woken by those events even when its <code>wait</code> asks for other ones. Typical use: a long-running NPC program that should react to a message or to its own death at any moment. | |
| − | |||
| − | |||
| − | + | == Parameters == | |
| − | + | {| class="wikitable" | |
| + | ! Name !! Type !! Meaning | ||
| + | |- | ||
| + | | flags || integer || <code>SFB_*</code> event kinds from <code>vme/include/vme.h</code>, joined with <code>|</code>: <code>SFB_CMD</code>, <code>SFB_MSG</code>, <code>SFB_TICK</code>, <code>SFB_DEAD</code>, <code>SFB_COM</code>, <code>SFB_DONE</code>... Write the constants themselves (see the notes). | ||
| + | |- | ||
| + | | condition || integer expression || Tested at each such event, with <code>activator</code>, <code>argument</code>, <code>command()</code> and the rest describing that event. | ||
| + | |- | ||
| + | | label || label || Where to go on when the condition is true, or <code>skip</code> to ignore the event: the program then stays where it was and the event goes no further in it. | ||
| + | |} | ||
| − | + | == Returns == | |
| + | The number of the interrupt, for [[Manual:DIL Manual/clear()|clear()]]. It can be used as a statement on its own (<code>interrupt(SFB_MSG, argument == "stop", stop_it);</code>) or assigned (<code>i := interrupt(...);</code>), but not inside an expression. | ||
| − | + | == Notes == | |
| − | + | * The interrupt stays set until <code>clear()</code> or until the template returns: it fires every time, not once. Running the same <code>interrupt</code> statement again replaces it instead of adding a second one. | |
| − | + | * When several interrupts match an event, they are tried from the lowest number up, and the first whose condition is true wins. | |
| − | + | * An interrupt whose <code>flags</code> include <code>SFB_ACTIVATE</code> is tested at every wake, and is removed the first time it jumps to a label. <code>on_activation(cond, label)</code> is a macro in <code>vme/include/wmacros.h</code> for <code>interrupt(SFB_ACTIVATE, cond, label)</code>. | |
| − | + | * Interrupts belong to the template that set them: a template called from it doesn't see them, and they end when it returns. With <code>recall</code>, the main template's interrupts are saved and restored with the program. | |
| − | + | * The flags must be a constant expression such as <code>SFB_MSG | SFB_CMD</code>. A variable holding the flags is read as 0, and that interrupt never fires. | |
| − | + | * Unit variables are cleared at the wake as usual: at the label, find the units again or keep them with <code>secure</code>. | |
| − | + | == Example == | |
| − | + | <pre> | |
| + | dilbegin guard_post(); | ||
| + | var | ||
| + | i : integer; | ||
| + | code | ||
| + | { | ||
| + | heartbeat := PULSE_SEC * 30; | ||
| + | i := interrupt(SFB_MSG, argument == "stand down", relieved); | ||
| − | + | :loop: | |
| − | + | wait(SFB_TICK, TRUE); | |
| + | act("$1n looks around watchfully.", A_SOMEONE, self, null, null, TO_ROOM); | ||
| + | goto loop; | ||
| − | + | :relieved: | |
| + | clear(i); | ||
| + | act("$1n relaxes and stops guarding.", A_SOMEONE, self, null, null, TO_ROOM); | ||
| + | quit; | ||
| + | } | ||
| + | dilend | ||
| + | </pre> | ||
| + | An NPC that keeps watch every 30 seconds until another program sends it the message "stand down" (<code>sendto("stand down", guard)</code>). | ||
| − | + | == See also == | |
| − | + | * [[Manual:DIL Manual/clear()|clear()]] | |
| − | + | * [[Manual:DIL Manual/wait()|wait()]] | |
| − | + | * [[Manual:DIL Manual/skip()|skip()]] | |
| − | + | * [[Manual:DIL Manual/sendto()|sendto()]] | |
| − | + | * [[Manual:DIL Manual/recall|recall]] | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
Revision as of 23:17, 14 November 2026
interrupt()
Function (integer). Sets up a jump to a label when a certain event wakes the program, whatever it is waiting for.
interrupt(flags : integer, condition, label) : integer
Registers an interrupt for the running template. From then on, every time the program is woken by an event of a kind in flags, the condition is evaluated before anything else; when it is true, the program leaves whatever it was doing (a wait, a pause) and goes on at label. The program is woken by those events even when its wait asks for other ones. Typical use: a long-running NPC program that should react to a message or to its own death at any moment.
Parameters
| Name | Type | Meaning |
|---|---|---|
| flags | integer | : SFB_CMD, SFB_MSG, SFB_TICK, SFB_DEAD, SFB_COM, SFB_DONE... Write the constants themselves (see the notes).
|
| condition | integer expression | Tested at each such event, with activator, argument, command() and the rest describing that event.
|
| label | label | Where to go on when the condition is true, or skip to ignore the event: the program then stays where it was and the event goes no further in it.
|
Returns
The number of the interrupt, for clear(). It can be used as a statement on its own (interrupt(SFB_MSG, argument == "stop", stop_it);) or assigned (i := interrupt(...);), but not inside an expression.
Notes
- The interrupt stays set until
clear()or until the template returns: it fires every time, not once. Running the sameinterruptstatement again replaces it instead of adding a second one. - When several interrupts match an event, they are tried from the lowest number up, and the first whose condition is true wins.
- An interrupt whose
flagsincludeSFB_ACTIVATEis tested at every wake, and is removed the first time it jumps to a label.on_activation(cond, label)is a macro invme/include/wmacros.hforinterrupt(SFB_ACTIVATE, cond, label). - Interrupts belong to the template that set them: a template called from it doesn't see them, and they end when it returns. With
recall, the main template's interrupts are saved and restored with the program. - The flags must be a constant expression such as
SFB_MSG | SFB_CMD. A variable holding the flags is read as 0, and that interrupt never fires. - Unit variables are cleared at the wake as usual: at the label, find the units again or keep them with
secure.
Example
dilbegin guard_post();
var
i : integer;
code
{
heartbeat := PULSE_SEC * 30;
i := interrupt(SFB_MSG, argument == "stand down", relieved);
:loop:
wait(SFB_TICK, TRUE);
act("$1n looks around watchfully.", A_SOMEONE, self, null, null, TO_ROOM);
goto loop;
:relieved:
clear(i);
act("$1n relaxes and stops guarding.", A_SOMEONE, self, null, null, TO_ROOM);
quit;
}
dilend
An NPC that keeps watch every 30 seconds until another program sends it the message "stand down" (sendto("stand down", guard)).