<HTML>
<HEAD>
<!-- This HTML file has been created by texi2html 1.51
from ProgrammersManual.texinfo on 4 March 1997 -->
<TITLE>LambdaMOO Programmer's Manual - Forking</TITLE>
</HEAD>
<BODY>
Go to the <A HREF="ProgrammersManual_1.html">first</A>, <A HREF="ProgrammersManual_36.html">previous</A>, <A HREF="ProgrammersManual_38.html">next</A>, <A HREF="ProgrammersManual_77.html">last</A> section, <A HREF="ProgrammersManual_toc.html">table of contents</A>.
<P><HR><P>
<H3><A NAME="SEC37" HREF="ProgrammersManual_toc.html#TOC37">Executing Statements at a Later Time</A></H3>
<P>
It is sometimes useful to have some sequence of statements execute at a later
time, without human intervention. For example, one might implement an object
that, when thrown into the air, eventually falls back to the ground; the
<SAMP>`throw'</SAMP> verb on that object should arrange to print a message about the
object landing on the ground, but the message shouldn't be printed until some
number of seconds have passed.
</P>
<P>
The <SAMP>`fork'</SAMP> statement is intended for just such situations and has the
following syntax:
</P>
<PRE>
fork (<VAR>expression</VAR>)
<VAR>statements</VAR>
endfork
</PRE>
<P>
The <SAMP>`fork'</SAMP> statement first executes the expression, which must return a
integer; call that integer <VAR>n</VAR>. It then creates a new MOO <STRONG>task</STRONG> that
will, after at least <VAR>n</VAR> seconds, execute the statements. When the new
task begins, all variables will have the values they had at the time the
<SAMP>`fork'</SAMP> statement was executed. The task executing the <SAMP>`fork'</SAMP>
statement immediately continues execution. The concept of tasks is discussed
in detail in the next section.
</P>
<P>
By default, there is no limit to the number of tasks any player may fork, but
such a limit can be imposed from within the database. See the chapter on
server assumptions about the database for details.
</P>
<P>
Occasionally, one would like to be able to kill a forked task before it even
starts; for example, some player might have caught the object that was thrown
into the air, so no message should be printed about it hitting the ground. If
a variable name is given after the <SAMP>`fork'</SAMP> keyword, like this:
</P>
<PRE>
fork <VAR>name</VAR> (<VAR>expression</VAR>)
<VAR>statements</VAR>
endfork
</PRE>
<P>
then that variable is assigned the <STRONG>task ID</STRONG> of the newly-created task.
The value of this variable is visible both to the task executing the fork
statement and to the statements in the newly-created task. This ID can be
passed to the <CODE>kill_task()</CODE> function to keep the task from running and
will be the value of <CODE>task_id()</CODE> once the task begins execution.
</P>
<P><HR><P>
Go to the <A HREF="ProgrammersManual_1.html">first</A>, <A HREF="ProgrammersManual_36.html">previous</A>, <A HREF="ProgrammersManual_38.html">next</A>, <A HREF="ProgrammersManual_77.html">last</A> section, <A HREF="ProgrammersManual_toc.html">table of contents</A>.
</BODY>
</HTML>