Lua Tutorial

Lua Tutorial



Get the Cheat Sheet Here :
Subscribe to Me:
Best Lua Book :

Data Types 3:43
Math 6:58
Conditionals 10:53
Ternary Operator 15:20
Strings 16:35
Looping 19:11
Repeat Until 20:44
User Input 20:56
For 22:00
For In 22:29
Tables 23:41
Functions 28:26
Return Multiple Values 29:25
Variadic Functions 33:13
Closure 34:27
Coroutines 36:46
File I/O 40:10
Modules 43:32
Metatables 45:30
OOP 51:00
Inheritance 55:04

Like the channel? Consider becoming a Patreon! Check it out here:
►►

*Watch More Learn in One Videos*
►► Java –
►► C++ –
►► Python –
►► MySQL –
►► PHP –
►► Kotlin –
►► C# –
►► JavaScript –

well hello internet and welcome to my Lua video tutorial in this one tutorial you're gonna learn pretty much everything you could ever want to know about the Lua programming language and into the description underneath the video I have a cheat sheet that has everything that I talked about in this tutorial and I have a lot to do so let's get into it okay so first thing you're going to want to do is install Lua so you can either install these binary files here you can just go to Lua – users dot org forward slash wiki /lu binaries like that or if you're on Windows you could go to code.google.com /p Lua for Windows you can use either one of those I am on OS X doesn't really matter everything here is going to be is exactly the same no matter what operating system you're on but you can see right here is everything going to need to install and you should have no problem with that so let's start writing some code ok so on the left side of the screen I have where I'm going to be inputting all the code basic text editor sublime on the right side I have my terminal and if you are on Windows this would be your command line so let's just jump right in here and you're going to have the extension of Lua for all of your files and this is just Lua Tut Lua right like that now let's do a hello world so we're going to say print hello world just that simple save that jump over here and this is how we're going to execute our programs just Lua and then the name of your Lua file and boom hello world so hello world is out of the way that is how you would print to the terminal now you're going to be able to make comments just like this or you can make multi-line comments like this you can see how they popped up right there so multi-line and there we go comments are out of the way now your variable names can't start with a number but they are going to be able to contain letters numbers and underscore and we just create one right like this basically Lua is going to dynamically type based off of whatever you save in this variable so this automatically becomes a string because this is a string right here and your strings of course can either be double quotes or you could have them be single quotes doesn't really matter another way for you to be able to print stuff out and is to go io right like this and you could say something like size of string and then you could separate that if you wanted to put the string inside of here and on top of that let's say that we also want to get the size or number of characters in our strings we would just put a number sign and name in this situation and then there's going to be a bunch of things you can backslash this is going to be a new line right this is just a short easy way for us to be able to throw a new line in here which is just going to skip us to the next line there's a couple other little back slashes you can use there's new line of course you can also do a backspace you could do a tab of course you're going to need a way to be able to print a backslash inside of something like i/o right you're also going to be able to print quotes double quotes like this and also single quotes and those will be used in situations in which you have double quotes here but you want to put a double quote would say right here that would allow you to do that and we need to do this because otherwise it would not know where the string ended and if we file save that executes it you can see size of string is 5 that automatically prints out for you now an interesting thing here is even though this is defined as a string currently we can just as easily come in here and go name is equal to 4 like that and that is not an error so you'll be able to start off as a string and then convert the data or the variable over into an integer we could then come in here and say something like my name is and name like that you see my name is 4 comes out there no errors are thrown other programming languages would throw errors however now Lua only has floating-point numbers or what they call just simple numbers and the biggest integer that you could store inside of Lua is this gigantic guy right here and if we can go and add one to that you're going to see that indeed that is true so let's go and try to print one more than what we have there with big num and you can see it comes back out as a negative number so this is as big as it gets in regards to integers you could easily come in here and then check the type of this variable by just surrounding it with type and calling the function type jump over here and you can see big number prints out there number prints out right there so you can see that everything integers or floating-point numbers are all considered just basic numbers inside of Lua now as we all know floating-point numbers or as some of us do anyway floating-point numbers are not necessarily precise so let's come in here and check the precision basically with a floating-point number you're going to have your digits be precise up to 13 digits so that just means like this 1 2 3 4 5 6 7 8 9 10 11 12 like that and then we could come in and add another digit to it so it's just throw in 12 zeros and then tack 5 onto the end of it and float precision and you can see that it is precise indeed right to that point if however we tacked on another additional digit onto there and reran that you're going to see that it lost precision so your floating-point numbers are precise up to 13 digits which is normally more than enough now let's say that we wanted to come in here and create a string that is going to go over multiple different lines and at the same time also keep any white space that you would type inside of here you would just put those two opening brackets like that and you could type in I ma or ever write like this and you're going to see if you run this it's going to not only print out the whole entire string but also pay attention any white space and any new lines that would occur within that long string let's go in here and throw in another string there's my name if you wanted to combine or concatenate strings inside of lua you can just go a long string like this is equal to and let's just use long string here you would just put two dots inside of there and name take that throw that in here instead and you can see that it went in there and automatically tacked on the other string just with this little operator that we have right there just so we can cover other data types that are available we also have boolean z' so able to drive let's say we want to monitor that and a boolean is going to either allow us to use a value of true or a value of false and of course we could come in here and also check the type of this so is able to drive like this you can see it comes back as boolean another thing that's a little bit odd about Lua it doesn't go out of its way to try and show you error so let's come in here and let's try and get a type of made-up variable so made-up variable has been basically just created right there in that line it's not been defined with any value and you're going to see right here that everything in lua by default gets the value of nil if it does not get a value assigned specifically by you inside of your code there's a rundown of a lot of what you need to know about variables now let's go and take a look at some math functions okay so basic math functions we have in little we can add subtract multiply divide and this is the modulus and let's just go ahead and save that and execute it and you're going to see exactly the results we're able to produce here just pay attention that this is a modulus operation right here and not what is commonly known as a remainder operator if it was a remainder operator it would actually give the value of 2.2 and this just is the remainder of a division by 3 so what it does is automatically cuts that off to just make you aware that everything else here is pretty straightforward in regards to this basic mathematical operations it's also important to know that you cannot perform common shorthand notation like let's say we had number plus plus can't do that you can't do negative negative and of course if you can't do those you're not going to be able to use this or this just common things that are in other languages that Lua does not have currently and this is version 5.3 of Lewis so it's the most current at least as I'm speaking right here another thing you can't do is this type of short notation which is normally equivalent to number is equal to number plus 1 if you want to do this inside of lua you are actually going to have to instead type that out there's a whole bunch of built-in math operations inside of lua these are some of the most common ones that you're ever going to see see right here we have floor ceiling max min sine cosine dah-dah-dah-dah and i'll show you some examples of how all of those work here's a whole bunch of math functions we're gonna check the flooring the ceiling the max minimum power and square root see exactly how all those operate the only difference between floor and ceiling is the floor is going to round this number down ceiling is going to round it up and you can see here max give me the maximum men's going to give you the minimum and there's power and square root functions as well something that's rather neat is using random numbers and you will probably use random numbers a lot in your programs let's go in here and generate a random number right now so let's just go right and you would do that with math dot random right like this and let's say that we want to come in here and generate a random number between 0 & 1 random and that is all that we would do but that's a little bit boring you may also have noticed here there's no semicolons at the end you could put semicolons in there but they are not needed in any way let's go instead and create a random number between 1 and 10 we just come in here type in 10 and then you could also come in here and also define random numbers in very specific ranges so let's say we wanted to go random number between 5 and 100 there you go 5 and 105 and 106 acute those they all work pretty simply the thing that is kind of useful is we're going to be able to set our seed value and that is just going to help us calculate a random value and how we do that is just go math.random seed and the best way to do that is to use OS dot time right like that and that is going to generate for us a nice random value the thing that is interesting is we can come in here and print our float values another way so let's just use print I'm going to be bouncing around a lot just to show you all the different ways we can use these things if we wanted to format a string we just go string format and let's say that we wanted to get pi is equal to and then we wanted to import directly into our string a value and let's say that we wanted it to be 10 decimal places long F for float right like this and we could also get the value of pi by calling the math library right like this you're going to see that we got the value of pi to 10 digits automatically sort it out for us also however be aware that this is only going to have a precision of 13 digits anything over that and it's going to start watching itself up and that's just the way floating-point numbers work in almost every programming language okay so now that we got the math functions out of the way let's take a look at conditionals okay whenever we're using conditional statements we're going to have relational operators as well as logical operators and the relational operator is going to be greater than less than greater than or equal to less than or equal to this is the funky one well that's fine that's just equal but not equal is actually going to be tilde and equal to very important remember that just tilde and equal to this one right here which you may be aware of is not used then your logical operators are going to be and or or not spelled out exactly like that and I'm going to demonstrate how all of these work so let's say you have an age which is equal to 13 and let's take a look at if statements and how they work now we're going to be able to perform certain operations depending upon the value of age here we're going to swing to say if age is less than 16 then make sure you put then inside of there you're going to see it automatically indents we're going to say right and something like you can go to school another thing to be aware of is any variable that we would define inside of our if statements for statements functions so forth and so on or to called local variables and that just means that they are local to this if statement and the value that is set here cannot be found outside of the of statement more on that here in a second now if this if statement doesn't come back is true maybe we want to check something else so we would go else if write like that no space between there and then we could say something like age is less than or equal to 16 and let's say that we want to check a couple things well this is where our logical operators come in we can say and and we could check another one age is less than or equal to let's just like it less than 18 like this then make sure we put our then statement in there and then we can come in here and print something else out so we could say something like you can drive and then finally as a default we would use else throw something else in there and you can say something like you can vote another thing we have to remember is anytime we have an if statement we always want to use end there whenever it is finished now let's jump back to this guy as well as also check out exactly what goes on here if we would go print like this and we would go local variable you see what happens here let's wrote this you can go to school but this comes back as nil like I said once you define a variable inside of here a local variable this is inside of the if statement and it cannot be accessed anywhere outside of the if statement and we're going to see more about that later on just want to make you aware of it now we come in here and play around with logical statements a little bit more just so we can check out how all of them work say we want to check if age is less than 14 and we could use or which is going to give you a value of true and then operate with whatever is after the then statement if one or the other of these is true say age is greater than 67 like this and you could just keep on going you can go I oh right like this just keep shock just keep typing everything's going to work say like you shouldn't work and then we can continue on the same line and then end and don't forget to come in here and put an age in like that and come up here and execute it and you can see you shouldn't work comes back because 13 is the value right here this comes back is true which means that we are going to print you shouldn't work out onto the screen now you can do this but it kindly gets sloppy so watch out there just wanted to demonstrate how the logical operator works and also make you aware that you may see people use really sloppy long statements like that now we're going to also be able to come in here and format or convert from multiple different variable types to other different variable types so let's just come in here and go print and string and then use format again and I'm going to demonstrate the night logical operator as well so let's say not true what will that do well I'm going to come in here and if you want to put a string inside of here you would just put a % and an S like that however we're going to have to convert our bowl into a string and how we do a cast or conversion to a string you can throw anything inside of here throw a number or whatever and you can see right here the not true part right there if we save that you can see that not true this logical operator before the value of true is going to give us a value of false you can also see how the boolean this guy right here was converted to a string so we could use it inside of our print statement now there is no ternary operator inside of Lua and a ternary operator normally looks like something like can vote is equal to age greater than 18 like this and otherwise false that's what a ternary operator looks like in other programming languages and it basically just says if age is greater than 18 we want to assign the value of true to the variable can vote otherwise we want to assign the value of false to the variable can vote so we can't do that however we can pretty much knock it off inside of Lua how we would do that is go can vote is equal to and we could go age is greater than 18 and true or false so it looks very similar to the ternary operator and it works exactly like the ternary operator and we could say can I vote and we come in here and go to string again and can vote let's change this to can vote and you can see right here the answer comes back as false so there's an example of how we can use conditional statements or if-else and else as well as knock off a ternary operator and it's also good to know that there is no such thing as switch inside of lua but you can also basically knock that off with if and else statements now let's go and take a look at some strings and ways that we can work with them okay so I went and created a really really long string right here for us to be able to play around with and I'm just going to do a couple simple little string operations that are common and then as the tutorial continues I'll cover some more so let's go and say that we wanted to do something like get the quote length right like this we could come in and go string uh deli N and then throw a quote inside of there and then make sure we separate everything with commas and you can see right there the quote length comes back at a hundred and eleven characters we come in here also and see that we can just go like this and throw the number sign inside of there and get exactly the same result this is the most common way to check the number of characters that you have in your string by the way you could also come in here and replace certain strings so let's say what we wanted replace and this is going to be bad English but replace I with me for example we could then go string dot G sub like this and then you're going to put inside of there the string that you're going to want to change and the value that you're going to want to change and then the value you're going to want to replace that with so we'll say me instead like that and always make sure that you close off all of your brackets so make sure you have a closing one for this one and file save and execute you're going to see right here replace I with me and it went and changed all of the versions of I inside of this great big giant string to me instead so it says me change my password incorrect and da-da-da-da-da another thing that we can do is find any matching indexes everything inside of in regards to strings is going to have an index every character so this is going to have the index of zero this space right here is going to have the index of one this is going to have the index of two and we'll be able to search through this great big giant long quote and find out exactly what the index is so we could say something like index of the word password and we're going to use string dot find and again we're going to pass in the string that we are going to be searching for and then the word that we're going to be searching for which in this situation is going to be password make sure we put a little comment side of there and you can see right here index of password is 14 so that's 14 characters into our great big giant long quote is the location of a word password and two other little simple things we can do let's say that we wanted to make all of these characters inside of our quote uppercase we could just put string upper or lowercase we could just go string dot lower save that execute and you can see right here made everything uppercase and everything lowercase we're going to get more into strings here in a second but let's go and jump in and take a look at looping and how that works inside of lua i'm going to start off with a while loop and what we're going to need to do is initialize or create a variable that is going to define the starting value that we're going to be leave looping through and then we're going to say something like while i the value of i is less than or equal to ten we want to continue looping through or executing code inside of our while loop so let's just say we got IO right and we just want to print out the value of I there you go and inside your while loop you're of course going to have to increment the value of I remember we can't do something like this not allowed you have to type everything out and then let's also say that we would want to jump completely out of this loop and not continue looping through it anymore if for example I has a value of 8 well then we're going to say something like break and of course we have to put an end right there that's going to end our if statement but then we're going to need another end that is actually going to end our while loop and then we could do something like print and throw a new line in there see if we execute that it's going to print out 1 2 3 4 5 6 7 and it didn't continue printing up until 9 and 10 because right here we said if it gets to the value of 8 we want to jump outside of the loop and stop executing all together also very important inside of lua to understand there is no continue statement so you might be used to that in other programming languages but it's not going to work here another thing we can do and this is more like the common do-while loop in other programming languages we have something else that is called the repeat statement so we're going to say repeat and we're going to continue to execute code inside of here as long as a condition is true and let's come in here and also demonstrate exactly how you can get user input from somebody so enter your guess like that we could then come down and store inside of the variable guess whatever they enter in the keyboard and that is how easy it is to get keyboard input just IO dot read then you're going to come down here and you're going to define the condition that this is going to continue looping now we are going to want to convert the guest that they made it's automatically going to be stored as a string so we want to convert it into a number remember if we want to convert to strings we type in string like that but in this situation we want to convert it into a number so we're just going to put guests inside of there and then we're going to say equals to 15 so this is going to be a little number guessing game in which it will continue asking them for a guess until they enter in 15 we type in that and then we are enter guess you can say 10 it's going to continue to ask until 15 is entered and then it's going to exit so that is how to convert the numbers and that is how to use the repeat until or what is commonly known as the do-while but inside of lua it's known as repeat until now let's take a look at four loops and they're a little bit different what we're going to do we're going to define an iterator just like we did with the while loop previously and we're going to give it a starting value then we're going to give it an ending value and then we're going to say how much should it increment each time we're going to put a 1 inside of there and for that you can put it to you put anything let's go and print the eye statement you could also throw a break statement in there as well you can go ahead and check out exactly how that would work and you can see it just cycled through there and printed out 1 through 10 right on our screen now what I'm going to do is this is a table which we're going to talk more about here in a second but it's basically like an array in other programming languages it's just going to be a variable that's going to allow us to store multiple different values inside of it and I jumped ahead to this right now because I want to show you how easy it is to cycle through all of these different tables so here we'll just go for and then we'll say key K for the key let's just call it key just so you can say n value because everything in a table is going to have a key or an a value or it's going to have an index by default if you do not sign a key and we could say something like pairs pairs doesn't matter you can put anything you want inside of here it doesn't matter what the name of that is and we could say months and then we could say do like this and then we could cycle through our table just like we did cycling through indexes and let's just say in this situation we just want to print our values out to the screen and of course we have to end our for loop right like this and of course make sure that we put value inside of there and not V and if we execute it you're going to say that it prints out all of the different months with a space in between each one of those months and of course we're going to be able to do more interesting things with tables and things like that but this is just a quick way for me to demonstrate all those things and since I brought up tables why don't we go take a look at how a table works basically as I said tables going to take the place of arrays dictionaries tuples and such in other programming languages and how we can create a table is just go in here and say something like a table and then put two curly brackets like that there you are there you have a table we could come in here and add values to the table using a for loop so we can add them just you know on their own but this is just a quick way to throw a whole bunch of values inside of there so we'll say a table and everything is going to be stored inside of an index this is going to increment through different values of I and it is going to just assign those values to I and this is the index so the very first index in our table is going to have the value of one just like you see right there and then of course we're going to end that and we're going to be able to come in here then and access those using the index that we assign so let's say we want the very first value stored inside of their index you're going to see that we have one there once again because we assigned it that way could also be 0 or it could be anything else and you can see first comes back is 1 right like this so that's one way to access values inside of a table you could probably guess exactly how we would find out the number of items that we have inside of our table let's just come in here won't you guess okay times up and let's just put a number sign and table inside of there and that is exactly how we're going to be able to access those values and you can see a number of items is equal to 10 just like you may have thought we could also come in here and insert a value in the side of a table we're going to put table don't let this confuse you a table is the table table is library we're going to use here and if we want to insert a value we just go table insert to find the table we want to insert into we then define the index so we want to insert into and then the value we don't want to insert and there you consume it didn't do anything let's come and print that out and then you'll see it go like this and first and let's just let that set for one and we like this and now you can see the value of the first index inside of our table is now 0 could also come in here and change this to 10 you see everything was moved up as well so there's 9 instead of 10 which is would have originally been set for we could also come in here and take a table and convert it into a string and then define how we want all the different items to be separated let's just use print again here how we would do that is table concat concatenate or combine same thing and table and then we could on how we want all those items to be separated let's say we want to be separated with a comma could do that come over here and there you can see exactly how that prints out you can also see the zeros in there because we inserted it in there another thing we can do with tables is remove an item at a specific index we would just go table remove and define the table we want to remove and the index we want to remove from copy this just so we can print this out again and you can see we went and remove the zero out of there and we could also come in here and create multi-dimensional arrays so let's go multi or multi-dimensional table sorry about that again we're going to create our table right like this and we'll use a for loop to come in here and populate this whole entire thing so we'll say 0 through 9 do then we go I'll tie table like this we're going to define and index we want to save inside of there and that is also going to be a table create another for loop that is going to also populate this guy so we'll go 0 through 9 again and do and then a multi table and we'll say I and you're also going to be able to come in here and pull values from these using exactly the same thing let's say we want to store them as strings to string I and then we want to also throw I and J inside of there just for the heck of it and then we can go end and then finally and this for loop now we're going to be able to come in and access the values that are stored inside of there of course by using the indexes as well so let's say table and we wanted to get whatever is being stored at the index of 0 0 inside of our multi-dimensional array go like this or alt a dimensional table sorry about that and we can get 0 and 0 like this and you can see it comes back as 0 of course we could come in here and go 1 and to save that and you get 1 & 2 right like that that's how everything is stored inside of the multi-dimensional array and like I said before we're going to use pretty much the same exact format for our for loop as you can see right here to cycle through and print out all of all items inside of our table like this and you can see that every item is printed out there right there on our screen and exactly how they are formatted good like that and you could say the 0 0 index so this is the zero index where both of them and the zero-one and so forth and so on you can go through and play with that and get a little bit more aware of exactly how tables are set up inside of lua and exactly how to use them so let's jump over and take a look at functions okay so a function is going to allow us to reuse our code as well as better organize our code and we're going to just create a function by just typing in function and then we give it a name like get some something like that if you want to pass values into a function to perform operations on them we can do something like number one and number two like that and if you wanted to perform an operation and then send it back to whoever called for our function to operate that is exactly how you would do it and then you would type in and we could then come in here and I'm bouncing back and forth between using print and the write command just to show you how to use them and we could do something like five plus two is equal to if we want to put a decimal inside of there we just put a percent sign and ad like that and then we would call our function with get some like that and then pass in five and two separate with a comma and there you can see our ants aren't exactly how we're able to call that function now let's come in and create another function let's have this one called split string and it's going to receive a string passed into it we're then going to create a string table like this say I said I'm going to keep coming back to all these different things I wanted to define a local variable inside of this and then let's cycle through the string and store anything except for spaces inside of our table how would we do that well we would go for and our string in and then we would type in string dot G match and then pass in the string that was passed into our function this guy right here and then if we wanted to basically capture all the strings up until the point in which there is a space what we're going to use is a % this is going to represent the beginning of a string or a bunch of characters or numbers or whatever that's what this little caret symbol represents and then a % and s this is what is known as a regular expression I have tutorials on regular expressions and I'm probably going do another one here pretty soon so basically what we're saying here is we want to cycle through the string that they pass into our function and we want to start capturing characters up until we reach a space and that is what we're going to be storing inside of our string table you can see here as we execute it exactly how that's going to work so we're cycling through the string we're going to go to string table and I for the index for our table and we're going to be pulling out each of those strings and storing them temporarily in a variable named str1 and all we need to do is increment our value for i and then end okay so we have all of those set up and now let's also show you how we can return multiple values from a function we would just go return and we can return our string table which is going to contain a bunch of values but then we could also send back the number of matches the total number of words that we found inside of our string and maybe this might make more sense if we typed in word right here and then we typed in word right here so either way it works and that's the way it operates and of course the end or function we put end and then we'd be able to return or receive those multiple values that are returned from this function right here called split string by just going split let's just say string table so this value right here is going to be stored in this guy right here and then the number of strings inside of there or a number of words whatever you want to call it and then we just call our function like this and let's just keep it simple and let's just say something like turtle let's just pass in two of those guys we could then come in here and cycle through everything that was passed back inside of that table 1 again number of strings we're going to keep cycling through there this is the maximum number of strings and that's the maximum amount we're going to loop through do and then we could print out on the string on the screen let's go and format this guy and we can print our index separated by the individual words that were also passed back inside of there so let's see J and then we'll go to split string table this guy right here and then throw the index inside of there find a thing we have to do of course is and our for loop and if you win inside of here you can see that it returned the and turtle and then nil there's always an extra little nil inside of there whenever we create tables and we could of course get rid of that by just going number a string minus one and you can see that it just prints out the thought and turtle that's how to go in call a function how to have it return multiple different values how to split a string into a table and how to return that value over here and save it into multiple different variables and then cycle through it so there's a whole bunch of different ways we can use functions inside of lua let's go and talk about ver attic functions this is how we're going to be able to receive an unknown number of parameters inside of our functions again just create a function and let's just call this get some more if we don't know how many different parameters or attributes or whatever that could be sent inside here we're just going to put three little dots inside of there and we can do something like local sum is equal to zero we could then come in and go for this is the key and this is the value once again in pairs this is going to be passed in here as a table then we're going to put curly brackets one two three it's going to automatically just keep cycling through there or depending upon the number of attributes or parameters that were passed inside there you go some is equal to some plus whatever value is passed inside course and that with an end and after it's done go return the total sum and then close off the function and then we could print all this out on a screen call get some more you're going to be able to pass as many things in here as you could possibly ever want to let's just keep it as six and it gave me a little bit of an error you can see right here get some I can type in some more like that and there we go sum is equal to 21 so that's how we're able to pass in an unknown number of parameters and then use it now another thing is kind of interesting inside of lua is a function is a variable in that we can store them under variable names as well as we store tables and we can also pass and return them through function so let's demonstrate that let's say double it there is a variable we can actually put a function inside of this so let's say it gets a value of x and then we can just say return x times two and then put the N function on there so we have basically just made double it a function it is a variable also however you can see right here if we just go print and double it like this and then pass in a four so automatically going to pop out an eight now going along those same lines a closure is a function that can access local variables of an enclosing function so let's go and let's create one so let's go function let's just call this outer func because this is going to be an outer function that is going to contain our closure you can see exactly why this is interesting so let's create a local variable I equal to zero and then let's put an inner function inside of it so return function like this and then inside of this guy let's go and increment the value of I and then return so this is a function inside of a function this is a closure and the reason why it is a closure we have to end this and we also have to end this one now whenever you include an inner function in a function that inner function is actually going to remember any changes made to the variable inside of the inner function now let's go and let's store our function inside of a variable called get I so we'll go outer func like that so there we stored it inside of there now what we're going to be able to do is go print get I like that and it's a function and then do exactly the same thing again the reason why this is a closure is you're going to actually continue to increment and store the changing values of I that are inside of here as you're going to see right here and there you can see value of I is starts off as 1 but then whenever it's called a second time it is going to increment it even though we assign the value of zero right there so that's an example of a closure and exactly how they work and how they store values as they are being called multiple different times so now let's go and take a look at a similar thing which is called a KO routine now a KO routine is very similar to a thread in other programming languages except that they cannot run in parallel and a co-routine is either going to have a status of running suspended dead or normal and you're going to see what all those look like so let's create a KO routine how you do that is by just coming in here and going co-routine and we're going to say create and inside of this I'm going to put a function and then inside of this I'm going to say for I is equal to one give it a starting value and ten is going to be the ending value and one is going to be the increment and then inside of this we're going to say print I value of I and also let's go print co-routine status just so we can see the changing statuses from running suspended dead or normal now we do that is pass in co-routine name inside of there close that off then we can say something like if I is equal to five then and if we want to pause or yield over to another go routine we just type in go routine yield and then we're going to end or if statement on the next line we will end our for loop and on the next line we will end our code routine so there we go have all those saved if it makes a little bit more sense come in here and go like this and like that now if we want to come in here and check our code written and see what its status is whenever it very first starts we'll just go co-routine status like that and KO exactly like this and you can see it starts off as suspended now we can call for this code routine to resume or start executing and then we can come in here and also check the status after we tell it to start running and you can see it starts off as suspended starts coming out here and printing out all these numbers and it comes right up to five and you can see it's also technically running right there that's the status until it hits five and the reason why is it says if I is equal to five we want to yield this code routine and stop its execution now let's come in here and create another coat routine that we're going to run at the same time very much like we would do with threads sequel to KO routine and we're going to create one and this one's going to go function create a function right inside of here create another for loop and go I is equal to 101 start it off at 101 and have it count to 110 and increment by one and do and print whatever the value of I is each time and then and our for loop as well as end our co-routine and now we can see that if we go in here again and go co-routine resume like this let's just have this vco2 like that but then also do exactly the same thing or restart or continue execution inside of our original code routine that we created and you can see the process here starts off as suspended gets up to five running then it gets suspended jumps over to the other code routine which doesn't get suspended or doesn't have to yield its time at all goes through its entire process and then whenever we call the co-routine to continue execution you can see that it continues executing until it is finally dead and you can see right here if we come in and get the status of this code written right here it is in fact dead after it has done or executed all the code inside of it so there's an overview of co-routines now let's take a look at something that a lot of people are confused about because there's not much good information online about it and that is file i/o now there's many different ways that we're going to be able to operate on files and you can see all the different ways right here we're going to either be able to have the file set as read-only which is the default we're going to either overwrite or create a new file a which means we're going to append or create a new file this is used this is used for read and write of an existing file overwrite read or create a new file and then append read or create a new file I'm going to come in here and actually start opening files and storing things inside of them so we can see exactly how this operates if we want to create a new file that we're going to be working with we just go IO open right like this and then we're going to give it a name let's say I call this test Lua like that and let's say that I want to have the opportunity to create a new file because I know this doesn't exist right now and then I want to also be able to overwrite and read from that file so I'm going to just use that guy right there because that works for all the things I want to be able to do and there you go you just created a file and you're able to write to it and read from it and also make it now if we want to come in here and write some text to the file we just go file right and we'll say a random string of text and we can use new lines inside of here for our files as well and let's say we want to put some more stuff in there so we'll say write again and we'll just say something like some more text throw another new line inside of there we can use seek now to move back to the beginning of our file by going file seek like this and then we're going to type in set and then the index where we want to jump because everything is indexes inside of string so we want to jump back to the beginning or a file and that's how we do that and then if we want to go and read everything from a file we'll just go file read write like this and then we'll put a star inside of here and an a meaning that we want to read everything from our file okay so we wrote to our file we created our file we jump back to the beginning of our file and we're going to read everything from that point final thing we need to do is close our file and there we go it popped up a little bit of an error I keep the errors inside of here just want to make sure we put our quotes inside of there because I think whenever you see me make an error that's going to help you keep from making errors and you can see right there print it out random string of text and some more text right like that now let's say that we wanted to come in here and also continue working with our file but we want to append data to the end of our file how do we do that well we can just leave this named as file because Lew is real nice about stuff like that and we'll just say IO open and again we're just going to type in the name of the file that we want to work with and then in this situation I went to a pen so I'm going to say a plus like that I can of course come in and write some new information to our file and let's just say even more text again remember you if you want to read from a file you have to jump back to the beginning of it so we're going to use seek to do that and we're going to say set and then we're going to say exactly where we want to jump to you can put any index in here to jump around all over inside of the file and then we can say print and of course we're going to read data our file and we want to get all of that data inside of there right like this and then finally we want to close our file if we do all that it's a random string text and then some more text comes in there and then even more text gets printed out on our screen so that is how we're going to be able to work with our files in side of Lua now let's take a look at modules okay so basically a module is like a library that is full of functions and variables and you're going to be able to create it and then use it inside of your program so let's go and let's create a module so we'll just come in here and go new file and I am going to save this guy using the name convert dot lua of course and then save that so let's create a module it's very important that your module name and your file name are exactly the same and to create one I'm going to go local convert the name of this guy and there is our module and then inside of this I can put a function let's say I want to do something like convert feet to centimeters so we'll go feet to centimeters and this function is going to receive an attribute of feet and then it is going to return whatever that value is times thirty point four eight leaf that's right and end and then you also need to return the module and save it and that is how we're going to do it just make sure we have convert here convert here and convert here very important and also the name of the file has to be converted or convert well now we can come over and do something like convert module doesn't need to be named convert module name whatever you want and if you want to be able to get access to that they have to be in the same directory of course we're going to say require and convert right like this and then we'll be able to come in here and work with this module function that's this and I'll say that we want only three decimal places that's how we do that with a floating-point number and there is centimeters and then if we want to call our module just convert module just like we created right there and then follow that with feet two centimeters which is the name the function we have in the module we pass in a value of 12 and you can see it gets back the answer of forty two point four eight zero centimeters there's a quick example of how we can work with a modules inside of lua now let's take a look at meta tables now a meta table is going to allow us to define how operations on tables should be carried out in regards to adding tables together subtracting multiplying dividing concatenating comparing tables and so forth and so on let's come in and create a table and put some default values in it so I'm just going to go a table like this and create my little table and then I'm going to use a for loop to jump through here and just fill it up with stuff let's just say one through ten two and a table and of course we're going to use the same indexes here and just store whatever the value is inside of there of course close that off so there we go we got a table now we're going to define our meta table and how we're going to do this is go MT and then we're going to define how table values should be added and you're going to be able to find like I said before adding you're going to be able to also define how table should be subtracted how they should be multiplied how they should be divided how they should be if you want to use the modulus and how they should be combined which is the same as this guy right here which we've talked about previously I'm just going to focus on a couple of these so I'm going to go add there's two spaces right here by the way there's two underscores alright so we're going to define how these are going to be added together we're going to go function and we're going to say if you pass in table 1 to tables I'm going to define here exactly how they are going to be added together I'm going to create a some table like this which is going to be the table that is going to be returned and I'm going to go for Y is equal to 1 and I'm going to cycle through the number of tables that we have right here and just do that for me and then let's say that I want to also come in here and check to make sure that the table values are not equal to nil well I can just go table 1 like that and get the that is number it's not it's the tilde sign not equal to nil can do that and both these have to be true for the Stork I can say table 2 and then also put the Y inside of there not equal to nil well then I will do some stuff well make sure that we put this guy over here close that off and then put then there we are open this up a little bit better what am I going to do if I got what I need here well I'm going to start adding stuff to the some table so some table the value is going to be stored inside of the same exact index is going to be equal to and this is just what I decided how I want to you can decide how you want to add table values anyway this is the way that makes logical sense to me so table 2 so I'm going to take the values that's in 1 and 2 and then store them inside of a brand-new table this got right here else let's say that one of these came back is no what I want to do in that situation we all want to say some table Y and let's say that I want to store 0 inside of there I make the rules in this situation so that's how it's going to work because that's how I decide it will work and we don't close off our if statement we want to close off our for statement and then after we have created our brand-new table with all those sums inside of it I want to return that guy right there and then I want to end this I'm going to say end and then I'm going to put a comma right after that now I can also come in here and define how table values are going to be checked for equality and how we can do that is to underscores again put EQ inside of there is equal to function and I can go table 1 and table 2 like this and then I can just go return table 1 get the value inside of it and make sure that it is equal to table 2 and get the value out of that as well and if both of those come back then we know that that is going to work for us and of course we have to come in here and we want to end this guy right here so you end in a coma another thing we can do is we can also check if it is less than and I'm going to let you use that as your homework assignment to figure out exactly how to make sure that these values work for whether a table is less than or all so if it's less than or equal to another object right there then also figure out exactly how subtraction and multiplication and division works a hint hint hint it's going to be something very very similar to this guy right here okay so now that we have our meta table all set up we need to attach the meta methods to our table how you do that is go set meta table and there's a table we're going to assign it to and there is our meta table so met set meta table right like that and then you come in and check if they were equal so a table is equal to a table that's going to work of course and it had a little bit of a bug up here in the a table change that to uppercase right like that and then down here inside of the Equality check make sure that those are equal fix that and if we do that execute you're going to see that this comes back as true just as you may have thought with this right here and now we're going to test what we did in regards to adding tables so we're going to create a new table called add table and then we can go add table is equal to and then we just pass in a table we can pass in different ones but it doesn't really matter and a table and you've seen this before you can just create a simple for loop that's going to allow us to cycle through our new add table we just created right here and whenever we execute it it's going to add all those values together in our brand new table and print them out on the screen so there's meta tables and meta methods and how we can work with them now let's take a look at object-oriented programming or at least lewis version of object-oriented programming okay so just so to make this very clear lua is not an Opie language and it doesn't allow you to define classes but you can in some ways fake it using tables and meta tables so what we're going to do here is we're going to create an animal table right like this and I'm going to define all the default values for everything inside of it so we'll have a height and a weight equal to zero and we'll also have a name the default name is going to be no name and then we'll also have our animal types of objects also have a sound what's a 7o sound okay so there is the table we have created for animal where the object that we are trying to create inside of here now we have to define a function for our table that's going to allow us to initialize this guy how we do that is with function animal and I'm going to give it the name of new and it is going to receive a height a weight a name and a sound very similar to an object-oriented programming language we're going to have to do now is set our meta table and we're just going to throw in a junk table right there and then animal right there so that's how simple it is to create that meta table now if we want to refer to height weight name and sound for our animal objects we use self because we don't know what the name of it is so we're going to say like this and we'll just go and create that we need to do that for all of the other different attributes we want for our animal objects so we're also going to have weight and change this to weight and then we're going to have name and then finally sound so we're creating it in very similar way as to what you have seen in other programming languages and this is going to initialize it create a new animal object and after it initializes everything it's going to return that animal object course end the function with end say we wanted to come in here and also create a function that is going to allow us to print out information about our animal objects let's call it – string it's not going to receive anything and we can then go animal string like this and string this is another way using string format that I haven't showed you and we could just go % s ways and then let's get a weight for our animal pounds is % F inches tall and says and let's get the sound that it's going to make then if we want to import all those things for our specific animal object we're going to go and get the self name and so weight and so white and then whoops make sure we do this right so height like that and then finally self sound that is a way for us to import all of those specific things for our animal into the to string function here and then we can you just go return animal string and end that function so this is an object function inside of here and this is how we initialize it and this is all the defaults and the meta table we define for it so it's all very very similar to things we've seen in the past let's go and create an animal object let's call it spot how we do that is we would go animal and we call our initialization function and let's just pass some junk values inside of here there's his name and then his sound let's say sound is wolf because that makes sense okay so we just called for our animal object to be initialized we could then come in and go print spot wait like that and then we could also go print spot and then if we want to call the function we just go to string like this to string it doesn't have to have that name it's just the name I gave it I said it was unexpected let's put this quote inside here that should fix it and there you can see it works so 15 comes out that is how we're accessing the weight with the dot operator right like this and then we can call that function which is automatically going to get all that information about spot and print it right there on our screen so you may ask yourself well does inheritance work inside of lua well yes it does and this is how we're going to use it so let's say we want to create a brand new object called cat we can inherit all of the functions defined inside of our animal object just by calling new right like this there we go now we have all that we have the to string we have the height weight name and sound all that stuff inside of our new cat object we can of course come in here and overwrite any of the functions so one of the functions we want to overwrite let's say we want to overwrite new and how it works inside of our new cat objects well we're just going to come in here and replace animal with cat let's say that our cat objects are also going to store in favorite food and not just height weight name and sound well we're going to set our meta table here to be cat instead of animal and then the only thing we need to change down here is to come down and grow this guy right there throw that down and let's get favorite food and let's also store that inside of it right like this and it's going to return and everything's going to work exactly the same we could then also come in here and override our to string function if we'd like let's go and get this remember we just changed this cat new instead of animal new throw that right there and we're going to do the same sort of thing for two string we're going to say cat two string and then how this guy is going to change is we're going to say something like says whatever and loves and let's get his favorite food inside of here so there that is we'll just go to self favorite food and now let's come down and create a cat so let's call it fluffy and we'll just call it with cat new just like we did before now let's just keep it simple 215 fluffy fluffy says meow of course and Fluffy's favorite food is tuna and of course we're going to be able to come in here and go print fluffy to string call that function and you can see fluffy weighs 15 pounds 10 inches tall and says meow and loves tuna so there you go guys there is a ton of information about Lua hope you guys found that interesting of course in the description is a link to all the code and a cheat sheet as well as links to all of the individual pieces I talked about about Lua so please leave your questions and comments below up next C sharp otherwise till next time

