logo_jazz jp


multilineB

in part b of this tutorial, we take a closer look at the multi-line script’s syntax by investigating its main building blocks: the conditional and loop structures.



multilineB_conditional

puce if

When the parser finds an if in the Script panel, it expects a boolean condition, that is, a logical proposition having one of two values: true (non-zero) or false (zero). If the condition is true, the parser executes the single line or block of code immediately after the condition, which, in the case of a block of code, will be enclosed within braces {}. If the condition is false, the statement in the single line or block of code is ignored.


The if structure in its simplest form:

if_01

( if firstof(x) is smaller or equal to 3, your Lemur sends a Control Change MIDI message on Target 0 for controller 20, with a value of 127, on MIDI channel 1. )



Several instructions can follow the boolean condition provided the block of code is enclosed within braces:

if_02

( if firstof(x) is smaller or equal to 3, your Lemur sends two Control Change MIDI messages: one for controller 20, one for controller 21. )


puce else

It’s also possible to make use of the else statement. If the condition is false, the execution jumps to the instruction immediately after else :

if_03

( if firstof(x) is smaller or equal to 3, your Lemur sends a Control Change MIDI message for controller 20. If firstof(x) is greater than 3, it sends a CC for controller 23. )


The else statement can also be a block of code contained within braces:

if_04

( if firstof(x) is smaller or equal to 3, your Lemur sends two CC MIDI messages for controller 20 and 21. Else if it’s greater than 3, your Lemur sends two CC MIDI messages for controller 22 and 23. )




puce else if

By using else if, it is possible to combine several conditions. Only the statement(s) following the first condition that is found to be true will be executed, and all other statement(s) will be skipped.

puce Example A

We’re using a row of pads as a rudimentary keyboard, and we’d like a Text object to display the notes’ names as we’re playing: A,B,C… For this we’ll use a script local to the Pads object, to be executed “on x” (each time a pad is pressed), and consisting of several else if statements. The firstof(x) function is used to return a value corresponding to the position of the pad being pressed, and the setattribute() function sets the Text object’s content attribute according to the value of the firstof(x) function.

if_05

if_06



multilineB_loops

puce for

The for loop is an iteration statement which allows code to be repeatedly executed a specified number of times. The type of for loop used in the Script panel is characterized by three control expressions separated by semicolons: the initializer expression, the loop test expression and the counting expression. The initializer sets up the initial value of the variable within the looped block of code. The loop test expression is evaluated at the beginning of each iteration through the loop, and determines when the loop should exit. Finally, the counting expression is responsible for altering the loop variable.

for

puce Example B

In the script below, a for loop is used to set up the value of a MultiSlider’s individual sliders on the basis of a cosine function. The initializer i=0 sets up the the initial value of the variable i to 0, which means that the first time, the loop block is executed for MultiSlider.x[0], setting up the value of the first slider. The variable i is then incremented by the counting expression ++ to i=1, and the code within braces repeated for MultiSlider.x[1] . The variable will be incremented and the code repeated as long as i< sizeof(MultiSlider.x), which means for each slider up to MultiSlider.x[31].

for_07

for_08



puce Example C

In this example, the for loop is used to set the X and Y positions of each of the Multiball’s balls in turn. Because the script is executed on each Lemur frame, and because the expressions used to define the x[i] and y[i] values include a time a variable, the balls’ positions keep changing. The circular motions are obtained by simultanesouly equating a ball’s x value to a cosine function and its y value to a sine function.

for_20

for_21




puce break

A break statement interrupts the execution of the current for/ while loop and jumps to the next statement.

break

puce continue

The Continue statement is used to skip the remainder of the loop body and continue with the next iteration of the loop. The effect is to prematurely terminate the innermost loop body and then resume as normal with the next iteration. If the iteration is the last one in the loop, the effect is to terminate the entire loop early.

break

puce while

A while loop allows code to be executed repeatedly based on a given boolean condition. The while structure consists of a condition and a block of code. The conditon is first evaluated, and if the condition is true, the block of code within braces is then executed. This repeats until the condition becomes false.

puce Example D

In the script below, the getfirst(Container) function is used to select the first object within the Container object’s hierarchy. Since there’re several objects in the Container, the boolean condition (obj) will necessarily be true, and therefore the statements within braces executed a first time. The getobjectrect(obj) function will extract the position and dimensions of the Container’s first object, and its vertical position (tmp[1]) recalculated on the basis of the Fader’s position, and object’s offset (the offset variable defines the vertical position within the Container, and is manually entered for each object). Once the setattribute(obj,’rect’,tmp) function has changed the object’s vertical position, the obj variable is replaced with the next object down the Container’s hierarchy (this is done with the getnext(obj) function), and the statements within braces repeated. Those statements will be repeated for each object within the Container, that is, as long as the getnext(obj) returns a non-zero value and keep the boolean condition (obj) true.

while_10

while_10

puce do while

The do while structure also consists of a condition and a block of code, but the difference here is that the block of code within braces is executed first, and then the condition evaluated. If the condition is true, the block of code within braces is executed again. This repeats until the condition becomes false. The do while loop can be referred to as a post-test loop, and the while loop as a pre-test loop.

while_12



multilineB_return


The return statement isn’t specific to conditional or loop structures, but can be used anywhere to instruct the execution to leave the current script. For instance, it can be used to optimize a script’s efficiency by preventing the execution to read the entire script needlessly:

return_22

As its name suggests, the return statement can also return the value or expression typed immediately after it:

puce Example E

The script below can be used to return the maximum value reached by an array (vector). Notice that the script’s execution mode is set to Manual, so it can be recalled at any time, in any parts of the project. For instance, it can be recalled in a monitor object to display the maximum value reached by a MultiSlider’s sliders. This is done by changing the script’s arguments (array) by (MultiSlider.x).



return_14

return_13 return_15




Download this example
Go back to workshops


© Copyright JazzMutant 2016 12-25-2016