Lua for GrandMA3 Episode 2 - If Statements, Value Comparisons, and Local Variables

Hey lighting folks! The following is the transcript from my YouTube video, Lua for MA3 Ep 2 - If Statements, Value Comparisons, and Local Variables

This video is part of my Lua for GrandMA3 Tutorial series. Please check out my channel, From Dark To Light, on YouTube, and you can find the code to go along with my tutorials here on GitHub.



Hello lighting people! Welcome back to my Lua for GrandMA3 tutorial series. In this episode we are going to talk about if statements and value comparison, and I'm also going to tell you about local variables while we're at it. Let's jump in!


I'm going to start by making a new file here in Visual Studio Code. Always name your file “.lua” at the end… And I'm going to create a main function as a starting point. I usually start by making this sort of code, just function and return, just to start off with, and then I click back into it to continue making whatever I'm making.

So I'm going to start by creating example if statements and kind of show you how comparisons work as we go. So before creating my if statement let's say I set this variable “myVar” equal to 35. Now I'm going to create an if statement “if myVar is equal to (it's two equal signs) 35 (close the parentheses), then Printf(‘It is so’)” cuz what else would we say, I don't know, and then “end.”

So the basic things you need to remember about making if statements are: if whatever's in the parentheses is true, then we'll do this, end. So this is a complete if statement that checks the variable and does an action based on what the variable is. Other ways I could compare this number include less than, less than or equal to, greater than, of course, greater than or equal to, and then the last one is, not equal to.

You can also use “not” to convert a comparison to the opposite, so if I put this as “myVar equal to” and then I put this here, “if not myVar equal to 35” then it basically flips this.

If you want to check for multiple conditions before deciding whether or not to execute the code, you could use “and” or “or.” I'm going to remove this “not” here and go like this. I can change “myVar is equal to 35” to “if myVar is less than 36 or myVar is greater than 34.” So this is going to literally do exactly the same thing as the other one and I'll actually show you basically what this is doing, so myVar is equal to 35, we know that, and then when it checks it it's going to find that it is less than 36 and actually it's both less than 36 and greater than 34, because I should have made this an “and.”

So if it is both less than 36 and greater than 34 of course we know that's the same thing as being equal to 35, but I will show you how it finds that true and prints the desired result… There you go.

Or maybe instead of this I want to exclude 35 from the possibilities. I could of course do “myVar...” Uh, let me take this out… “myVar is not equal to 35” or I could do “if not myVar equal to 35” but another option would be to say “if myVar is less than 35 or myVar is greater than 35,” so that will just print this if it's less than or greater than, but not if it's equal to. Where it starts to make more sense is when you have multiple variables that you're checking; maybe “if var1 is equal to two or var2 is equal to two” – there's so many options.


But let's say I need more options than just whether or not myVar is 35. You can also use an optional “else” or “elseif” to check for multiple conditions at different times. Note that when you do this the first if is checked and then if it's not true the second one is checked, and so on. I'll show you how that works.

So let's change this back to “myVar equal to 35 then ‘it is so,’” then we can go down here and go, “elseif myVar is less than 35 then Printf myVar is less than 35” – I'll just change this to”myVar is 35” just cuz that's easy to understand; anyway then we could add another one that says, let's see, “elseif myVar is less than, no, greater than 35 and myVar is less than 40 then Printf… is more than 35.”

Now what if it's more than 40 though? I actually just want to do a blanket, like, catch all, if it's anything greater than 40, so instead of doing “elseif myVar is greater than 40” – which I could do – I'm actually going to do “else” and then “Printf(‘number is too large’);” why not?

So in this case what it's going to do is, it's going to check, “is it equal to 35” and if it finds that the answer is yes then it executes this and the rest of this just gets ignored, none of it happens.

If it finds that myVar is not equal to 35; let's say this is 34 instead; myVar is not equal to 35, then it goes, okay, well then is it – or “else,” is it less than 35? Oh, yes, it's less than 35, so it does this and the rest gets skipped.

Otherwise if we change this to 36 then it goes, “is this…? Nope. Well then, is this accurate? Nope, well then, is this accurate? Oh yes, 36 is greater than 35 and less than 40,” so it does this and this part gets skipped, but let's say we change this to 40, then it goes through all of these and it gets here and it goes, “well none of these are valid so we get here, this is the only option left, if none of these are valid then we do this.” And then the statement's over.

So while “else” can be very useful, it can allow for errors in some contexts. Let's say myVar wasn't set prior to running this; we'll just take this out entirely… It doesn't exist, we just run this. In this case “else” lets it slip by and print “number is too large” when in fact the number doesn't exist at all, so sometimes you need to specify every possibility you want to see used.


