If/Then Statement

Site: QSC
Course: Q-SYS Control 101 Training
Book: If/Then Statement
Printed by: Guest user
Date: Saturday, 23 November 2024, 2:09 AM

Description

Lesson Description

Explore the decision-making process of the If/Then Statement, and use it to create conditional statements that control your Q-SYS design.

Video Transcript

0:08
This is it! It’s all been leading up to this! All the printing and commenting, control tree metadata,
0:14
functions and variables, they’ve all just been pieces of the puzzle. Now … now we finally get to do
0:19
something. Introducing – the If/Then statement. This is the basic decision-making component of our
0:26
script. If this thing is true, do X. If that thing is true, do Y. If you press the gas, then your car goes
0:35
forward. If you film this on your first take, then we don’t have to come in on Saturday.
0:40
Now, it may have felt like we were touching on this decision-making process earlier, when we
0:44
started combining the EventHandler and Functions.
0:48
After all, we were able to use the EventHandler to launch a function, which then performed its task.
0:53
That feels like if you press the button, then the function happens. But there’s no real decision-making
1:00
or analysis happening here, because the function will always do the exact same thing every time the
1:06
EventHandler is triggered, regardless of whether the button is toggling on or off.
1:11
But now with our If/Then statement, we can make qualifying conditional statements.
1:16
The body of an If/Then statement looks like this: if [qualifying statement] / then [result] / end
1:26
We could use this to make sure the result only occurs under certain conditions. For instance we
1:31
previously showed you a fader that turned yellow when it reaches the top of its range. We could do
1:36
this with an if/then statement like this:
1:40
When Controls.MrFader.EventHandler is triggered, it will start a function()
1:46
If Controls.MrFader.Value > 0 / then Controls.MrFader.Color = “yellow”
1:57
end (once for the if/then statement) and then end a second time for the function.
2:02
As we move the fader, its EventHandler is constantly triggered, but its function will only change the
2:07
control’s color if the control’s value is in the specified range.
2:11
But … we don’t have anything in place yet that changes the fader’s color back when it returns below
2:17
0 decibels. Now you could add a second If/Then statement after the first one which states “If the
2:25
Value is less than 0 then change the color to … aqua,” but there’s a more efficient way to do this. The
2:34
body of our If/Then Statement can also include an “else.”
2:39
The else allows us to specify an alternative result that will occur if the qualifying statement is not
2:45
met. In this case, we don’t need this second If/Then statement, because that can be summarized
2:51
with a simple else statement within the body of the first If/Then statement.
2:57
You can see this works perfectly: our fader is yellow at the top of its range, and aqua everywhere
3:02
else. We can actually continue to modify our If/Then statement with another tool, called “elseif.”
3:10
These are additional qualifying statements that will be analyzed, in order, if the previous qualifying
3:17
statement is not met. While our current simple If/Then statement only has two possible results,
3:23
the addition of elseifs can account for an unlimited number of possible results. I’ll start off simple
3:29
with this fader, and add a third color when the control is near the bottom of its range. So I’ll add an
3:35
elseif … Value < -40, then Color is “gray.” Now if the control doesn’t match the first qualification of
3:46
being above 10, it will check to see if it matches the second qualification of being below -40. If it is,
3:53
the color is gray. If it isn’t, then it defaults to the else condition of being aqua.
4:00
Don’t forget to use variables to your advantage, too. I can make this If/Then statement easier to type
4:06
if I were to define a global variable at the top such as Y equals Controls.MrFader. Now I don’t need to
4:15
bother typing that every time, I could just use Y.Value and Y.Color, which makes it a little friendlier to
4:21
read, too. Also, keep in mind that this is a very simple statement. You could have multiple things
4:29
occur in each of your results, targeting any number of different controls and their many control tree
4:34
properties such as their Values, their Strings, or you could target one of a control’s methods instead,
4:40
which as you may recall will allow you to activate a trigger, which might be wired to something else in
4:46
your design like a snapshot. You can get very complicated with your results to make a wide number of
4:52
things happen.
4:53
You can also add complexity within your qualifying statements. For instance, you could specify that
5:00
two different things must both be true with an “and” statement, or that either of two things could
5:07
satisfy the condition with an “or” statement. You could also combine these ideas together using
5:13
parentheticals to make extremely specific conditions.
5:16
Don’t forget to use your printing and commenting tools as well. You could add a print command
5:23
within the body of your result, so that you have some record that it happened. This is a great way to
5:28
collect proof that your function is operating correctly. And as your script gets larger, add a comment
5:35
to remind yourself or someone else what each chunk of code means in layman’s terms.
5:41
You might also choose to reformat your script by referencing functions within your functions. For
5:47
instance, this if/then statement is getting hard to read. Instead, I’ll take all of result one and define it
5:55
as a named function here at the top of the script. Let’s literally name this function “result1”. I’ll then
6:02
add an end to cap off this function, and then in the if/then statement I can reference the function
6:09
rather than typing all of that out. Now I'll do the same thing for "result2". I'll cut it from here and
6:17
redefine it her at the top of my script as its own function, and then reference that function within
6:23
my if/then statement. Now my If/Then statement is much easier to read, since I’ve replaced these
6:29
long ugly commands with the function’s name, which will only be launched under the correct
6:34
conditions. There’s one thing you may have noticed in this last function that I should warn you about,
6:40
which is the use of the double-equal sign. Let’s swap over to a really simple script so we can focus
6:46
on this. Here I have a single button. When it’s Value is 1, we’ll print “on”, and otherwise we’ll print
6:53
“off.” Looks like pretty clean code to me, but in the Debug Window we get an error. Why is that?
7:00
Well to you and me this equal sign makes perfect linguistic sense – “If the Value equals 1, do the
7:06
thing.” But to a computer, an equal sign is actually a call to action. This tells it to change one thing to
7:13
become the other, but we don’t want to change anything here! So if you are simply comparing two
7:18
things, you want to use the double-equal sign, which is the symbol for equivalency.
7:23
In computer speak, this says, “if the control’s Value is equivalent to 1”, then we do something. This is
7:32
an easy thing to get wrong when you first start writing If statements, so be careful!
7:37
If we think back to the Control Components, you hopefully remember that we accomplished simple
7:43
tasks using Control Functions. There are about thirty different functions in here, which are, in
7:50
essence, preselected scripting blocks. The Logic AND block, for instance, requires both of its inputs
7:57
to have a Value of 1, for its output to be 1. Well that’s the same as this If/Then statement:
8:03
If Controls.Inputs 1 and 2 are equivalent to 1, then Controls.Outputs.Value = 1. Else, the Value is 0.
8:15
So now you can probably see how these Control Functions are useful for simple logic paths, but
8:20
they lack the customization you can implement when writing your own script. The qualifying
8:26
statements and results of these Control Functions are set in stone and only do one thing, while a
8:31
script is as flexible as you want.
8:34
Finally, let’s look at building If/Then statements in the Block Controller. You’ll find the “if” block in
8:41
the “Flow Control” category. The nomenclature here is If/Do, but it’s the same concept. You can
8:48
define your qualifying statement with the If slot, and build the desired result here in the Do slot. If
8:57
you click on the cog icon, you can restructure the body of this block to include an Else statement, and
9:04
as many Else If statements as you like. Just like in Lua scripting, you can stack multiple commands
9:10
within each “do” section, you can reference other functions or variables you’ve defined elsewhere,
9:16
you can add more logic operators within your if and else-if slots for added complexity, and you can
9:24
attach debug print blocks and comment blocks to keep everything organized.
9:30
So, go have some fun! Return to the Control Worksheet and complete the If/Then Statement
9:36
exercises, and feel free to get creative and see what else you can accomplish, incorporating all
9:41
the different things you’ve learned in this training so far. Then move on to the next video whenever
9:45
you’re ready.