Loops

Fallout 3's scripting language, unlike most programming and scripting languages, lacks many usual "flow control" features. However, the FOSE team has made two very useful functions available that allow us to better control the flow of our scripts. These functions are Label and Goto, and one of their most important uses is in the creation of loops.

This page contains templates for three common loops:

Before we go over the specific syntax of each loop, let's have a look at the syntax of Label and Goto. Both of these functions are non-reference functions, and take a single parameter, referred to as "LabelID" in the FOSE Documentation.

Label is used, as the name implies, to label a specific point in a script so that it can be targeted with Goto. Once a point in the script has been labelled, Goto can be used to redirect the script to the labelled point, as specified by its LabelID.

While

"While" loops are used to continually execute a section of code for as long as a specified condition is true. Here is some typical code for a "while" loop, in C++ syntax:

while (<condition>)
{
	<code>;
}

The condition specified in the "while" loop works in exactly the same way as in a conditional statement - if it is true then the code contained within it is executed. The main difference between a "while" loop and a conditional statement is that, once the code has been executed, the loop repeats itself, starting with re-checking the condition. Because of this, the code contained within the loop will be executed over and over again until the condition is no longer true.

Because the code contained within the loop will be executed over and over again until the condition is no longer true, it is very important that the code within the loop can cause the condition to become false, otherwise the loop will never cease and Fallout 3 will freeze. Keeping this all in mind, a "while" loop can be constructed using FOSE's Label and Goto functions like so:

Label 1
if <condition>
	<code>
	Goto 1
endif

Do while

A "do while" loop is very similar to a "while" loop. The only difference is that the code is executed before the condition is checked. In other words, the condition controls whether or not to run the loop again, as opposed to whether or not to execute the code contained within it. As a result of this difference, the code within a "do while" loop will always be executed at least once.

Here is some typical code for a "do while" loop, in C++ syntax:

do
{
	<code>;
} while (<condition>);

So, thinking again about the structure of a "do while" loop, here is how one could be written with Label and Goto:

Label 1
<code>
if <condition>
	Goto 1
endif

For

"For" loops are different to "while" and "do while" loops in that their duration is (normally) not determined by the code that they contain, but instead by the value of a controlling variable. Instead of just a condition, the start of a "for" loop consists of three parts:

  • Initialisation
  • Condition
  • Increment

The initialisation step occurs only once, when the loop first begins, and sets the controlling variable to an initial value, which may be the result of an expression. Then comes the condition, which is essentially the same as for a "while" or "do while" loop - if the condition is true, then the code runs, otherwise the loop is terminated. The increment step occurs after the code contained within the "for" loop has been run, and changes the value of the variable slightly.

Like with "while" and "do while" loops, you should take care to ensure that your "for" loops cannot become infinite loops. The combination of the initialisation and increment steps (and possibly the code within the loop) should always be able to render the condition false.

Here is an example "for" loop, in C++ syntax:

for (i = 0; i < 10; i++)
{
	<code>;
}

As you can see, provided that the code contained within the "for" loop does alter the value of the controlling variable, "i", it will run 10 times. On the 11th time, the value of "i" will be 10, rendering the condition false and terminating the loop.

So, now that we understand how a "for" loop works, here is how one can be written using Label and Goto:

<initialisation>
Label 1
if <condition>
	<code>
	<increment>
	Goto 1
endif

Now, go and have fun with your new knowledge of loops! If you have any questions about the content of this article, or if you notice any errors, don't hesitate to contact me.