Flow Control, part 2
The most fundamental flow control is deciding which of two possible code blocks to execute. This is done with the "if" statement. In it's simplest form, it tests a condition, and, if true, executes a piece of code
if (condition) then {
// code to execute
};
The following are working examples of if statements:
The second example checks whether _x is zero, and sets _y to 1 if it is. Note that the block following then is not executed if the condition is false, which means that in case _x is not zero, _y will be undefined.
The second form of an if statement tests a condition, and then executes one of two possible code blocks depending on the result:
if (condition) then {
// code to run when condition is true
} else {
// code to run when condition is false
};
Examples of this:
if ... then and if ... then ... else statements allow for a wide variety of possibilities. In fact, this and the while construct discussed in the next chapter are actually all that is needed. Any other constructs are purely "cosmetically" and just make the script easier to read.
SQF scripting allows another use of the if statement that is uncommon and not usually found in other languages: The if statement can be used in an assignment.
You can often find this in scripts in a case like this:
allUnits = (if (isMultiplayer) then {playableUnits} else {switchableUnits});
playableUnits returns all playable units in a multiplayer game, but has no effect in single player. Conversely, switchableUnits shows all units the player can switch to in a single player match, but returns an empty list in multiplayer.
The above line, then, checks if the game is in multiplayer (if (isMultiplayer)) and if it is returns playableUnits, else it retuns switchableUnits. The result is assigned to allUnits.
To recapture:
The most fundamental flow control is deciding which of two possible code blocks to execute. This is done with the "if" statement. In it's simplest form, it tests a condition, and, if true, executes a piece of code
if (condition) then {
// code to execute
};
The following are working examples of if statements:
- if (_a < 0) then {
player sideChat "_a is less than zero";
};
- if (_x == 0) then {
_y = 1;
};
The second example checks whether _x is zero, and sets _y to 1 if it is. Note that the block following then is not executed if the condition is false, which means that in case _x is not zero, _y will be undefined.
The second form of an if statement tests a condition, and then executes one of two possible code blocks depending on the result:
if (condition) then {
// code to run when condition is true
} else {
// code to run when condition is false
};
Examples of this:
- if (alive player) then {
player setPos _somePosition;
} else {
player setPos _someOtherPosition;
};
- max = 0;
if (_a > _b) then { max = _a} else { max = _b;}
if ... then and if ... then ... else statements allow for a wide variety of possibilities. In fact, this and the while construct discussed in the next chapter are actually all that is needed. Any other constructs are purely "cosmetically" and just make the script easier to read.
SQF scripting allows another use of the if statement that is uncommon and not usually found in other languages: The if statement can be used in an assignment.
You can often find this in scripts in a case like this:
allUnits = (if (isMultiplayer) then {playableUnits} else {switchableUnits});
playableUnits returns all playable units in a multiplayer game, but has no effect in single player. Conversely, switchableUnits shows all units the player can switch to in a single player match, but returns an empty list in multiplayer.
The above line, then, checks if the game is in multiplayer (if (isMultiplayer)) and if it is returns playableUnits, else it retuns switchableUnits. The result is assigned to allUnits.
To recapture:
- An if ... then statment checks a condition, and if the condition is true, runs a block of code
- An if ... then ... else statement is an extended version that checks a condition, and runs the first piece of code if the condition is true (the "then" branch) and the second piece of code (the "else" branch) when the condition is false.
- A special form of if ... then ... else can be used in assignments to assign values depending on a condition