Introduction to Lua Programming | Kyle Schaub | Skillshare

Introduction to Lua Programming

Kyle Schaub, Software Engineer

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
12 Lessons (1h 6m)
    • 1. Introduction

      1:50
    • 2. Installation and Setup

      3:36
    • 3. Variables

      6:04
    • 4. Conditional Statements

      7:55
    • 5. Loops

      11:09
    • 6. Functions

      7:45
    • 7. Comments

      3:19
    • 8. Local and Global Variables

      2:34
    • 9. Tables pt. 1

      7:43
    • 10. Tables pt. 2

      4:13
    • 11. Syntax Review

      6:37
    • 12. Class Project and Wrapping Up

      3:08
17 students are watching this class

About This Class

Interested in learning programming?

The course "Introduction to Lua Programming" provides everyone, even complete beginners, the tools they need to start coding. Lua is one of the most beginner-friendly languages, and it is used all throughout the software industry, from web applications to game development.

The following topics are discussed within the course:

  • Variables
  • Conditional Statements (if, elseif, else)
  • Loops
  • Functions
  • Global vs. Local Variables
  • Tables
  • and more!

Everything you need to get started is available in this class. After all lessons are completed, a short project is assigned for students to apply what was learned and be creative with solving a real programming problem.

Transcripts

