hello folks in this video I'll be going through the fundamentals of programming with Lua you don't need any prior coding experience and you can follow along in your browser if you go to lu. org then go to download then go over to live demo there are a few different site options that you can go to Each of which has a text editor and you can run Lua code directly in your browser the very first thing that we're going to discuss is variables a variable is a piece of stored data whose value can be changed to demonstrate this let's go ahead and put one into our code at the top of the file I'm going to write message equals and then in quotation marks Lua is awesome in this line we just created a variable the variable's name is message and the value that this variable contains is the text Lua is awesome now to make sure that we did this correctly let's print the value of this variable so in print instead of hello world let's go ahead and pass in message so then when we run we'll see the text Lua is awesome it's just going to grab whatever value this variable contains and all kinds of data can be stored in this case it's storing text and in programming text like this is called a string a string is just some words or letters surrounded by quotation marks like this a variable could also store numbers for example we could set this message Vari variable to five just the number five and keep in mind that we do not want quotation marks around five because we want it to be treated as a number not a string and same deal as before if we're printing out the value of message we should see five over here in the output we can have pretty much as many variables as we want for example let's put in another variable and I'll call it chicken and let's set it equal to 10 so now we have a message variable and we have a variable but even when we run it's still going to say five over here because we're printing message specifically let's make a third variable and we'll call this one result but this time let's do something a bit different instead of setting it equal to a new value let's set it equal to chicken so now the variable result now contains whatever value chicken has so since chicken has 10 and let's try print result this time we should see 10 in the output because result contains whatever value chicken has and we're printing result when programming the value stored in a variable could change later on in the program so even though for example chicken starts off at 10 I could later on change it to chicken equals 25 for example and when we print chicken we should see 25 because even though it starts off being 10 this line changes it to 25 5 and we print it out at that point but this result variable is set to chicken here on line three and at this point since the program runs from top to bottom chicken is set to 10 result is set to chicken meaning that result actually contains 10 and we can see that if we print result and run it is 10 another way that variables can change in your program is by performing basic arithmetic on them what this means is that we can do addition subtraction multiplication or division with these variables so let's try that out with result instead of setting result just equal to chicken let's set it equal to chicken + 1 so what this does is since chicken equals 10 we'll see 10 + 1 and when we print result we will see 11 it performed this arithmetic right here and you can perform any kind like I said you can do minus one or times uh say time 20 we'll see that says 200 or you can do division and similarly we'll get the decimal value keep in mind that this math only works when we're working with numbers if say chicken was a string like hello well this suddenly becomes strange because we're trying to take the string hello and divide it by 20 and that doesn't really make sense in fact when you try to run we'll see an error and when you're programming it's very common to run into errors it's just a natural part of learning and just a natural part of programming even experts run into errors and here we can see that it is um attempting to perform arithmetic on a string value Global chicken uh so it just you can't perform arithmetic on strings and that's why we would need to change this back to a number in order for this to run properly and even after a variable is already initialized we can still change its value later on with arithmetic so with chicken for example I could say chicken equals chicken + 10 so here we have chicken starts off at one and then this line is going to set chicken equal to the current value of chicken which is 1 + 10 so that should give us 11 and if we print that out we will see 11 that about covers the basics of using variables these are the foundation of any program and having a good idea of how they work will make you a very capable developer next up we're going to cover if elseif and else statements these types 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 a condition we're going to start off with our code looking like this where we have a message variable and we're printing it out and when we run we just see Zero over here but also let's add another variable to the program and I'm just going to call it condition you can call it whatever you want and you you can also set it to whatever you want I'll set it equal to 25 we're going to use this variable as part of a conditional statement that's going to change the value of message we're going to start with an if statement and that starts with if condition is greater than zero then and then end now between this then and this end we're going to put what actually happens when the condition is true so I'm going to set message equal to one so this line reads as if the value of condition is greater than zero then and only when that condition is true it's going to perform what's in here it's going to say message equals 1 and we can test that message starts off at zero but if we run we can see that it goes to one and alternatively let's change condition to - 255 so now this if statement won't be true anymore because -25 is not greater than zero so since this condition isn't true that means this line here won't run and again when we run we see message remains to be zero this line never happens we're going to add another conditional statement to our code and it's going to be a really similar idea to before but this time it will check to see if condition is less than or equal to zero then and in here we'll set message equal to1 so same idea as before is going to see is condition less than or equal to zero and only if that's the case will message be set to1 and we will see that that happens this line is reached because its condition is true but again all of our programs happen from top to bottom so first this line happens then this line and then it's going to check this condition and in our case this condition is not true so this line does not happen then it moves on to this condition and since this one is true this line does happen now writing our code this way with two separate if statements one right after the other is acceptable but there is a much easier and more efficient way of writing this exact same thing so I'm actually going to remove this second condition or the second if statement and instead we're going to add to this first one I can put in else and say message equals -1 so this is a new type of condition where it just says else so the way this reads is it's first going to check this condition if condition is greater than zero then this line happens but if this condition is not true or if condition is less than zero instead of greater than then it's going to say else and once this else is reached then whatever is between else and the end is what gets run so in this case message equals ne1 so this is the exact same functionality as before but it's more compact with just one single set of if and then else and we can test this if we run we still get our -1 in output so far we've used if statements we've used else statements and finally we're going to move on to the last conditional that is sort of a combination between these two and it's called the else if let's go ahead and put one into here and I'm actually going to remove our else for now just to clean it up a bit but in its place I'm going to add the else if so you say else if one word like that and we'll add a condition to this condition is less than ne10 then and in this case we'll say message equals -1 so this is different than before because with our else statement there was no condition attached to it so what's going to happen is this condition is going to check first and if it's true we're going to do this but if this condition is not true it's going to move on to here else if and it's going to check this condition and if this one is true then this line here is going to run but if neither of these conditions are true then neither of these lines are going to run at all so testing this out since condition is -25 right now we still get our -1 but if we change this to say -5 instead so with that5 is not greater than zero so it moves on to the else if and -5 is not less than -10 either so neither of these conditions were met and that means that message remains at zero what it starts at now to tie everything together is that you can put an else in addition to everything else so I'll put message equals and let's set it to a string no conditions met so with this ne5 example this is not true this is not true so the else is going to be hit and this line is going to happen so we can see if we run no conditions met now with blocks of code like this you can have one if you can have one else but between the else and the if you can have as many of these else if portions as you want so I'm going to put in another one actually I'll say else if condition and we'll say is equal to -5 then and in this case I'll say message equals hello now keep in mind that I'm using the double equal sign here when you're using double equal sign that's how you're testing to see is this equal to this but when you're using one equal sign that's called an assignment meaning that this value is being directly assigned to this variable so we're not setting condition to5 we are testing to see is condition equal to5 and if it is then this line will happen and we can test it out if we run we will see Hello one final comment on this topic is that in these intersections you can have as many lines in here as you want so in addition to hello I could do like an extra print and say hi so when I click run we get both the hi and the hello both of these lines run because everything between this and this the entire inner indented section that's going to be run the next topic 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 show this off we'll Jump Right In with our first type of loop the while loop I'm starting with the code looking like this where we have our message and we're printing it out and after the message we're going to write the first Loop and it's a while loop we'll say while message is less than 10 do and then in here let's say message equals message + one let's go through this section of code line by line to get a better understanding of what it does we start with while and then this message is less than 10 so this is really similar to our if statements where this is a condition and only if this condition is true do we go on to the intersection and inside of this while loop we are saying message is equal to the current value of message which is 0 + 1 so after this line runs that would mean that message would equal one but what makes Loops interesting is that once it reaches here the end it's actually going to jump back and go while message is less than 10 so it's actually going to check the condition again right after I checked it the first time and message is now one so it's going to ask is one less than 10 and since it is that means message is going to increase to two and then once again it go back here is 2 less than 10 and then now it's three is three less than 10 and over and over and over it's going to keep looping which is where the name Loop comes from until finally message is going to increase to be 10 and it's going to say is 10 less than 10 and since 10 is not less than 10 that means that the condition is no longer true and this intersection stops running it does not run anymore after that and then it moves on so after this runs that means that this intersection ran a total of 10 times and message ended up being 10 and we can see that if we print message and run we see 10 keep in mind that this whole process that we just Ted talk through happens instantaneously since computers are incredibly fast the program comes across this while loop here and can almost immediately get through all iterations you could even jump this 10 up to 10,000 and it's still going to run instantaneously it gets through all 10,000 iterations in no time speaking of this message equals message plus one is a very basic example between the do and the end you can have as much code as you want just like the if statements and all of it will be executed as long as the condition is true for example we could add a second variable I'll go ahead and just call it test and I'll set it equal to zero then within the loop let's do something like test equals test minus5 so I'm also going to drop this back down to 10 so it's a little bit easier to understand so just like before this Loop is going to run 10 times which means that this line here is also going to run 10 times which means that test if we print out test will be 50 it subtracted by five 10 times earlier I mentioned that the loop will run until the condition is no longer true and as a programmer it's your responsibility to make sure that whenever you use a while loop it will eventually end otherwise your Loop will keep going on and on forever and just as a learning experience we can go ahead and try it out just to see what happens what causes the loop to end is this line because message keeps growing until message is no longer less than 10 so if we change this to minus one well now this Loop is going to keep going on forever because message will always be less than 10 so if we run we can see that we kind of get stuck so if you go it yeah so it actually times out if if you do this on your own computer be careful you might have to force close the program um but yeah that's what happens whenever the while loop runs forever it just it just never stops and it times out so before we forget let's change this back to plus and we are back to normal that about covers while Loops so let's move on to the second type of loop which is called a for Loop this kind of loop is just as useful as the while but it's useful for different situations in programming you'll notice that there's normally many ways to do something so you'll always have options for how you go about solving problems let's start by making a new VAR variable again and I'm going to just call it pickle it doesn't really matter what you call it and I'll set it equal to zero and this is the value we're going to print out pickle and after the while loop we're going to start the for Loop and this is what it looks like you say 4 I = 1 comma 3 comma 1 do and then inside here let's do pickle equals pickle plus 10 now looking at this code it's a little less self-explanatory compared to the while loop so let's go through it bit by bit so we can better understand what's going on here this first line specifies three different values first we have I equals 1 in a for Loop this is going to act as our iterator which basically means that it's a value that's going to be updated every time this for loop loop Loops the next number in the sequence this three is the number that our iterator will approach and once that number is reached the loop will end and finally this last number is the step value for the loop or in other words it's the amount that our iterator will increase by every time this Loops so I know that was a lot of information all at once so let me go through what will happen when this Loop runs the loop will start I starts off at one and again this is our iterator so the loop begins pickle is going to increase from 0 to 10 so 0 + 10 is 10 so now pickle is 10 now the loop ends it goes back to the start but this time I increases by this value so I increases from 1 + 1 is 2 so now I equals two and since two the current value of I is less than this value the three that means the loop will continue so we're going to go into the loop once more pickle is going to increase by 10 again so now pickle is 20 and again we go back to the start of the loop I is going to increase from 2 to three so now I is equal to 3 now that I is equal to this middle number 3 is equal to 3 that means that this is the last iteration of the loop so pickle is going to increase by 10 one more time and then the loop is fin L done and it moves on so that means this line in the middle here or I should say this whole section ran three times total the one to three so we can see that pickle after all of this should be 30 now at face value this Loop might look a little bit more complicated than the while loop we did earlier since there are a few more numbers for us to keep track of but once you're used to it I think it's pretty self-explanatory the loop will start at this number increase by this number each time until it reaches this number so it's an easy way to have a predetermined set of Loops something interesting and very useful that you can do with for Loops is that you can use this iterator value inside of the loop itself so for example I could increase pickle by I what this means is that I is going to start off at one so pickle is going to increase by one then it's going to increase by two and then it's going to increase by three so 1 + 2 + 3 is 6 this is a really common way to use for loops because there will be many circumstances where you'll need this iterator value in order to perform certain calculations in programming it's important to remember 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 paste it to another spot so that there's now two copies of the same code 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 ran elsewhere in the program you'll understand what I mean in just a moment we're starting off the code with just message equals 0 and then printing message which outputs zero but after message we're going to write our first function we're going to say the function keyword and the name of our function will be increase message and then inside of this function we'll say message equals message + 5 so here function keyword indicates that the next thing is the name of our function the two parentheses is necessary to indicate it's a function and then everything before end so whatever is here this is going to be run whenever the function gets called now by default just because we Define this function that doesn't necessarily mean that this code is going to be run off the bat like if I click run we'll see that our output is still zero in order for this code to be executed we need to call the function which just means we type the function name increase message and then put the two parentheses so this is referred to as calling the function which just means that when you type this line it's going to take whatever code is inside the function and run it so we'll see now that when we run the whole program our message value is five and similarly to demonstrate this I could copy this function call and say call it two more times so we're calling this function three times total which means that this code is going to run three times which in the end means that our message is 15 now we're going to make the function a little bit more interesting when you're writing a function you have the option of giving it parameters parameters are what go in between the parentheses sort of like what we're doing with this print function down here we could change it so that this function accepts one parameter we'll say it accepts something called Fu and then we can use this parameter in the function so we'll say plus fu so instead of adding five each time it's going to add whatever value we pass in so when we call increase message I can put in whatever value here that I want so I can say 99 for example and when I run we got 99 but I can also call it a second time increase message but this time I'll pass in two and this time it's 101 it called this code twice the first time fu is equal to 99 and the second time fu is equal to two although in our example we only have one parameter in this function you are able to put as many in there as you want and you do this by putting commas between each one uh so if I wanted another one called Val and another one called me it doesn't matter you put them all in an order and then when you call the function you would pass in additional values for each one of those parameters so this is how that would look if you wanted to do it that way now one very important thing to know about functions is that you have the option of returning a value to explain what this means let's go ahead and alter our function so that it returns something I'm going to clean up this code a bit so we're just back to the one parameter Fu so instead of having message increase by just the value of Fu let's make it so that Fu equals Fu * 2 so we're going to pass a value in and then immediately we're going to double that value and then let's return F so now the function is behaving a bit different than it was before before we manually said message is going to increase by Foo but now we're just changing Foo and then returning Foo so what happens is in order to actually apply this function to message we would say message equals increase message and then let's pass in 10 so what's going to happen here is this 10 is going to be passed in as Fu then fu is going to double so that 10 is going to turn into 20 and then it's going to return the new value of Fu which like I just said is 20 so when something gets returned in a function that basically means that the function call is going to replace itself with the result so since we were returning 20 up here it's basically the same thing as replacing itself just with the result so it's basically doing that and but the beauty of it is that you're able to pass in whatever value you want and it will take this and apply it so in this case I typed in 99 the result is 198 it just doubles this value and then since we're setting message equal to this returned value that's what we get in the print the reason why this is useful is because now if I wanted to apply this function to say a new variable like chicken the way I had it set up before where I manually said message inside of this function that would mean that it only applied to this variable and that's it but now when I'm returning something I can use it for any variables so I could say chicken equals increase message 5 and then if I print chicken we should see 10 a cool thing that you can do with functions is pass other variables as parameters for example at the top of the program let's make a new uh variable I'll call it monkey and let's set it equal to 100 then what I can do is I can pass this monkey variable let's say into here so I'm going to just say monkey so what's happening is the value of monkey which is 100 is going to get passed into increase message and then get applied to chicken which results in 200 having functions in our tool belt will be incredibly useful as you move forward with programming you'll see how often sections of code need to be ran at different points in the program and it's a lot easier to manage everything when the code is in one place rather than copy and pasted throughout the file so far everything that we've written in our Lua file has been Lua code but with pretty much every programming language there's an option for programmers to put in little notes that are not treated as code and these notes are called comments for example here in this code from the previous lesson we have a function that's called increase message if I was writing a bunch of code and I had a ton of functions written throughout my files it might be difficult to keep track of what each function does so it's helpful to write comments into your code to help you remember in Lua you write comments by typing Dash Dash and then everything after it is considered a comment so I can write my message doubles of value and returns it so this message here is ignored by the program but it helps me or whoever is reading my code uh it just gives you some extra context it's a little reminder a cool trick that you can do once in a while is called commenting out some code comments are helpful for these little reminders like this but you can also use it to disable certain sections of code so for example if I wanted to see what would happen if I completely removed this line I could delete it or maybe I want to just temporarily delete it by doing Das Dash now this line is considered a comment and when I run the program it basically pretends like this line isn't there at all but at the same time it's really easy for me to bring it back there's also the option to treat entire sections of code AS comments and you can do this by typing D- Square brace Square brace and then when you want to end the section of comments you do square brace Square brace so everything between this and this is considered a comment so neither of these lines here are going to be run getting into the habit of commenting your code is really important it helps for your own not but if someone else wants to look through your code comments make a huge difference in understanding what you as the programmer were trying to do when you were writing it reading other people's code can be difficult at times but having comments to guide you along the way makes the process a lot easier in Lua and lots of other programming languages there are two types of variables Global variables and local variables so far we've been working with globals Global variables are things like message once you define it it can be accessed anywhere throughout the program and that's what Global refers to 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 variable and all you have to do in order to make a variable local is add this local keyword beforehand and as the name implies the local variable can only be accessed within the same section of code that the variable was declared in so for example let's update this increase message function to do something different and utilize a local variable I'm going to make a new local variable in here and call it local VAR and I'm going to set it equal to Fu then on the next line I'm going to do VAR equal VAR / 2 and then finally we're going to return VAR so now rather than doubling the message we're now cutting it in half because we're setting VAR equal to the Past in value cutting it and half and then returning VAR but it's important to note that the variable VAR is only going to be used in this section of code inside the function and since that's the case using local here makes sense we wouldn't want VAR to be utilized anywhere outside of this section of code right here and that's where local becomes really useful is when you're managing your project when there's a large amount of variables throughout your program it's more likely for a mistake to be made when there's lots of different glob because if you made a second variable later on and I called it VAR well it might not be clear which VAR we're talking about but if everything is local to its relevant section of code there would be no mistaking which VAR this is referring to just in case you named two variables with the same name so this is a habit I highly encourage you getting into especially with writing functions I would say this is a very common scenario where I use local variables because I don't want these variables to be used outside of the function that I'm clearing them in everything that we've covered so far is very common in the programming World you'll find all of these things in pretty much any language however our next topic is tables and this is a data structure that's pretty unique to Lua every language is going to have options that are similar but tables in Lua 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 keep everything organ ganized it's a lot like variables a variable stores one piece of data and that's it nothing else than that one thing a table is capable of storing multiple pieces of data in the same variable except it's a table not a variable so let's go through an example from scratch on how to utilize tables I'm going to start off with our typical message equals z and then we're printing message and after this message variable let's make a new table I'll call it test scores and then when we created a new table we set it equal to curly braces in Lua if you ever see these curly braces that's how you know that we're working with a table and since there's nothing between the curly braces that means it's an empty table so here we initialize test scores to be an empty table for this demonstration we're going to store a bunch of madeup scores in this table to do this we're going to assign a score to its own index in the table let's go through how to do this first we want to write the name of the table test scores and then right after we're going to put square brackets and then between the square brackets we're going to put an index you'll see what this means in just a moment to start we'll put in index one and I'll set index one to 95 for example and that's it we assigned our first score to the test scores table at index one now we can move on to the second test score let's use test scores and then two we'll set this one to 87 for now we'll put in one more test score I'll say test scores three and I'll set this one to 98 so at this point we now have a table called test scores and it contains three scores one at each of these three indices now in order to access that data that we just put in we simply need to write the table name and refer to whichever index we're talking about about so let's say we want to set message equal to test scores index 2 so this is just going to grab whatever value is at index 2 which is 87 and we're printing message here so we should see 87 keep in mind that we only assigned values to index one index 2 and index 3 what would happen if we tried to access index 4 we didn't put anything in there so what would happen if we set message equal to test scores index 4 and try to run this we actually see nil so when nothing is assigned to a particular index it actually contains the value nil now nil is a reserved word in Lua that just represents the meaning of nothing it just means nothing exists right here in this position so this method that we're assigning values to the table is okay but there is a more efficient way to do this exact same thing I'm going to go ahead and delete these three lines and instead of writing out three different lines for those assignments we can go back up to this original curly bra section and put in those three different scores and that was 95 comma 87 comma and 98 so this whole line here accomplishes the same exact thing that we were doing before with the three manual inputs for these three scores or in other words this value is at index one this value is at index 2 and this value is at index 3 and we can test this out let's go ahead and set message to index 3 of test scores and we get 98 which is this value although this is the easiest way to put values into a table there is one other method to doing this that still is important to go through we're going to redo all of this by simply making test scores start empty and similar to the way we did it before we're going to write three separate lines but this time we're going to use this function called table. insert same idea from earlier where we wrote Our Own functions the Lua programming language comes with a bunch of functions written for us that we can use and table. insert is one of them what this function does is simply takes a value and puts it into a table the function has two parameters that we have to pass in before we can use it the first parameter is a table name so let's type test scores that's name of the table that we want to insert into and the next parameter is the value that we want to insert so let's first put in our test score of 95 and we can do the same thing with the other two scores and say 87 and 98 now the benefit of this table. insert function compared to what we were doing before is that we don't have to specify what index we want these scores to go at and you'll run into situations where you won't actually know which index you're on you'll write code that just says put this value into the table but you won't know at the time how many items are in the table or if there's any gaps so this insert function figures all of that out for you just a little side note when assigning values to a table in Lua it's typical to start at index one and then go up from there it's probably more common for languages to start at index zero but Lua is one that starts at index one but you don't even have have to use numbers as an index you can also use strings you could say test scores and then in square brackets I'll say hello and then we'll put the value 90 there then similar as before I can just recall that same data by passing in the appropriate index and we get 90 but like I said earlier the oneline approach if you already know what data is going to be in there in this case 95 87 and 98 this is the most compact way to do it and when you have tables full of data like we do here it's important to be able to efficiently and easily go through all of 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 table's entries and examining the data it contains remember the for loop from earlier we can use them in a new way here in order to easily go through the table table and here's how it's done we can say for and I'll say I comma s in I Pairs and then the table that we want to iterate through is test scores do and then inside the for Loop I'll say message equals message + S now this Loop here is going to go through each item in the table and for each item it will perform whatever code is inside of the loop this I refers to the current index that the loop is on and this s refers to the value or the data that exists at that index so in this case test scores has three different scores in it currently that means that this Loop is going to loop three different times the first time I is going to be one and S is going to be the value that's at index one or 95 then at that point message is going to increase by 95 then the loop is going to start over again I is going to be two this time s is going to be 87 or the value at index 2 and once again message is going to increase so this Loop in general is just going to add up all of the scores together and put it into message so I'm going to get rid of this line and we can run and see that the sum is 280 that's what we get if you add 95 + 87 Plus plus 98 there's one last thing about tables that I'd like to go through a cool feature about them is that you can add properties to them which is basically a variable that's tied directly to the table itself let's go ahead and add one to this test scores I'll say test scores. subject equals and in quotation marks math and that's it we can now get this property by just calling it similar to any other variable and we should get math this is a convenient way to clearly Mark what each piece of data represents but really all that this line is doing is test scores and then in square brackets the string subject equals math these two lines accomplish The Identical thing this is just an alternative way of doing this and vice versa this really demonstrates how flexible tables are and they let you get really customizable with how your code is organized and that's about it for the main fundamentals of using Lua if you found this video helpful be sure to leave it a like and if you have any interest in game development I have tons of videos on my Channel about using Lua to make games including my Dev Vlog series as well as my tutorial playlist for love 2D check it out if you're interested and if you made it this far thank you so much for watching this video