Manual:DIL Manual/interrupt()
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)).