35 thoughts on “Lua Tutorial

  1. Learn in One Videos for Every Programming Language
    Subscribe to Bookmark them: http://bit.ly/2FWQZTx
    C++ : https://youtu.be/Rub-JsjMhWY
    Python : https://youtu.be/N4mEzFDjqtA
    Java : https://youtu.be/n-xAqcBCws4
    PHP : https://youtu.be/7TF00hJI78Y
    MySQL : https://youtu.be/yPu6qV5byu4
    JavaScript : https://youtu.be/fju9ii8YsGs
    C# : https://youtu.be/lisiwUZJXqQ
    HTML5 : https://youtu.be/kDyJN7qQETA
    CSS3 : https://youtu.be/CUxH_rWSI1k
    JQuery : https://youtu.be/BWXggB-T1jQ
    TypeScript : https://youtu.be/-PR_XqW9JJU
    ECMAScript : https://youtu.be/Jakoi0G8lBg
    Swift : https://youtu.be/dKaojOZ-az8
    R : https://youtu.be/s3FozVfd7q4
    Haskell : https://youtu.be/02_H3LjqMr8
    Handlebars : https://youtu.be/4HuAnM6b2d8
    Bootstrap : https://youtu.be/gqOEoUR5RHg
    Rust : https://youtu.be/U1EFgCNLDB8
    Matlab : https://youtu.be/NSSTkkKRabI
    Arduino : https://youtu.be/QO_Jlz1qpDw
    Crystal : https://youtu.be/DxFP-Wjqtsc
    Emacs : https://youtu.be/Iagbv974GlQ
    Clojure : https://youtu.be/ciGyHkDuPAE
    Shell : https://youtu.be/hwrnmQumtPw
    Perl : https://youtu.be/WEghIXs8F6c
    Perl6 : https://youtu.be/l0zPwhgWTgM
    Elixir : https://youtu.be/pBNOavRoNL0
    D : https://youtu.be/rwZFTnf9bDU
    Fortran : https://youtu.be/__2UgFNYgf8
    LaTeX : https://youtu.be/VhmkLrOjLsw
    F# : https://youtu.be/c7eNDJN758U
    Kotlin : https://youtu.be/H_oGi8uuDpA
    Erlang : https://youtu.be/IEhwc2q1zG4
    Groovy : https://youtu.be/B98jc8hdu9g
    Scala : https://youtu.be/DzFt0YkZo8M
    Lua : https://youtu.be/iMacxZQMPXs
    Ruby : https://youtu.be/Dji9ALCgfpM
    Go : https://youtu.be/CF9S4QZuV30
    Objective C : https://youtu.be/5esQqZIJ83g
    Prolog : https://youtu.be/SykxWpFwMGs
    LISP : https://youtu.be/ymSq4wHrqyU
    Express : https://youtu.be/xDCKcNBFsuI
    Jade : https://youtu.be/l5AXcXAP4r8
    Sass : https://youtu.be/wz3kElLbEHE

  2. Everything was excellent in this video except one part and that's OOP. It looks ok until you try to create another instance of your Animal class. If you would create another animal in a similar fashion it would print everything just fine, but then if you would try to print out details about first "object" called spot, you wouldn't get the details you've entered for him in the first place. You would get details of the second newly created object because spot details were overwritten. So the approach showed in this video is only suitable if you have only one instance of a class.

  3. Which one is help for begineers ? pls anyone tell me
    I like to make lua scripts any good hearts here for help begineer?😌

  4. Hi I am an apprentice programmer on roblox and I am French so you dreed to make subtitles in French or in other languages otherwise super video

Leave a Reply