In a perfect world, your script would always know exactly what is going on in your scene or with your model. It would be aware of any changes or adjustments the user could make. And it would already know how to deal with any task that it might come across. If you were making a one-time-only script that ran on only one scene or object, then you could program around these conditions. Of course you cannot expect a sophisticated script to work on numerous scenes or objects in such a way.
There will be times when you need to test if some condition exists in your object or scene and have your script act accordingly. These tests are called Conditional Statements.
For example, you want to use a Modeler script to merge two selected points. Before you can merge anything, you may want to check that the user has two points selected. If the points are selected, then the script can merge the two points. But, if two points are not selected, you must politely tell the user to first select two points to merge.
A situation like this happens often in programming, and a variety of statements can perform these types of conditional tasks. The role of conditional statements is to verify other statements. If a statement is true, then LScript will execute some piece of code. If a statement is false, that piece of code is ignored. The most common of these conditional statements is the if-then statement.
The if-then statement checks that an expression is true. If the expression is true, then the script will execute the block of code between body markers that immediately follows the statement. If the expression is false, then that block of code is ignored and LScript will execute the next line of code following the closed body marker (}).
Let’s walk through this example. The if-then statement evaluates the pointsSelected variable to see if it has a value of 2. If it does, then the mergePoints() command is run. If pointsSelected does not have a value of 2, then the mergePoints() command is skipped and the next statement is run.
In the example, the block of code contained only one statement to run (the mergePoints statement). When this happens, the ifthen statement can actually be written without the body markers, as shown in the code that follows:
You can write the code this way only when a single line of code will be executed if the expression is found true.
You may have noticed that the expression in the if-then statement was pointsSelected == 2. This is not a typo, two equal signs are the operators in this expression. Let’s look at the different operators we can use in LScript.
The following list contains the basic operators we will learn:
Operator Translates in English to
=1 is now the value of
> is greater than
< is less than
>= is greater than or equal to
<= is less than or equal to
!= is NOT
Now we will use the aforementioned if-then statement to demonstrate the uses of each operator where applicable:
a = b a is now the value of b.
if(a == b) if a equals b…
if(a != b) if a does NOT equal b…
if(a > b) if a is greater than b…
if(a >= b) if a is greater than or equal to b…
if(a < b) if a is less than b…
if(a <= b) if a is less than or equal to b…
The if-then-else statement is nearly the same as the if-then statement, except for one major difference. Rather than completely skipping over any code if the expression is false, the code jumps to the else block and executes the code there.
Notice that the else statement directly follows the closed marker (}) of the true statements and there is no semicolon following it. This is because the else statement is still part of the if-then statement.
Writing the statement this way is actually more correct than the previous format. The conditional’s body markers are unnecessary when the statement uses only a single line of code. As you can see, this format can really save space in long scripts.
The error() statement simply brings up an error message box to the screen, and stops the execution of the script.You can also use the inline if then else format ? : as below
isittrue= test ? Aresult : Bresult;
isittrue evaluates to Aresult if test is true, or Bresult if test is false.
When you combine the if…then/else statement with a Boolean variable in the expression, you get an interesting situation. If the variable’s Boolean value is true, then the expression is considered true. Likewise, the expression is considered false if the variable has a false value.
done = false;
You can even "short circuit" Boolean tests, allowing expressions like this to be used:...
file = "C:\\CONFIG.SYS";
f = File(file,"r") || error("Cannot open file '",file,"'");
The switch() conditional statement is extremely powerful. Like the if-then statement, an expression is solved, but its resulting value is compared to a list of values. If the solution matches a list item, then the code directly following the case statement is executed. The break command leaves the switch() statement all together.
valueA = 3;
In the example above, the variables valueA and valueB are given integer values. The result of the expression valueA - valueB (which has the value of 2) is then passed to the switch() command. The switch command will then compare this value to a list of case values. When a match is made, the code bound to the case command is executed.
The first case statement value is 1. The expression value does not match this case value, so LScript will skip over the code and compare the next case statement. The next case value happens to match the expression value, so LScript executes the info() command. This command displays the text "A - B = 2" in an info() box. The next statement is the break statement. This instructs LScript to direct execution to the switch() statement’s closed marker (}).
The switch() statement help you avoid using several if-then or if-then-else statements to compare an expression against several different values.