Variables

Q-SYS Control 101 Training : Flow Control

3 ) Simple Communication

15m 13s

4 ) Feature License Activation

4m 12s

5 ) Block Controller

19m 7s

7 ) Flow Control

34m 20s

8 ) Control 101 Conclusion

1m 42s

Lesson Description

Variables 5m 41s

Make your coding easier by using variables to reference objects in Lua and the Block Controller.

Video Transcript

Variables 5m 41s
0:08
Let’s talk about variables. You hear the word “variables” a lot in bank heist movies when they talk
0:13
about things that might go wrong. You might hear a variable defined as a “known-unknown” –
0:20
something that you know exists, but you don’t know what it is. But often times in scripting you will
0:25
definitely know what your variable is, and you just chose to use a variable to make your script easier
0:31
to write. I prefer to think of a variable as just a convenient label for a value that may or may not
0:37
change.
0:38
In algebra, you’ve probably seen x, y, and z often used as variables: they’re just a letter that
0:45
represents some other number. In Lua, you could define these variables just by typing the name of
0:51
of your variable, the equal sign, and then whatever you want that variable to represent. But you’re
0:57
not limited to single letters, or to numbers. You could use whole words to represent variables, and
1:03
a variable could define a numerical value, or a string, or a control property,
1:08
or even a complete function, or more.
1:12
Here's an example of using a variable to represent a changing value. I’ll assign variable “s” as the
1:18
string “Hello”, then I’ll “print(s)”. This prints the variable s rather than the literal character of “s”.
1:27
Next I’ll reassign variable “s” as the string “World” and “print(s)” again. The two commands here,
1:34
“print(s)” are identical, but because the variable has changed in between them as the compiler runs
1:41
through the script, we get two different results, based on what the variable was at the time the
1:46
command was issued.
1:48
Similarly, you might use a variable to represent a control’s Value or String. This information isn’t
1:55
defined within the scope of your script, it comes from the normal Q-SYS environment. But when you
2:01
print your variable, it will show you the current state of that control.
2:05
Often times you might use a variable as an easy shortcut for something that is used multiple times.
2:11
For instance, what if you had a larger script in which you have multiple functions that all return a
2:16
string of “Message Complete.” If you wanted to change that string later on, you would have to search
2:22
through every function and change it line-by-line, or you could have just used a variable in the first
2:28
place. Then you could define your variable at the top of the script, so you only need to ever change it
2:33
in one place and that will be carried forward to every instance the variable is referenced.
2:40
These variables we’ve been creating are called global variables, because they’re valid everywhere in
2:45
the script. Sometimes you might want to create a local variable, which will be defined within the
2:50
body of a function or control structure, and that variable is only valid within the scope of that
2:56
function. If you had multiple functions that all do very similar things, for instance, it might be tedious
3:02
to use global variables such as output1, output2, output3, etc. Instead, each function could create its
3:09
own local variable called “output” which would never interact with anything outside of its function.
3:15
We’ll look at how to create functions in the next video.
3:18
In the Block Controller, you can manipulate variables with the blocks you’ll find here in the “Variables
3:24
and Functions” tab. Use the “Create” block to make a new variable, which you can then define (or
3:31
not) on the other side of the equal sign. You can then set this variable to a new value using the “set”
3:38
block. If you need to reference this variable, you can use this block here with the variable’s name. If I
3:45
wanted to print a variable that is defined by one of my control’s Values, I might do so like this.
3:51
When you’ve created multiple variables, you’ll see their names are all available in the drop down
3:56
box. You’ll notice that the default variable name is “foo” which, and I cannot stress this enough, you
4:04
should pity.
4:06
That’s right. Pity the foo. Okay, but all seriousness aside, if you don’t like the name Foo you can, of
4:13
course, change it, but it’s a fine name. For those of you who feel the need to fight the foo, let just say
4:20
this. All My Life I’ve been dealing with variables, and a good global variable is My Hero. There’s no
4:28
reason to throw a Monkey Wrench in your script In Times Like These. Think of a variable as the
4:35
Pretender, which can represent anything you want. It will make the Best of You … and ensure that
4:40
your design will last … Everlong.
4:45
One last thing in the Block Controller, note that it knows the variable is a global variable. Whereas if
4:52
you were to create this variable within the construct of a function block, it will automatically
4:57
understand that this is a local variable. You don’t need to decide if it is one or the other.
5:02
Also, in the Block Controller, you can only use the Create block for each variable once. After the
5:08
variable is created, you can only change it to something else using the Set block. In Lua you can use
5:14
the same command to either create or redefine a variable, as you can see from the Lua tab of these
5:20
blocks: “foo=”awesome” and “foo=moreawesome” but the Block Controller needs those two
5:27
concepts separated.
5:29
And that’s it for variables. We have an exercise on variables in your Control Worksheet, and once
5:34
it’s done we’ll move on to talk about Functions.