Alright, so this all applies to comparing numbers, but what about the other data types? Can you compare strings and booleans? Of course you can! Strings are always compared alphabetically if you compare them using less than or greater than symbols, and boolean and nil values can only be equal to or not equal to.

The only things to be aware of with strings, however, is that they are case sensitive so capital “A” is not going to ever be equal to lowercase “a.” For some reason lowercase letters are considered greater than uppercase letters so that may be relevant.


Now if you're quicker than me you may have already figured out how if statements can be useful to you in MA, but just to make sure we're all on the same page I'm going to go ahead and show you how I most frequently use them. I'm going to make a confirmation popup like the one I showed you in the last video, but this time I'm actually going to do things based on which button the user hits when they get the pop up. Now one way to do this is to put the confirm function in the if statement, and I'm going to start with this because it's a great visual. Remember that the if statements look for truth – you can specify “is equal to true” but it's not necessary with a variable that will be a boolean so in this case I go “if Confirm(‘Hey!’, ‘Confirm me’)” cuz I can, right?

I can specify “is equal to true” but that's not necessary because this will already be true by itself, because this is already returning a boolean, so the contents of the yellow parentheses will be whatever is returned from the confirmation popup, which will be either true or false, depending. So going to finish this if statement, then, let's do “Printf(‘User confirmed the popup’)” because we want information, right? Alright, “else,” we'll do, ‘user canceled the popup,’ and of course you can put whatever you want in here because it's just information for you; as long as you know what it means then that's great, but it's nice to have readable stuff.

So I'm going to select all of this and go over to MA3 to run it. So I run it, it says “Hey! Confirm me.” If I hit “Okay” then it says “User confirmed the popup,” if I hit “Cancel” then it says “User canceled the popup,” so now we have information. That's very useful, but there's one problem with this.

So let's go back over here and I'll show you, if I just need to know, “User canceled” or no, then this works, because they either did or they didn't. But let's just say I want to specify the false answer.

If I try to type “elseif” and put this whole confirm function in again it's actually going to run the confirm function a second time. So if the user cancels this one so this isn't true, then it will run it again to get an answer for this, and that's not good because then you have like multiple popups for when you only needed one.

So there is in fact a solution to this! Instead of putting the function inside of the if statement I'm going to remove this and set a variable to the answer. The way this works is, “answer” – that's my variable, remember, “equals” – paste that in there… So then the code gets to here, it runs this and sets the result to this variable, and then we'll put this here, “if answer then… elseif answer equals false then…” and the reason why this works is because, obviously this will either be true or false. So if it's true then this is true, this executes; if the statement that “answer is equal to false” is true then that makes this true, does that make sense?

So we'll copy this and you'll see the same result as before. It's going to look exactly the same when you run it, but what's happening behind the scenes is different. So confirm it, “User confirmed...” Cancel, “User canceled...”


Now just as a little nugget of a thing that needs to be explained and is kind of related, I'm going to explain what a local variable is. When you set a variable the way I've been doing, like this, “answer equals...” whatever, that variable is a global variable, meaning it now exists in the system until the system is reset through a reboot or the variable is changed, and it can be accessed using Lua absolutely anywhere. I could go create another plugin here, and call that variable, and it's going to be able to access it. Now that's fine if that's what I want, but sometimes you actually don't want to find that variable name somewhere else; in fact sometimes you might even want to use the same variable name in a different plugin to reference a different thing.

So to make sure that this cannot be accessed outside of this plugin I can type the word “local” in front of it and that sets the variable as a local variable, which means that it cannot be accessed outside of the containing function or statement. So if I set this in my main function, which is where it is currently, then anywhere inside of this main function after it it can be accessed. It can be accessed here; it can be accessed in here, it can be accessed in here… If I set a local variable in here it can only be accessed within the same if statement; it cannot be accessed in the whole main function. So basically whatever function or statement you set it in, it's local to that, but everything else that's also contained in that can also access it. If you try to call such a variable outside of the function where it is allowed to be accessed you'll get a “nil” because it doesn't exist.


Well, that's all I have to talk about today, but in the next video we're actually going to get into using the command line from a Lua script. This is an important function of Lua that we'll be using regularly later on, so I know you won't want to miss it. Have a great day and I'll see you in the next one!

Comments

Popular posts from this blog

Lua for GrandMA3 YouTube Crash Course

Intro to Lua for GrandMA3

How to Make a Reminder Plugin for GrandMA3 Using Lua