Conditional statements are statements that start off with the question if; if the condition if proves true, then the statement is executed. Conditional statements can be used as switches and are good ways of controlling an animation channel or variable through the state of another animation channel or variable. For example, you might start rotating a cube after it reaches a certain height (position in Y). The if and if-else statements are very common conditional statements found in most scripting and programming languages. You will often use relational and logical operators in conditional statements.
if statements
The if conditional statement is formatted in this way:
if ( condition )
statement;
If condition is true, statement executes.
Example:
if ( Cube.Position.Y > 30 )
Cube.Roll_Z = 45;
This sets the value of the Z rotation channel of cube to 45 when the cube is moved higher than 30 units in Y.
if-else statements
The if-else conditional statement is formatted in this way:
if ( condition )
statement1;
else
statement2;
If condition is true, statement1 executes. Otherwise statement2 executes.
Example 1: Simple if-else statement
if ( Cube.Position.Y > 30 )
Cube.Roll_Z = 45;
else
Cube.Roll_Z = -45;
This sets the value of the Z rotation channel of cube to 45 when the cube is moved higher than 30 units in Y. If the Y position of cube is 30 or less, then the z rotation of cube is set to -45.
You can use more than one statement after a condition with this format:
if ( condition )
{
statement;
statement;
}
else
{
statement;
statement;
}
For this to function properly, you need to enclose the multiple statements within braces ( { } ).
Example 2: Braces in if-else statement
if ( Cube.Position.Y > 30 )
{
Cube.Roll_Z = 45;
Cube.Pitch_X = time * 0.25;
}
else
{
Cube.Roll_Z = -45;
Cube.Pitch_X = -( time * 0.25 );
}
If the Y position of the cube is greater than 30, then the z rotation is set to 45 and the x rotation is set to the current time, multiplied by .25 (or divided by 4 - same thing).
If the Y position of the cube is less than or equal to 30, then the z rotation is set to -45, and the x rotation is set to a negative value of the current time multiplied by .25.
Chains of conditional statements
Every if-else statement can be followed by another if-statement resulting in a syntax like this:
if ( condition1 )
statement1;
else if ( condition2 )
statement2;
If condition1 is true, statement1 gets executed and the remaining statements in the if-else function gets ignored.
On the other hand, if condition1 is false, the second if statement gets processed. If condition2 is then true, statement2 gets executed. If neither of the conditions are true, then both statements are ignored.
You can add an if-else statement to a previous if-else statement as follows:
if ( condition1 )
statement1;
else if ( condition2 )
statement2;
else
statement3;
If the first 2 conditions are false, then statement3 is executed.
Example:
if ( Cube.Position.Y > 30 )
Cube.Roll_Z = 45;
else if ( ( Cube.Position.Y =< 0 ) && ( time =< 6 ) )
Cube.Roll_Z = -45;
else
Cube.Roll_Z = 0;
In this example, if the Y position of the cube is greater than 30, then the cube's Z rotation is set to 45; however, if the Y position is 0 or less AND the current time is 6 or less, then the Z rotation of the cube is set to -45. If neither of these conditions are true - say the cube position is 13 and the time is at 4 seconds - then the Z rotation is set at 0.
You can build chains of if-else statements and multiple statements within braces ( { } ) using this format:
if (condition1)
{
statement;
statement;
}
else if (condition2)
{
statement;
statement;
}
else if (condition3)
{
statement;
statement;
}
else if (condition4)
{
statement;
statement;
}
else
{
statement;
statement;
}
Conditional statements based on object existance
A special case of a conditional statement is the conditional statement based on object existance. You can execute statements based on the existance of other objects in the project. This can be done with this syntax:
if ( exists ("objectname") )
The objectname within the brackets must be put in quotation marks.
|