Syntax
Starts a label as a cooperative task that runs alongside the main program, and stores its handle in a variable.
<handle> AS TASKHANDLE
<handle> AS JOB
STARTTASK <handle>, <label>
<label>:
<statements>
RETURN <value>Parts
| <handle> | A plain variable that receives the task handle. Declared AS TASKHANDLE or AS JOB (the same type), it starts as NULL and accepts only tasks. |
|---|---|
| <label> | The name of a label in the program. It must exist: "Unknown BASIC task label". |
| <statements> | The work of the task. It shares the program's variables. |
| <value> | The result of the task, read later with TaskResult(). |
Good to know
- The task does not start on the spot. It begins when the main program next yields: Sync(), Wait(),
AWAITTASK… - Tasks are cooperative: only one piece of code runs at a time, and they take turns only at a yield.
- The handle must be a plain variable.
STARTTASK jobs[1], Workercompiles but the program cannot start. Start into a variable, then copy it. STARTTASKcreates the variable if needed, even underOPTION EXPLICIT. Assigning anything but a task to a typed handle stops the program withBASIC value is not a task handle.- A bare
RETURNleaves no usable result. An error in a task nobody awaits is silent: onlyTaskFailed()shows it. STARTTASKis allowed inside aFUNCTION. Inside the label, give each yield its own line, with no comment after it (seeYieldTask()).
Example
loader AS TASKHANDLESTARTTASK loader, LoadLevelConsole("started") ' startedDO IF TaskDone(loader) Console("rooms: " + Str(TaskResult(loader))) ' rooms: 3 EXIT ENDIF Sync()LOOPLoadLevel: FOR room = 1 TO 3 ' one room per frame Sync() NEXTRETURN 3In the manual : The game loop and tasks