1. Introduction: welcome to the introduction Toe Loop programming class. The goal of this class is to go over the basics of programming as well as the fundamentals of using Lua. If you've never program before, this class will be perfect for you to get your first taste of what programming is like and learn all the concepts that are used throughout all programming languages. The lessons are designed for those with no prior coding knowledge, so the content is easily accessible to everyone. If you have program before and you have an understanding of the basics, like what a variable is, how loops work and what functions are. And there may be sections of this class that you can skip over here is how the class is set up. We start by installing all the necessary software onto our computer, and then we'll dive into the fundamentals. This is the part that some more experienced students may choose to quickly glance through or skip. Since it'll be at a beginner level after the fundamentals are discussed, we'll talk about tables in Lieu. Uh, tables are a data structure that could be found in other languages, but the loo implementation is very different. So all students learning Lewis should go through this topic, since they can be a little tricky at first. Towards the end of the class, there was a sin tax review lesson that goes over the Louis specific syntax, which will be a great review for those who are learning had a program from scratch. And it will be a great baseline for those that I've already done some programming in the past and don't need all the fine details that are discussed in the main lessons. Then, after all, is said and done, you'll be assigned a class project to apply the new skills learned throughout the class. This will be an excellent opportunity for you to experiment with the language and actually build something useful. So when you're ready, let's move on and get our computer set up to write some code 2. Installation and Setup: In order for us to get started with writing Lua code, we will need to install a program that can run the code for us. For this class, we'll be using a program called Zero Brain Studio. This program is a lua i. D e e or integrated development environment, which allows us to write Louis code and then conveniently run it directly from the same program. In order to get this installed, you want to go to studio 0.0 brain dot com. That's brain spelled B r A N E. Then, once you're at this main site, you want to go over to here to downloads. Then you'll be given the option to donate to zero brain or, if you choose not to. You could just choose Take me to the download page. Now you're gonna choose the installer for your operating system for me. Since I'm on Windows, I'm going to choose the Windows 32 bit installer. Is this going to download? It might take a few seconds, but then, once it's done, you're just going to run the installer and start the program. When you get the program running, you should see a screen that looks like this. There is possibility that there might be a welcome screen or something. So just choose an option that gets you to this part of the program before we write any code will want to change our project directory to something else. Since the default when we have here is kind of cluttered to change directories, we got to the top when we choose Project Project directory and choose. Now we want to create a new folder somewhere that will store all of our code. This folder can go anywhere. So what I'm gonna do as I'm going to navigate to my documents and I'm just going to right click choose new folder and I'm gonna just call this Lua and then we'll set this folder. So now you'll see over here on the side. We're now in documents slash leua. This is a better working space for this class. And at the moment since this is a brand new folder, it's empty. We don't have any files in it. So what we're gonna do is actually going to save this untitled that lua that automatically springs up into this folder. We're gonna go to file save as and make sure you're in here, Lou a folder. And we're going to save this As variables dot lua variables will be the first topic that we learn about. So this will be a good starting point for this project. And with that saved, the sooner is that the name of the file ah, is now variables right here. And you'll also see it inside of our folder right here. Now to make sure that everything with zero brain works properly, let's put a little bit of code in here just to test it out. We're gonna write the word print than an open and closed parentheses. And inside the parentheses, we're gonna put in open and close quotation marks. And inside the quotation marks, you can put any kind of message you want. I'm gonna put in Hello, world Just like that. Then if you control us to save and then we click this button here, execute the current project slash file. You run that you'll see down in the bottom. We have our message. Hello World printed out to the screen. We're gonna be using prints quite frequently throughout this class, so you should get used to how this works Now that our computer is also have to run Lua code , we're ready to start learning how to program. 3. Variables: So, as I'm sure you've noticed, when we look at this file the text that's written here, his work it's printed out down here. So as we're learning had a program, everything that we want to print to the screen is gonna go right in here. So the very first thing that we're gonna learn about is variables. A variable is a piece of store data whose value can be changed to illustrate this. Let's go ahead and put one into our code at the very top of the file. We're going to write a message equals Lua is awesome and be sure to always save after he types and code in this line, we just created a variable. The variables name is message in the value that this variable contains is the sentence. Lou is awesome. Now, to make sure that we did this correctly, let's print the value of the variable. So instead of what we had here in print, we can just type in message just like this. And if we save and then run the program, we can see down here. It says Liu is awesome. So even though we didn't directly put Louis awesome into our print. It reads the value that was contained within message to figure out what it needs to print out variables, construe or all kinds of data. In this case, it's storing text in programming. Text like this is considered a string. A string is just some words or letters surrounded by quotation marks. A variable could also store numbers. For example, we could set our message equal to five. Keep in mind that we do not want quotation marks around this number, since we don't need it to be a string. Same thing will happen here if we printed out so we can save and then run and it says five down the bottom. We can have pretty much as many variables as we want. For example, let's put in another variable. We'll call this chicken and we'll set this variable to 10. So now we have the message variable that contains a number five and the chicken variable that contains 10. If we want the program again, we'll see. That still prints out five. And this is because we're still printing message. It doesn't take chicken into consideration for what it prints out at all. Let's go ahead and make 1/3 variable, and we'll call this one output. But let's do something a little bit different for this one. Instead of setting a equal to a new value, let's write. Output equals chicken. So what does this mean? The variable named output now contains whatever value that chicken contains. And let's double check this by printing out how put, save and run and says 10 So chicken contains 10. Output contains the value that chicken contains, which is 10. And then we print out output, which, as you just said, contains the number 10 in programming. Variables need to be adjusted and changed based on what needs to be done. One way that you can change the values round is by performing arithmetic on them. What this means that we can do addition, subtraction, multiplication and division on these variables. Let's go ahead and try that out now on the same line. Instead of just setting output equal to chicken, let's try setting it to chicken plus one every save and then run. As expected, we get 11 from the value of chicken, which is 10 plus one, and like I mentioned, you can do other kind of math operations. On this, you could try times three, for example, if you save and then run that we get 30 10 times three is 30. Keep in mind, though, that this math Onley works. When we're dealing with numbers. Let's try something out. Instead of sitting chicken to 10. Let's set it to a string. We can say math is fun like this. So what's gonna happen here is gonna be a little strange. We have the string Mathis fun and we're multiplying that by three. Now. That doesn't really make much sense now, does it? If we try and run, we get an error. The program does not run successfully, and it actually tells us what the issue is. It says We performed arithmetic on global chicken, a string value which, as you just mentioned, you can't perform arithmetic. You can't take a sentence. Times three it is doesn't work, and it even tells you what Lina was on its on line three right here. A while ago, I mentioned that variables are able to be changed. Let's go ahead and show that right now the variable message is initially set to be five. What if later on the program. We want the value to decrease by one. Nor to do this we could do message equals message minus one. What this means is that message is being set to the current value of message, which is five minus one. So the results of before and if we print out message and we were on it, we still get the air because chicken is so famous by by three. So let's get rid of that. The seven run once more we get four. So message sort of five minus one is four as being set to message. And if we put that out, we get for that about covers up the basics of variables. These are the foundation of any program, and having a good idea of how they work will make you very capable, developer. Next, we're going to move on to conditional statements, which are ways of checking the data in variables in order to decide what the program will do next. 4. Conditional Statements: in this lecture we're going to cover if else if else statements. These type of statements may be known as conditional statements because they determine whether or not a section of our code will run at a given time based on the condition. So let's jump right in for this lecture. We're going to go over here. We're going to create a new file in our product directory. We'll call it condition ALS that Lula and go ahead and open that up. We have a blank file. We'll start this off simple, and we'll have a variable called message said it. Zero and we'll print message, verify it works. Make sure you save and let's run. And, as expected to get zero, let's go ahead and actually add another variable. If this program were gonna call it condition and you could set this to whatever number you like, I'm gonna set mine to 25. We're going to use this variable as part of a conditional statement that is going to change the value of message. Go ahead and put in the following code. If condition is greater than zero, then message equals one, and then you had to put an end to here. This line reads as if the variable condition contains a value that is greater than zero. Then we're gonna do this line pretty straightforward, right? So in our case, since the condition variable holds the value 25 do you think it will be printed out, figurehead and save and we run it. Prints of the value one. So what happened here is we compared The number 25 25 is greater than zero. So this line happens. Let's actually change 25 to negative 25. If we save and run that we're printing out zero now. This because negative 25 is not greater than zero. So this line everything between then and the end does not happen. Messages never changed a one here. We're going to add another conditional statement to our code. It's going to be the same idea as before, but this time we're gonna check and see if condition is less than or equal to zero. So say if condition is less than or equal to zero, then well, say message equals negative one. And so same idea. We're gonna be comparing the number negative 25 or whatever condition holds and see if his less than or equal to zero, meaning that could be a negative number or zero. And if it is, then this line will execute. So if we save and run this time, we see that negative one is printed out. So, overall, what happens in our code is we have our condition. Variable negative. 25 we ask, is it greater than zero? It is not rare than zero. So this line does not happen and the code moves on and we get down to this line and we ask , is condition less than or equal to zero? So is negative. 25 listener equals zero. It is. So then this line occurs, messages set to negative one and then we print it. Which explains why we get negative one down here now, writing our code this way with two separate if statements is OK. But there is a much easier and more efficient way of writing this exact same thing. We're gonna get rid of the second if statement completely, and we're gonna actually add to this first section. We can use the keyword else here like this and say message equals negative one. So this else just means if this condition is not met, then we're gonna do this line. In other words, say condition is negative. 25 his condition greater than zero. It is not so. Then we go to this l section and perform this instead. And if we save and run, we get negative one again. So this condition was not met. So this line does not execute. And since it's a case, we go to this one instead since it's in the l section. So up until now we've user ifs and our else statements. But there is one more conditional that we will work with that is sort of a combination between these two and is called the else F. Let's go ahead and put else if into our code, to show how it works. First of skippered of this L section. Now we'll add the else if where that was will say else. If one word condition is less than negative 10 then message equals negative one. Like I mentioned else. If Zara combination between else and if they're like else, is because they only execute if the previous condition was false, So in this case, this lion right here would only be reached if this line was not true. So if condition was not greater than zero But then what happens is the else if adds a different conditional to its section. So once the code reaches the else, if it'll check to see if the condition is less than negative 10 before executing this section of code after it. So this whole section reads as if condition is greater than zero. Then do this. Otherwise, if condition is less than negative 10 than do this in our case, condition is negative. 25. So this is not true. So we do not execute this. Instead, we go this line and we ask, is negative 25 less than negative 10. Well, it is so messages set to negative one. And if we save and run, we still get negative one. But what if we were to change its negative 10 to negative 100 for example, If we tried that the next 25 would not pass this section, we would go to this line and ask is negative 25 less than negative 100. And that is not true. So this line is not execute either, so essentially nothing happens to message. It started zero, and it ends and prince with zero as well. What we can do to make this even more interesting is we can add in else to top this whole section off after the else if we could just right in else, and we'll set message equal to about no conditions met like that. Remember, the else statement doesn't have a condition attached to it. So if the program ever reaches this line, it's just going to execute what's contained within it. So in our case, we have negative 25. It does not pass this condition. So goes to this one. It also does not pass this one. So it goes to the else and just executes this. We save and run. We get our output of no conditions. Met sections of code like this can have as many else ifs as you want. Keep in mind, though, that you can only have one if and you can only have one else. But in between, there's no limiting the number of else ifs. For example, let's put in a 2nd 1 will say else if condition is equal to which is done by doing to equal signs. Negative. 25. Then we'll set message two. How about 1000? So in this case, we're gonna reach down to this section we're gonna ask is negative 25 equal to negative 25 . And since it is, message will be sent to 1000. If we save and run, that is the case. 5. Loops: The next concept we're gonna go through is loops, and there's a reason I'm showing you these right after conditional statements. Loops are sections of code that run more than just once, based on certain conditions to illustrate. This will jump right in and work with our first type of loop the while loop. We'll start by making a new file. Will name this loop stat Lua. We'll keep our code simple. At first we will have message equals zero and print message and make sure it works. We get zero. That's good. Now, right after the message line, we're gonna put in our first loop and this is how you do it. The right while message is less than 10 do And here were the right message equals message plus one and end. Now let's go through this section of code line by line to get an understanding of what it does. We start with. Wild Message is less than 10 Do. This is very similar to an if statement that we learned about in the previous lesson. The condition is that message is less than 10 and if it is, we move on to the next line on the next line we see that message is increased by one. What makes us go different from an if statement is the key word. While it means that we're not a loop, and this section of code is going to keep running and running into the condition is no longer met. So in this case, the code is going to get to this wild loop. And since messages lesson 10 0 is less than 10 we go into this line or message increases by one. So now messages one. And then at this point we go back to this line and we ask again, Is message less than 10? Well, now messages one. So one is less than 10. So we go into this line of your message, increases to two, and you see what's going on here now, too, is less than 10. So we go three and so on until eventually we'll ask is nine listen, 10 It is so the nine goes to 10. So now messages equal to 10 and we ask, is 10 lesson 10 And here finally, that is not true. 10 is equal to 10. It is not less than 10. So that's when the wild it breaks and we go to this end and we move on. So based on an example, if we save and run, we'll see that message is now equal to 10. That means that this section of cobra here ran 10 different times all within this one small section. Keep in mind that the whole process that we just talked through happens instantaneously. Since computers are incredibly fast, the program comes across this wild loop and can almost immediately get through all iterations. Even though this message equals message. Bust one happens 10 times during the loop. It happens all in an instant. I mean, you saw what happened. We quick to execute, and it immediately returned 10. And this is not limited to just 10. We could change this to one million. And if we save and run at this time, it still happens in an instant. Even though this line here ran one million times, it doesn't really matter. We're gonna change us back toe 10 as we move forward, though speaking of which this message equals message plus one is a very basic example Between the do and the end, you can have any amount of code, and all of it will be executed as long as the condition is true. For example, we can add a second variable called Test, and we'll set it equal to zero. And within the loop, we can do something like test equals test minus five. Now remember to put test and here for the print statement. So we're doing two different lines of code, but each of them are going to be executed as long as the condition for a while. It is true. If we run, we'll see that we get negative 50. So that means that this test eagles test minus five occurred 10 different times, giving us a total of negative 50 for this print statement. Remember how I mentioned that the loop will run until the condition is no longer true? Well, as a programmer is your responsibility to make sure that whenever you use a while loop, it will eventually end. Otherwise, you're loop will keep going on and on forever as a learning experience. Let's go ahead and see what happens when we do this inside the while loop. Let's get rid of this message equals message plus one. So now miss. It starts at zero, but it will never increase. It's always going to be less than 10. So this while loop will keep going on and on, it will never break. Now I'm gonna run this, but just a word of warning. If you want to try this yourself, make sure you know how to force Kloza Stuck program. Normally, you could do this through the task manager on Windows or a Mac. You can force close programs by right clicking on the icon and choosing force quit. So I'm gonna save, I'm gonna run and it looks like nothing's happened. It's kind of like all these buttons are repressed, Nothing's really going on. It looks like the program is stuck. And that's because this while it is still executing even as we speak, so is going through a never ending. So I'm gonna have to force close or possibly yeah, I could just stop. So if you're using zero brain, you can just click the stop button right here. Now, before we forget it, let's put that message equals message plus one back in the code so that we don't get that again. Now that we have a good understanding of how while loops work are going to move on and get into the second kind of loop called the Four Loop. This kind of Lupus just as useful as a previous one, but it's useful for different situations in programming. You'll notice that there are normally many ways to do something, so you'll always have options for how you go about solving a problem. So to move on, let's start by making a new variable at the top, and I'll call it Pickle, and I'll set it equal to zero at first. Then at the bottom, we're gonna make it so it prints out the value of pickle. After the while loop, we're gonna put in the following code for a four loop four. I equals one comma, three comma, one do and we'll do. Pickle equals pickle plus 10. And and now, looking at this code, it's a little less self explanatory in the while loop. So let's go through a bit by bit so we can better understand what's going on here. The first line says vice three different values. First we have I equals five in a four loop. This is going to act as our generator, which basically means that it is a value that is going to be updated every time this section loops. The next number in the sequence is three. This is the number that are it. Aerator will approach. And once this number has reached the Lupul End, and finally, the last number one is the step value for the loop. In other words, it's the amount that our generator is going to change each time the loop goes around. So I know that was a lot of information to take in all at once. So let's go through what will happen when this loop runs the Lubell start with I starting at one and the code inside the loop will execute. So now pickles want increased from 0 to 10 and now the loop will go back to the beginning. In our generator, I is going to increase by whatever this number is one. So now I is equal to two. Since one increased by one, it's going to check and see if the operator is equal to this value, and since it's not, we go into the loop once more and we say pickle equals pickle plus 10. So how pickle is 20 then it goes back to this line once more. I will increase by one again. So now eyes three and it compares that three to this three. And this time they're the same. The it aerator. I increased three after the loop cycled a couple times. So now these numbers are the same. The Lupul then run its code one last time. So now pickles 30 and then it will end. So if we save and run this time, we see that pickle is equal to 30 now at face value. This four loop might look a bit more complicated than the while loop that we did earlier, since there are a few more numbers for us to keep track of. But really, it's pretty simple. If you take the time to think about it, loop will start at this number. Continue until it reaches this number, and each time it will increase by this number and the amount of flexibility that for lips provide is very nice. What I mean by this is that you could make the starting value a small value, like one. The way we had it, where you could have a start at a negative number where you could even have it started a very large number and count down. For example, by changing the increment value to native one, we could change this to five. How about and what changes? 3 to 0. So now, in this example, Eyes five, it's going to count down by one each time until it reached zero. Now again, remember that you need to be mindful of how you're doing each of these kinds of loops to, because if there is no way for the loop to ever end, our program is going to freeze up on us like it did before. Something interesting and very useful that you can do with four loops is that you can use the generator variable of the loop inside the code itself. So first I'm gonna change his values back to what they were 13 and one. And then how about instead of increasing pickle by 10 each time we increase it by I So we're actually using this variable here within the loop code itself. So the way this works is our loop starts eyes one we could down to this line pickle is going to increase by I or it's going to increase by one that is gonna come back to this line eyes going to increase to two, because this is one. So now I is to get into this line. Pickle increases by two this time. So people increased from 1 to 3. So now I increased it to three. These equals this number. So we go on one more time. Pickle increases by three from 3 to 6, and then we end. And if we save and run that we should get six. And there it is. It is very common for you to use the I value within your four loops since accounts up and you may need that number within your calculations. 6. Functions: in programming, you should know that copying and pasting your code is not very good practice. If you ever find the need to copy a section of your code and pace it in another spot so that there are now two sections of the same code in different spots, you should instead consider using a function. A function is a section of code that you define, and then you can call that section of code to be run elsewhere in the program. You'll understand what I mean. In just a moment, we're going to start by making a new file. We'll call this functions that Lula go ahead and open it up and we're gonna start simply. Same as always, Message equals zero and printing out message. But this time after message, we're going to write a function that increases the message by five. To do this, that's right function Increase message like this with you parentheses at the end and inside will write message equals Message plus five and we'll write end Pretty simple right. However, you may notice that when we save and we run the code, the value of messages still zero, despite the fact that we have this line here. Message equals message. Supposed five. This is because this code in here doesn't actually execute until we call the function itself. We call the function by simply writing the name of the function increase message just like this. So whenever we write this line right here, it's going to search a file for this. Our function increased message. And then when we call it, it's going to just take the code. It's in here and run it. So since we have this line here now we can run the program and we see that we get five. That's because this now runs the code is inside increased message to be even better. Illustrate this. We could call this function more than just once. So if we run now, we get 15 as the value of message. That's because this line called this section once, so it's now five this line. Increase it to 10 in this line, increase it to 15. So all this code here was called three different times. There was no need to copy and paste this line here. Now we're going to make our function a little bit more interesting. See, we need to find a function. You have the option of giving it parameters. Parameters are what go in between the parentheses in here for this function, we could change it. So the function accepts one parameter, and we'll call it I. Now that the function except perimeter, we can use that perimeter within the function itself. So in this case, how about instead of increasing the message by five, we increase it by I like this. With these changes in place, our function acts a little bit different. Now, whenever we want to call the function, we now need to provide a parameter as well. So since it accepts one parameter here, I we have to also pass it a value four i for this increased message call in this case, we could say to So if we save and we run, we get to meaning. Our message now contains a value of two. And this happens because we called increased message, passing a value of two for the perimeter I and since icicle to to hear message increases by two. And this works when we call it other times as well. We could set it to five here and set it to three here, so we're increasing message by to them are increasing it by five. There were increasing it by three, so we should expect See 10 right there at the bottom. Another thing to keep in mind with functions is that you can have more than one parameter I have to do is just put commas in between the different primaries you have available Now. One very important thing to know about functions is that you have the option of returning of value to explain what this means. Let's go ahead and ultra function so that it returns something instead of having message increased by I in the function. Listen said, Make it so that the value of I doubles. We could do this in a similar way just by doing I equals I times to like this. Now the function no longer changes the value of message in an instead alters the parameter value within the function with the value I doubled. Let's do return at the end of the function to return, I will say return. I just like this With this return statement in place, the function acts a little bit differently than before when a function returns the value, it basically means that when the function is called, they will put the value that is returning in its place. You'll see what I mean into the second first, let's run the program and see what happens. Message is still zero at no point does message change from zero. All we're doing is calling increased message a few times and, as we just discussed, is not touching. The message is very Bledel. But what we could do is we could say message equals increased message just like this. Now, remember what I just said. Whenever you have a return statement within the function, it basically means that this value that it's returning is going to replace this entire section here. So here's what's gonna happen. We're sitting message equal to increase message, and we're passing increased message of value of two. So the code is going to go up to this section is gonna say, OK, I is equal to two right now. So then I is going to double. I is now equal to four, and then we're returning the value for So essentially, after all this runs, it's as if the number four was here, but it does all these calculations on the fly very quickly. So if we save and run, we see that message now contains the value for So with all that done, we just read a function that takes a value, doubles it and returns it. The reason returning values is so important is because you will not always be using these functions for the same variables. By that, I mean, before we had it. So the function increase the value of message. But that means that the function on Lee works for message, and it couldn't be used for any other variable. What if I made a second variable? And I wanted that one to work with the function as well? With returns, you can get around the problem completely with functions. A cool thing that you can do with them has passed other variables as parameters. For example, at the top of our program, we can make a new, very bill called Haram, and we'll set it equal to 500 for example, then down here an increased message instead of just passing it. The number two, we could pass it. Haram, it's going to take the value that Paramus contains, which is 500 pass it into the function. So we should expect to see 1000 which is 500 times to as the value for message. Having functions in our tool belt will be incredibly useful. As we move forward with programming, you'll see how often sections of code need to be ran at different points in the program, and it is much easier to manage your code when it's located in one place, rather than copy and pasted throughout the file. 7. Comments: So far, everything that we've written in our Lou Files has been lua code, but with pretty much every programming language. There is the option for programmers to put in little notes that aren't treated his code. And these notes are called comments. For example, here in our code from the previous lesson, we have a function called increase message. If I'm writing a bunch of code and have a ton of functions written, threat my files, it might be difficult to keep track of what each function does, so it is helpful to write comments into your code to help you remember in Lieu, uh, you write your comments by typing Dash Dash like this, then anything else on that line is true, does a comment and isn't actually read as code, so I can write something like, uh, this function doubles the parameter. Something written like this couldn't just be placed in the code, such as Like that it wouldn't make sense being treated as Louis Code, but since it's Street as a comment, it doesn't actually get read. It's just there for the programmer or anyone who is reading the code as a little reminder, a cool little trick that you can do once in a while is called commenting out. Some code comments are helpful for these little reminders, but you can also use them to take out sections of your code without actually deleting them . For example, if I run the program as is, I expect to get an output of 1000 because program has a value of 500. But let's say one. See what happens when this line does not execute. Well, I could just delete it. But then what if I want to bring it back later on? Instead, what I could do is I could just make this a comment since, like I was saying, this code, it's a comment is not treated as riel Lua code. It's just there in the text file. So if I save and run now, we get zero because this line ever executes and messages never updated. Now, if I want to bring this line back, I just get with comments. Additionally, there's another way to make comments, and that is a block comment. So if I do dash, dash and then square brackets square bracket now, everything after this line, you'll see that the rest of the file changed colors. All the rest of this is being treated as a comment. But if I want to close this, I just do, Ah, square brackets square record like that. Now everything between this and this is Trias a comment. In other words, this function doesn't actually exist in our code anymore. It's a comment, and I could get rid of it just as easily. Putting comments into your code is extremely important. As I mentioned before. It's great for your own benefit because it will help you remember what each of your code sections do. But to add to that, if someone else wants to look through your code, comments make a huge difference in understanding what you as the programmer we're trying to do while you were writing it. Reading other people's code can be hard at times, but having comments to guide you along makes the process much, much easier. 8. Local and Global Variables: in Lieu. Uh, and most of the programming languages. There are two types of variables. Global variables and local variables. So far in our lessons, we've been working with global variables. Global's are things like message up here. Once you typed this line out, you can use message anywhere else in the program. It will always be recognized, and that's what the word global refers to. It could be accessed from anywhere we could type message anywhere in the file, and the program would know that we're referring to this variable up here. The alternative to a global variable is a local one and only had to do in order to make a variable. Local has put the word local right before it. Just like that. As the name implies, the local variable can only be accessed within the same section of code at the variable was declared in. So let's get rid of this one for now. And let's take a look at increase message here. Let's say we want to use a variable for calculations within this function. Let's go ahead and rewrite this function to do something else. On the first line of our function, we're gonna make a new variable called to the bar and we'll set it equal into I. Then on the next line will do. Var equals var divided by two and then finally we just return of our This function takes in a value and returns half that value. The variable var is only used in this section of code and nowhere else, and as a result, it would be best if we made this local. Now this var variable is local to this function. It's local to this section of code right here, and that's it. We couldn't say set message equal Tovar, because of our is not recognised outside of this function here. Using local variables when applicable is important for managing your project. When you write your programs, you will have a lot of different variables and if they're all global and it is more likely for mistakes to be made, an example is what if you made a second function later on, and in that function you also want to use very bull called of our. This could potentially cause some issues because it might be unclear for a program which var variable were referring to. However, if everything is local to its own section of code. Then there is no risk of the program misinterpreting what we met. 9. Tables pt. 1: everything that we have gone through up to this point is pretty standard among all programming languages, ifs, else's functions, variables. You'll find all these things and pretty much any language use. However, in this lesson, we're gonna talk about tables and Lou. Uh, and this is something that you won't find in just any language. They're all gonna have something similar. But Tables and Lou are one of a kind, incredibly powerful tools. You use them to store sets of related data, and since that data is all stored in the same place, it makes it much easier to access, and it keeps it organized. We learned about variables, right? A variable stores, one piece of data and that's it. One integer, one string, one floating point number and nothing else other than that one thing. A table is capable of storing multiple pieces of data on the same variable, except it's not a variable. It's a table. Let's go through an example of making a table, and we'll start with our regular routine of making a new file. Will call this tables that Lua and in here will do Message zero and print message. Then we're gonna make our first table. We're gonna call it test scores like this. And when you create a table, you're at the table name. We just test scores and then you said it equal to open and close Curly braces in lieu. Uh, if you ever see these curly braces, you know you're dealing with a table. And in this case, since there is nothing between the curly braces, that means it's an empty table. So here we define test scores, equals and envy Table. As you may have guessed, we're going to store a bunch of made up test scores in this table. To do this, we're going to assign a score to his own index in the table. Let's go through how to do this. First, we want to write the name of the table and then put square brackets after it, like test scores and open and close square records. Then inside the brackets, we're going to put Index. You'll see what this means in just a moment. We're going to start with Index one and an Index one. We're going to put our first score, which is 95 and there we go. We assign our first test score to the table. Now we can move on to the second test score, and we do that by simply doing the same thing. Test scores at index to equals 87 and the pattern just continues on. We're gonna do one more will do. Test scores. Three equals do 98 and that's basically it. We now have a table called test scores that contains three different scores. Now, in order to access that data that we just put in, we simply write the table name and refer to whichever index we're talking about. For example, let's say we want to set a message equal to the score at index number two. We just right test scores to, and what this line does here is it'll access test scores, find the value that sort of index to and just assign into message. So in this case, we should expect to see message equal to 87 because that's what sort of index number two keep in line that we only assign values to the indices 12 and three. What would happen if we tried to access the next four? We didn't put anything there, so what would ever turn. Let's go ahead and try it instead of putting to here. Let's put four and be sure to save and let's try running. It says nil. Nil simply means nothing. Nothing was stored in index for see with tables. Any index that isn't assigned something is given mill by default. Neil is a key word that you may see once in a while, and basically it just means nothing. So the way we assign values to this table is OK, but there is a much easier way to do this. Instead of writing out three different lines for three different scorers like we did here, we could get rid of all these and put those values inside the brackets up here. Let's try that. All right, 95 comma, 87 comma and 98. So this does the same thing that those previous lines were doing. It puts 95 in next 1 87 in next two and 98 at Index three, plus, try accessing in next three real quick and we get 98 which is what was expected. Although that is the easiest way to put values into a table. There is one other method to doing this. That is still important to go through. Well, we do this by making the table start empty again. Now there's a function in lieu of that we can use called table dot insert. Remember from a lesson on functions how we wrote our own functions do certain things. Well, Lou comes with a bunch of functions written for us that we can use as well, and this is one of them. What this function does is simply takes a value and puts it into a table. The function has two parameters have been to define before we can use it. So let's get started with this was a table that insert, and the first parameter is the table that we want to insert into, which is test scores. And the next parameter is thieve value that we want to insert into the table, which will use the first score that we were working with before, which was 95 and close it off so just tested. This is working. Let's look at index one save and run and we get 95. So this line successfully put the Value 95 test scores, and we can put the rest of the scores in by just copying. Facing this line here just to verify at next three, we get 98. Once again, this set up is the exact same as the previous two. However, there is a very important reason that I'm showing you this method. See, in the other two ways of putting values into the table, we knew which indices we wanted our values to go. We knew we wanted 95 to be the first, then 87 than 98. But you will run into situations where you won't know which index Iran you'll write code, it says. Put this value into this table, but you won't know at the time how many items on the table. So this insert function figures that out for you. Just a little side note when assigning values to your table is standard to start at index one than two and so on. But you don't necessarily have to go in that pattern specifically. In fact, you don't even have to use numbers as an index at all. You could do something like this where we right two scores and use this string of meth as the index and give that our score. So does this. And then, in order to set our value, we just do that and we should get some gate. You'll see over time just how flexible tables are and how you can accommodate pretty much any situation by using them. 10. Tables pt. 2: When you have tables full of data, it's important to be able to efficiently and easily go through all that data to either find what you're looking for. Do some calculations or do whatever it is that you need that information for. Luckily for us, Lua gives us a very easy option for Iterating through all of the tables entries and examining the data it contains. Remember the four loop that we learned about in the section on loops? What? We can use them in a new way in Ortio easily go through a table. Here's how we do it and start the section. I'm actually going to change this back to two and get rid of this. So in order iterated through all the items in a table we're gonna do for I come A s in high pairs tests Gore's do and we'll do Message equals message. Plus this and and I'm actually going to get rid of this assignment down here. This loop will go through each item in the table and for each item it'll perform. Whatever code is inside, this could block the I appear refers to the current index of the table, the loop is on and the S refers to the current value at that index. Let me explain exactly what the sleep will do. The loop will start by looking at the table test scores, and it will find the very first value that it contains. So that means that the loop will start with I equals one and s is whatever the value at Index one is. In our case, we have the first value being 95. So with that said, they will move on to the contents of the loop where it's going to increase message by s or it'll increase message by 95. That is gonna go back then eyes going to increase to two and s is going to be the value at index to which is 87 then message going to increase by 87 this time. Once again, I was going to increase the three and s will be 98 this time, and message will increase one last time and in the four live will be over. All that being said, message will end up being the sum of all three of these values, which is 280 and even though in this example, we only use the value of s in the code block. You can use I as well. For example, instead of adding s, we could try adding I so like the way I described before. It's gonna go through this loop three different times, but this time is going to use eyes value so it should add one plus two plus three, which is six. There's one last thing about tables that I'd like to go through. One cool feature about tables is that you can add properties to them, which is basically a variable has tied to the table itself. Let's go ahead and add one toe. Our test scores table. We're going to add test scores. Dot subject equals the string math, and that's it. Now we can access that value that we specified at any time by simply typing test scores dot subject, For example, instead of typing message output, test scores dot subject. If we save and run, it outputs the string meth because that's what test scores. That subject is equal to calling this a property is a little bit misleading because actually, just a piece of syntactical sugar, which does Essentially, this is the same as doing subject. And if I run, we're gonna get math again. Is this a little trick that speed things up? This is a lot easier to write than this, and it feels more natural. Toe Add properties two tables in this way and that concludes, all the information you really need to know about tables at this time, you'll learn, is over the course of your experience with Lua, how powerful these are and how they could be applied to many different situations. 11. Syntax Review: The purpose of this lesson is to go back through all the basic syntax that was discussed in the previous lessons. To do this were at syntax station dot com, which is a website that I put together a while ago to briefly give you a little code snippets that help out with remembering what the basics in Texas of various languages. So in our case, once you noticed in taxation dot com who want to navigate over to the Louis section and you'll see we have a bunch of different sections here that show different principles of using basic Lou, Uh, so we'll start off with the variable section very well. Start with the variable name and you said it equal to whatever kind of value he want to give the variable. And there's lots of different data types, such as regular integers or floating point numbers or strings or even Boolean values. True or false comments work by using a dash dash, followed by whatever comments you want to work with and blocked comments. Work with dash, dash and square brackets than everything after that will be considered a comment, and you close it with these two closing square rackets. Doing arithmetic with variables works in this way. You have, ah, variable that contains data and then you simply use Ah, whatever operator you want to use in this case we're using Plus, but there's also subtraction, multiplication division, Ma division and exponential. Now for conditional. If else else ifs, they work in this way. You have if and then your condition. In this case, my name is equal to zero. Then if this condition is true, everything within this code block will be run. So in this case, my numb will increase by one. And here's a quick list of all the different operators or comparison operators that you can use. And here's an example of a series of different condition. ALS. We start with our If so, this will run. If this is not true, then we'll go here. And if this isn't one, is not true as well. Then we go to the else and in this is run. Here's the section on loops. We'll start with the while loop. Here's how these work with his wild keyword. Whatever this condition is here, this section of code will continue to run. While it's true. So in this case, my numb assuming it starts at zero, will count up from one all the way up until 50 until this condition is no longer true. In which case the valuable end and four loops work. Similarly, we have r I equals starting at one in this case and accounts up by one until it reaches 100 . And you may notice that we didn't include this number like we did in the lessons. This number is actually optional. If you do not have it, it is assumed to be one. So in this example, this for loop counts up from 1 to 100 where I increases by one each time in this example. Down here I starts at 100 but at every iteration I decreases by five each time. Here's the basics of functions. Here's the syntax. You start with a keyword function and in this is the name of your function. Then you define your open and closed parentheses here and this value. And here is our parameter. You can have more than one parameter like we do down here, but it's not necessary. Then inside the function, you can reference the parameter directly like we say in whatever is passed into the function is what and will be. And then this is how you call it. You simply write the name of the function, do the open, close parentheses, and then whatever value you want to pass in, in this case, repressing in negative 15 and that will act as n within the function. And here's a case of parameters. A little trick that you can do in lieu is that you can assign variables to be functions themselves. Notice that we do not have the open and closed parentheses here like we do in this case. This is an example of us calling the function meaning the code, and here is actually being run. This is an example of us referencing just the name of it. So my var now acts as is negative function. So when you call my bar like this, it's going to run the code up here because my bar is now equal to is negative and filing tables. All tables can be set to be an empty table by writing opening close curly braces. We have my table at index one. Use a square braces to refer to particular indices and in this case, presenting the character A to be at the next one. It's important to keep in mind that in lieu, uh, tables start at Index one, which is pre abnormal Tau other languages, most the time it starts at zero. But Lou is its own thing, where it starts at one, but you don't even have to use numbers as the index. In this case, we're using the string Lua as our index, and we're setting it to 20. And here is an example of us removing a value from my table. He just said It's a nil. We talked about how in the lesson on tables if there is no value assigned to an index, it just contains nil. Well, if you assigned nil to a Nen decks that essentially removes the value from it, it's treated as empty. Here's an example of us using property. I use property loosely here, since it's not exactly a property, but you can reference my table dot name at any point after this line occurs, and here's the shorthand way of creating tables. This will put the value too, and next one, the Value three index to in the value five and next three. And here's our trick of iterating through every value within a table. You have the four I comma in in I pairs and just inside the parentheses whichever table you want to iterate through. So here we're entering through other table, which is this one. Is this going to go through two, then three, Then five in this case were adding up each of the value. So we should get 10 because writing to post week was five. So if we were to output result, we would get 10. So as you're working on your own projects, be sure to come back to this website and reference these different code snippets that you have a better idea of how to do these simple things. 12. Class Project and Wrapping Up: time talk by the project For this class, it's pretty simple, and with everything we've learned, you now know what you need in order to complete it. First, I'll need to download this project template file. It should be available in the project section of this class. For some reason, you can't find it. You can just copy the code from this video. It's fairly short. And here's the problem. I give you this function here called some all numbers, but at the moment it doesn't work quite right. This function is supposed to take in a number which will call Numb like the parameter, and it needs to add up all integers between one and numb. For example, if we passed in four and the function would add one plus two plus three plus four to get 10 then it would return the number 10. This last line down here. It's just that you can see the output it'll print to the screen, the final some is, and then your answer when you pass in 12 to the function. If we run it as is, we can see that it says the final some is zero and this is just because we're returning zero right here. We're not returning the correct answer when we pass in 12. And that's the goal of this project, by the way, with 12 some is 78. So at first you should aim for getting that number. Correct. But of course, you should be able to put any number in here, no matter how big. And it will still give you the correct answer. The total, not a code you need right in here should only be about five or six lines. In order to solve this problem, think back to previous lessons. Which of those tools would come in handy here, toe Add up a series of numbers. I don't want to tell you which one, but we briefly did something very similar to this in one of the lessons. And one of these programming concepts over here works very well with this problem. When you come up with a solution, be sure to take a screenshot of your code here and post it to the class project page. I'll take a look at it and give you some feedback. If you spend some time thinking about this problem and you can't quite figure it out. Be sure to conduct me and I'll give you some hits. Until then, good luck. After you complete the project that will conclude this class. Thank you very much for going through this material with me. If you have not already, please leave a review, and if you enjoy the class, please recommend it. I greatly appreciate the support moving forward. I recommend diving deeper into each of these topics discussed in the class. We only scratched the surface of what Lula has to offer. The language has excellent documentation and tutorials on lulu dot warg. So I highly recommend checking that side out. Also, feel free to contact me with any Lua or general programming questions. I'm always willing to help. However I can. I always like hearing about what kind of projects students are working on, or if you need some ideas or guidance moving forward, I'll be glad to provide some input. With all that said, I believe that covers everything thanks again for taking my class and look forward to seeing you in the next one