Flow Control

Loop Statements

Let's have a look at how scripts can be used to control the progress of a game. Loops repeat when a statement is true.

The while Statement

this script simply loops from the given condition
test = 10
decrementing by 1 each time,
test -= 1
down to 0
displaying the value of test
and then exits the level.
As long as a given condition is true, the while loop will repeat 0 or more times.
A while loop evaluates its test expression before executing the body of the loop.

Okay, so counting down for a ten count and then exiting the level may not be much of a gameplay, however, the principle is valid for many scenarios. A popular application of a while loop is to check if a certain type of monster has been completely wiped out and then something happens, like a door opens or closes, or a sector rises or lowers.

For example:

Script 1 checks if all shotgunguys with TID 10 (T_SHOTGUY, 10) have been taken care of. If yes, then a pillar opens.

Script 2 checks if any monster with TID 11 (T_NONE, 11) has been taken care of. If yes, then the message WELL DONE appears on the display and the level ends.

The do Statement

A do loop is simply a while loop turned on its head.
First comes the loop, then the test expression.

Unlike a while loop, a do loop always executes at least once.

The for Statement

Again an example to do the same as before, count from 10 to 0 and end the level.

In a for loop the initialzing test and modifying expressions are grouped in one area, separated by a semicolon.

See also FOR_and_WHILE_loops in the WIKI for more examples.