Transcript for:
Introduction to Lua Programming Language

and hello everyone welcome to your first lua tutorial well this isn't much of a tutorial it's more of a like a guide into the course so let's first talk about what exactly is lua lua is a procedural programming language and if you don't have no idea what that means don't worry about it we'll get to that in the course used to develop software work with machine learning and to create all sorts of games from basic text games all the way to amazing vr games a lot of people when they decide to learn lua decides to learn it because of how great it is when you want to start making games so if you are planning on becoming a game developer then this could probably be the language you want otherwise most people learn it as well to just use it with c and c plus plus because it's so easy to just implement those two together now one of the things you will need to know about this course now first off the difficulty of the language itself is relatively easy you don't need any prior programming knowledge to follow along whilst it is always recommended to have at least one other programming language of knowledge you can always start somewhere and this is a good place to start the course duration is short the entire course is about four or five hours and if you have enough determination you can probably finish the entire language within a week the execution speed of this language is very fast meaning that it's great for games so i might actually do a whole gaming course on lua as well but we'll see if that happens i might i might not the type of the language as we said before is procedural so you have procedural and object orientated as i said before if you didn't know what that means you don't have to worry about it we will be covering that in the course itself and it's also general purpose meaning it can be used for anything whether it be games or just simple programs anything it's meant to be used for anything in general that's why it's general purpose now here's just some projects that were made with lua such as adobe photoshop lightroom apache http server awesome window manager roblox in fact a lot of roblox games use lua in the back so if you're into making roblox games then lu is probably language to learn angry birds the sims 2 nightlife mafia 2 world of warcraft and fable 2. and that was just to kind of get interested in learning the language more but without any further ado let's get into the tutorial and hello everyone welcome to your very first official lua tutorial so unfortunately for most of the tutorial we will be needing to use the terminal unless you have an code editor that allows you to kind of modify it without needing to use the terminal but just for right now i'm going to keep things simple so i'm going to show you how to install lua if you are on linux at least arch then it's just sudo pacman dash capsule is then you have lua installed if you are on windows or mac you will have probably have to go to the website and download it from there and then use it from there unfortunately i don't use either mac or windows so i cannot show you how to do it but once you have lua installed to make sure you have it installed you can just type lua and you should see something like this if you say hello then it should repeat or call back hello to you to exit you can press ctrl and d and that will exit so let's create our first lua file then so we first need to create a file so new document and an empty file here we can say main.lua now especially for windows users make completely sure that this ends in.lua a lot of times in windows windows will be main.lua.txt you do not want a dot txt it should end with dot lua oops and once you have done that you should have this and yet again a from windows just make double sure this is dot lua and not the dot lua dot txt once you have this you can choose a code editor sublime text vs code whichever you prefer i do like vs code a lot though so i'm going to be using that for the entire course so here is my code editor it's actually open in this folder but if you want to do the same as me then you can just drag and drop and there you go now the file is inside of vs code so vs code is basically just an very powerful notepad you can code in whatever you want but i'm going to do this code once you're in here we can start working on our lua skills so first let's try and put some text on the screen to put text on the screen what we can do is we can say print whoops print and this print allows us to put something in there a piece of text and then that text be displayed back to us so if we go like this to quotation marks this can be single or double quotes it doesn't matter then inside of them you can put whatever you want in this case i'm going to say hello world once you have this here you can run it now i have my terminal open right here and let me just close this part here now what i'm going to say here is just lua and i'm going to run this file right here that main.lua and if i were to just al is here as you can see there is my main.lua we're in the same folder as this file so i'm going to say lua and then main.lua once you say that and enter then as you can see we get hello world and that's just what the print statement does it prints text to the screen and you can have as many of these as you want so these will all work if we save this and we run it then as you can see we get a bunch of hello worlds it doesn't have to be hello world you put anything you want in here any piece of text that your heart desires so that is how to put text on the screen however there is times when you want to leave yourself a comment because you might want to be like okay i'm going to continue with this code tomorrow but tomorrow you forgot what the code actually does in a basic program like this it usually doesn't matter but in a large scale program it can be very confusing if there is no comment to tell you what's happening so for example if we go here then to create a comment it is two dashes like that once you have those dashes there you can put anything you want here notes or a tip for later or something like that i usually leave a lot of comments around my code whenever i'm learning a new language so i can know what this does if i ever need to go back to it and here i can say print text to the screen now the special thing about a comment is it does not get read the program will ignore comments it will just go past him so if we run this as you can see we get our one hello world because we have a one print statement here but if we were to remove those two dashes as you can see my editor already sees that this is not something that should be here if i try and run this code we'll get an error because it doesn't know what text is so it doesn't know what that is right there so that is why these two dashes are very useful so we save this and it will run again now these dashes can comment anything that includes these right here these statements so if we were to put two dashes in front of these then this program will run without any output because we made this a comment so the program already just skipped this so when it sees a comment it skips the line but take note you can also put a comment here so what this basically does is it just puts the comment here and it reads up until here but once you see these two dashes it goes to the next line so this will work perfectly fine as long as this is before these two dashes it will execute but anything after these two dashes will not anything after this comment will also work so if we say print oy then that will also work we get our oil right there and we can also put this underneath so if i were to do this whoops my bad there we go then as you can see it still works but this is a single line comment meaning it only comments out one piece of text so if i want to do another line here i will have to go print oy right and that means if i want to put another line here i have to do this a couple of times and take note they didn't have to be so close to each other we can do that we save it and run it we don't see it but there are so many dashes here and the thing is we can actually create a multi-line comment because as soon as we get to the next line here the comma doesn't exist anymore because it's a single line comment if however we did two dashes and then opening and closing brackets like these these square brackets these are multi-line comments which i believe let me just fix that up there we go thing that these should be against those there shouldn't be a space there anyhow so here we have our multi-line comment it's multi-line because it can span multiple lines so we can say this program is a basic program to display some text on the screen so it can span multiple lines well this cannot say if i say this program and then when i go to the next line i have to add two more dashes here so that is why we have these two so we have a multi-line comments which span multiple lines take note if i were to move this print statement in here it will no longer execute as you can see there we only get oi and that is because it is inside of this multi-line comment but if you put it outside of this multi-line comment whether it be above or below it it doesn't matter if we go like that it will still work like a charm now before i forget now let's go back to printing so i can say print and let's say i say hello jack now if i were to run this if i can just go back here and just do that i can just say lua main.lua and we get hello jack but sometimes there comes a time when you want to add multiple strings to one string you might not understand it completely now as to why but in the next video you will finally understand when and why this following thing will be useful so let's say we wanted to add jack to this now we cannot just do this as a jack because if we were to print this out we get hello and then on the next line we'll get jack so what we can do to keep it on the same line is add two dots these two dots basically means concatenate or in other words add two strings together and in here we can say jack taking it i do have a little space there because it's going to literally do this this is going to be the end result but we see this if we then run this we get hello jack in one sentence we can of course add more things here so we can go like that say dot an exclamation mark maybe go add another dot dot and here i can say my name is steve [Music] now if we were to run this we get hello jack my name is steve in one line because these dots basically did this so we do that and then here it did this and then here it did this that's what those dots did on the end of the day whilst we see this the end result will be this so this was basic output as well as comments in lua so today we will be talking about some basic types and variables in lua we won't be covering all the types because there is one type i want to make a dedicated video about but it's called tables for in case you were interested to know let's get started so there are a few types we can choose from there are nil which basically means nothing it is empty it's if you didn't have a variable it's nil which we'll get to what the variable is later as well but anyways then you have a number and number is like 1 or two three two four and you know zero point one and forty four that's a number they have a string and yeah string is just words so hello or hello world you know like what we did was in a previous video where we printed out text we're actually printing out strings so strings have single or double quotes and yeah that's a string and then we have booleans this is basically yes or no so true which would be yes and false which would be no and then we have tables which we won't be getting into but if you have any experience with other programming languages tables are basically the oop of lua as well as arrays and dictionaries and whatnot so yeah we will later get into tables they're a bit complicated to begin with so yeah we'll leave that for later so to create a variable you can just say local and whatever you want your variable name to be it can be anything as long as it doesn't start with a number or with quotation marks or anything that might be special so to stay safe use letters use underscores and yeah you can use capital lowercase letters you can use underscores you can you can't use dashes take note of that but yeah so to stay safe just stick with normal letters and underscores and you can also put numbers inside of them they just can't start with numbers so anyway so let's start with a for example now if you do that you've created a variable now a variable is just a container so you remember with math in like math class you used to do things like 2 plus 5 is equal to x right you just do that in math well this x right here that's a variable and we're basically setting that variable in this case it's just a but remember like i said in math class it can be any letter it's it's a variable either way or you could have been like seven like 9 plus a is equal to 10 for example and what you have to do is you have to go and say a is equal to 10 minus 9 and then you will get a is equal to 1. but in this case we are actually setting a we are telling you what a is and you can do whatever you need with it so we can actually set a when we do this we don't actually set the variable it's basically an empty variable so if we say print and we just set a then if we were to run that with lua as you can see we get no and as we as you see before nil is basically an empty value no can also be false which we'll get to in later but yeah so no it's just any value it's nothing now to give it a value you can either you can go equal to and give it its value but i'll just set it to mil if you wanted to you can do that if you want to give it like a number value you can make it like two you save that and run it here we get two we can also make it we can also like go here and say a plus five now that would be the same as with your math because you would like a plus five is equal to let's say in this case would be seven because a is two and then you would have moved five over to sign and what not if you run this we get 7 because a plus 5 that's 2 plus 5 because remember this is this variable it's basically just another way of referencing this calling that number so you don't have to say 2 everywhere that you say a you can just say a or let's say you have like a string of text you don't want to put that string of text everywhere in your program you want to have it in one place like an example would be look at this sentence here what is the one thing that just would suck if we had to rewrite this that's the name because the name appears three times and we have to go on every every time i want to change it like let's say phil for example now we have to like let's copy down and we have to go find jack and let's hope we don't miss that jack because then we have to go and search for it again that's a lot of effort so what we as programmers can do is we can just say local name is equal to jack now instead of doing that we can just if we run it we get my name is jack i'm 12 years old i decided my name should not be jack because the name jack sucks so what we can do i'll also certainly want to have the name jack i didn't really mean it to suck now if we want to change the name to fill we can just go fill save that we have only had to change one line of code but that one line of code changed everything inside of this now this is just a small example but what just was on a larger scale like maybe this was like thousands and thousands of lines of story or something like that then this name right there that would become very very very useful now let's say you set a variable you go local name equal to jack again and let's say after you have written like your 1 000 plus line program and you want to make sure that this program is optimized as much as possible for speed one thing you can do is you can delete the name by saying name is equal to middle that will basically delete the name because it will clear the variable so if you want to do that you can so this also means you can later on rename or reassign variables so if we were to go here and say print name and we can just yeah we're going to go like that and we can just copy this c v and then v and then v so your name is jack here name can be 20 and here name can be false run that as you can see we get jack 20 false so once you declare the name and set it to something you can just kind of change it wherever you want to so yeah once you've said it you can just change it you don't have to use local again local is just for assigning it and a local scope so let's say you want to add two variables together then we can go local something like a surname is equal to smither for example then we can just say print name dot dot add a space dot dot surname and we just remove that extra dot which is not necessary run it and you can see we get jack smither we can do exactly the same so if we want it in a variable so we wanted to do that and we can go here and say local full name let me make that equal to the name plus the surname so then we can say full name run that we get jack symmetra if you wanted to add two strings together you also have a multi-line string so let's say let's let's just say there's a description now and description can have like that that is a multi-line string so i can say hello world and remember string actually starts here so i can go cool or color whatever and here remember that that tab it it actually creates a tab so if you don't want that tab you have to go you know you have to do that so you print out description do that as you can see color and then tab hello world and then please and as you can see there's also a new line because everything inside of this is actually taken literally which means if you don't want a new line you have to do that if you don't want there to be a type you have to keep it outside and if you ever were to do that then there would be a space here at the top or a tab here at the top so as we said before booleans either true or false there are multiple booleans you can give so let's say um old so let's say the person is old it should be true so it should be yes basically so all we can say that's uh true and then they are old yeah we'll get later actually using booleans but for now right now you can just know they exist but take note that false which is no and nil are both false values so both of them are basically equal to no so just think that these are the only false values in lua which means that things like zero so if you come from other program languages zero that's true negative one that's true an empty string that's true and true of course that's also true so yeah that is true now let's talk about scoping so let's say we have a box and in this box we have a variable called x so yeah that's an x so this variable we don't actually want this variable to be accessed from outside of this box so let's say this box is inside of a bigger box because this kind of our program will always be structured a bunch of little boxes instead of one big box so this x right here should be should stay here so this if this x is set equal to 2 then this x should only be equal to 2 inside of this box so if we were to print x here so let's imagine that's print and the function you print x here it should actually output two but if we come outside of this box and we say print x then this should return nil this is because we didn't really want this x to be accessed outside of this box because sometimes you just want your variables to stay in a local environment so once you've used it it should not be usable everywhere anywhere else this is very useful and you'll see later on why because right now a lot of these things you're just going to be wondering why do i need to know this but yeah this is a local scope now there is something called a global scope variable so imagine this right here this is global so that's g and this right here that's local because whatever is inside of here cannot be accessed outside here but whatever is outside here can be accessed inside here so if i were to create a variable a in order to make that equal to 5 if i were to print out a inside of here then that would return 5. same with if i were to print a here that would return 5. the reason is because this box right here this box is inside of the bigger box the bigger box basically contains the little box and whatever is inside of the big box is also inside of the little box and whatever is inside of the little box is also inside of the big box but the big box cannot use what is inside the little box right now might be a little bit confusing but if you've got that well done so let's give you an example or another exam let's show you how to create a global scope variable so to create a global scope variable it's as easy as just typing the variable name x is equal to 20 or c is equal to 2. that is a global scope variable you might see that my ide actually gives me a little bit of a blue line here because if we were to just press that there as you can see moxie has a defined global because a global scope variable usually lua should start with a capsule case just so you can differentiate between these two variables it doesn't matter if it is capital or lowercase but just so you know that capital case that will always be global and that will always remind you that hey this is a global variable i should be careful with it while a local scope variable that is like c for example that can be equal to 20 as well or let's make a 10. take note that these two are not the same if we were to print out c and c so big c and then small c and we were to run that as you see with 10 20 and 10. this is because lua is also is also case sensitive so lowercase c will not mean it's the same as a capital kc we could do that and it will still give us the same output but as soon as we change this lowercase c into a capital k c then this little c there doesn't exist anymore my bad but anyways then if we were to bring that out it would get thin because this capital c is now equal to 10. there's another way we can define global scope variables which is also a bit more recommended is by going underscore g and then dot and then the variable name so in this case hello and that can be like hello there do that and then this is a global scope variable as well so if you ever want to know the type of a variable before we end this so let's say we want to know what the type of x is let's say that's 12. so we want to print x then we read get 12. i want to know what the type is because sometimes you want to know what is inside of this variable because it can be anything what you can do is you can say type and then just in curly brace or just embraces you can you can just get it as you can see type so we would just put it in multiple lines you will be able to see that it's just another function or another thing we can print do that and as you can see we get a number if we were to do nil then we get nil because mill is his own type false that would be a boolean if you wanted to put a string in here never give you a string so today we'll be talking about doing mathematical stuff in lua don't be scared but we won't be going to difficult stuff that you know teachers used to go into we're going to stay with the basics so you don't have to worry too much so we're going to be working with numbers so first let's start off with converting a string to a number because you might want to do that at some point so let's say we have a local it's also i'm working in a sub text today i didn't feel like starting up is good at the moment and let's call this maybe like sdr or something and let's make it 22. print the type of str then if we were to lua this as you can see it's a string we want this to be a number so we can work with it so what we can do is we can say to number and this will convert this string into a number do that as you can see it's a number take note that if you put a letter in there you might get a few errors such as just returning middle because it cannot that is how you convert the strings to numbers and again how to do that we can work with numbers a bit easier okay so let's just print out a few things first so let's start with basics five plus five that's how you add that will give you ten you can add more so 5 plus 5 plus 5 that'll give you 20. that's where you go plus 1 this time that gives you 16. okay that's plus very simple let's say we have 20 minus five minus seven and let's do that we get eight let's say one plus two now we have ten so you can add them together and you can subtract it doesn't really matter if we wanted to we could even go into the negative so let's say you want to go minus 22. that will take us any negatives lou has no problem working with negative numbers we can times and stuff like that as well so let's say you want to have two to the power of two that will give you four so you can use the carrot symbol and it's right above the six on a normal qwerty us keyboard or let's say you wanna go two to power five that'll give you 32. or let's swap it around let's say 5 to the power of 2 there we go 25 that's very nice if you want to times it's also rather simple 5 times 5 so you just use an asterisk do that you get 25. five times nine you get 45. and you can times the more you know times two and i'll also give you a number so there's a plus five here and you're gonna say 95 but if we were to add brackets here there's gonna be 415. that is because we're going to work this out before it times this you guys are going to times this then that value they're going to times here they're going to work this out first so that's kind of the basics of math operations there and we also have divide so let's say 10 divided by 2 that should give us 5 100 divided by 3.14 that will give us 31.84 division relatively simple you just use a forward slash we also have modulo so this is also considered the remainder operator in a sense so let's say we have 10 divided by 3 divided by 3 then you get point free for free so basically it's nine but there's one remaining so what we can do is we can say 10 modular three or modulus whichever you want to call it and we get one because it is free time or so so it returns free but there's still one number left there's a one value that it can take in so that gives you one so we were to make this four it would be two because four plus four is eight you can't plus another four because that would be more than ten so you get two so the remainder is two take note this is integer value is not floating quite vowel so it doesn't doesn't that that it's not that part right there it is a normal number so yeah take note about that so not really the remainder it's more just like the remaining number values like the remaining full number that's being left out that's kind of the medullous modulus modulo whichever you want to call it so remember the execution is the same in math no matter where it is so if we say five plus ten times two so you might wanna say you'll get fifteen times two and that gives you thirty but in fact it'll be ten times two plus 5 so it will give you 25. that is because times takes priority if you remember botmas then units brackets i think it's emitting then division and times and yeah all of that so if we were to say maybe like plus 2 times 10 divided by 2 then we'll get 15. that is because it will work this out first it will say we'll go this will be five and then it will times two with five which is ten and it will give out over five i actually find this a bit confusing because you know remembering all this math operation what takes priority especially if it comes to this these long equations this is not really very long but once it does it really just gets confusing so what i like to do is i like to do this now i know these two they will be first and if we do this then what's gonna happen is these two are going to add to each other but you're going to make seven seven times ten is seventy seventeen divided by two that's thirty two i mean thirty and there you go 35 so these will take priority first because they're the closest brackets then this one will take priority if i were to remove this then it would be these and then these and then all them together then we get also get 35 actually because 7 times 5 is 35 and 7 times 10 divided by 2 those are very bad okay makes sense but anyways yeah if we wanted to we could also do something like this let's say this should be first be 2 times 10 and then we'll get 15. so we'll first do this right here and then it will do this and then we'll do that so brackets just allow you to say what should happen first and it's really useful you can use as many as as of them as you want and yeah you can also put things in variables so like let's say x is equal to 5 y is equal to 10 and you can also say like z is equal to x plus y so if we go here then we can say print out c and then we get 15. so yeah that's kind of the basics of math right there we also have a math library which can be incredibly useful so let's say we we can just use print now it doesn't matter we can say math dot p i which will return pi so three point one four one five nine i really didn't remember the rest i only remember these but yeah so there's pi if you ever need pi and let's say you want to get a random number value so let's say we go master randomly all right let's print that out we get 0.84 because it gives you random value between 0 and 1. do it again but that's not very random every time we run it we get the same value that's because computers are really very random to begin with so what we can do is we can go here to the top and to make it more random we can say math dot random seed and as we'll see if we select one then it will be a little bit different but it will stay the same if we say two and a little bit different but it would stay the same so this will kind of seat it so a way to get around the non-randomness if you can say os dot time oyster time just basically returns the current time so if i were to just print of time here then first of all we get a random value and here you turn the current time from i think like 19 something like the amount of seconds it runs again now we get another random value current time 19 and from plot so yeah very useful so with each iteration it would be a different value now because there's you know each second so every second will get a different value we're going to specify that you'd have some things in it so if we get 10 it will be between the integers 1 and 10 so we were to run that we get 6 5 5 5 4 8 so yeah you get a bunch of random values between a bunch of random things and yeah i guess i think both one and ten are included it's very difficult to get all of them so yeah we can also specify between what so let's say between 10 and 50 for example that'll give us a value between 10 and 50. if i can run and you see there we get a random value between 10 and 50. i might be getting a lot of double boundaries here but that's only because i'm taking i'm faster than a second so you know but yeah the random value between 10 and 50 i think both of them are included i'm not quite sure but yeah that's getting random values now let's say we have some sort of list now one thing we do is we just print math dot men so let's say we have some sort of list if we say math to min that will return the smallest value inside of that list which is one so if we just clear everything like that so we did one same if we go math.max that will return the largest value in this case it's 50. you might be thinking it's kind of useless but once your application scales and you get a lot of random values that you might not know or have control of this becomes really really helpful we can also use some extra cool map function so let's say math.floor and let's go 3.14159 that's pi it will turn three basically it rounds down it will always round down no matter what this number here that could be nine it would still round down to three you also have seal so this will go to the ceiling the other one go to the floor because ceiling is up floor is down anyways that will give you four always go up so we go 3.1 always go up no matter what this value is it will always always always go up other math things such as math dot sin and costs and whatnot i don't actually know how to use these but you know there you go like send to any that would be that it also goes like i think you can also do cos cos 20 that will give you 0.40 and then 10 yeah so this is some of the basic math things you can do there's a lot more math things you can do i just showed you how to do some of them so in the previous lua tutorial we talked about how to work with math a lua we talked about the math library so you didn't feel like a map of pi math.cl math. but how do we work with strings now if you can remember strings or text so if i say local and let's say x is or let's just call this string htr is equal to hello world that is a string yeah so that's a string so we print str and we just go here and just say lua main.lua there we go hello world so it's just basically normal ticks that can mean double or single quotes it doesn't really matter if you want to check the type of it you can say type and we can just pass it in like this and that will give you a type of string same if we were to do this in double click because they are the same thing it's just a piece of text can be come from c or something like that uh basically strings are even characters so characters are still seen as strings there is no such thing as a normal just character anyways if you want to have a string that lasts multiple lines so it goes down multiple lines you can start with two brackets so square brackets and the two ending square brackets this will add multiple lines if i go like this this will actually be seen as we're seeing it right now so if we were to print this then as you can see it's hello world and now there's a space here and there's a tab here in the front if we were to just remove that tab then hello world my name is jack and here we can just move that back here so we don't get that extra space and in here we can also say two plus two is is equal to four do that run that as you can see we can go multiple lines down take note that if you were to indent this which means tabbing it up as you can see now it still has tabs or now it also has tabs here so just be careful this actually takes your strings very literally so to get the length of a string it is relatively simple if we go here and put a hashtag there that will get the length of the string so if we run this then as you can see we get 11. if we were to count this as you can see it actually tells you it's 11 bytes and a character is a byte the character is just like abc or a space or whatnot 11 bytes that basically means it's 11 characters long if you were to actually select this and go down here as you can see it says 11 selected which means it is really 11. cool take note that just because this right here is a variable you can still put that hashtag right here because it will give you this it will basically mean the same thing because remember this is just a container if we could we could basically have just said this instead and it would basically be the same thing it's just with a current variable it's much easier so if you haven't seen my video on variable sheet i recommend you do because it can be really handy if you want to add it to a string so let's say we were to remove this hello world so now it's just hello print that out we could have if you want to add to that string you can say dot dot and then here we can just say world that will append to the string to add to the end of the string say hello world signal that that space is lift their intention if i were to remove that space then it would just be one long hello world same thing here we don't need to do it there we can always just do it here hello and then dot dot world give us the same thing because remember this is just a container it just it's just like a stand in for this let's say we have a number so i'm going to create two new variables so local x with my bad local x and y or actually i'm going to make them a separate line so local x i'm going to make that 22. if we were to print the type of x we would get a number because that is not a string that's a number if we were to make do this then that would be a string because now it has quotation marks around it sometimes you want to be able to convert this into a string so let's go local and i'm just going to call this y and i'm going to make it equal to 2 string and this will convert your integer to a string so if i were to just add a comma here and say type of y and you see we get number and we get string because this right here that's a string because it has been converted this number to a string technique that just like before if we were to do this then we wouldn't even need this one right here and we just read that and now instead of saying it will set on a number it will say string so you can see right there you can also do this so if we go like that to go like this and just instead of 22 you say x it will say string because this is the same as what we just did before this so it's the same as doing that so let's talk about escape characters so let's just go here so i'm going to create a print statement with a lot of escape characters in them so there's a lot of escape characters to choose from so let's start so hello and then if you use a backslash you can create an escape character escape character allows you to add things to text so if we were to uh let's say we wanted to have this on two different lines hello world on two different lines one way you can do is we're gonna create two print statements and then say world now if we were to print this out we'd get hello world another way we could have done this is installed in quotation marks because you have used brackets so like that and then like that and then if i were to just go to the next line i could have got a world and you see you get the same output both of the methods are kind of tedious though so one way you can actually get around this is by using escape characters so escape character start with a backslash and then what you want to escape so in this case you want to escape in so backslash n that means new line so if i say world now if you print this we get hello and then a new line world so there's a lot of escape characters to choose from backslash t will give you a tab and let's say i added a bunch of exclamation marks now there's a tab here so we could we can use backslash t or we can add a comma and then we can remove the backslash t so it's basically up to you what you want to do but then give you a tab anyway so let's keep that back ht there so we can just use it like this as examples to get a vertical tab we can just say backslash v and here we can say i am steve do that and now it says goes to goes downwards and then it continues so that's a vertical tab let's say you actually want a backslash how would you actually go about getting a backslash well we can just use two backslashes that will escape a backslash and give you a backslash so let's say stevenetsu to run that as you can see i am steve nitsu if you want to use the forward slash you don't need to escape it you can just use forward slash you see so yeah so let's say you want to have double quotes because currently the text is inside of double quotes one thing we can do is we can change this to single quotes work and do everything the same but it's not quite the same so i would like to just go like this and let's create a double click there's a backslash double click and in here backslash double crochet do that and as you can see hello i am a double quote steve schnitzel double quote in order to remove this backslash it will actually cause an error because now it just gets confused so you can see there so we do not want to remove those backslashes what about if you used single quotes how would you add a single quote let's say you want to add a single quiz here so now we'll be like that backslash single quote do that and now we can have a bachelorette single quid there as well if we were to remove that backslash then it will cause an error and there we go so those are just some of the escape characters there's a lot more i'm going to cover all of them because that will take an entire video now let's just uh go back to printing out string and just adding world here and print this we get hello world so to convert an entire string to lowercase we can just say string that will call upon the string library just like with math.random this is like string and then dot lower and that will cause whatever is passed in to become a lowercase so all the characters will now be lowercase we can do the same with uppercase or upper do that and now everything is uppercase to get the length of the string you have your choice of of course just using a hashtag so i'll go like that and as you can see now it's 11. but you can also do this so string and then dot length or just that link but anyways do that you get 11 as well so it's up to you it's your preference you know which do you want to use do you want to use hashtag or do you want to use lin to get a substring from a string you can just go here and we can say sub instead and then we pass in the string we want we're going to start set index one and let's see let's say you want to get the entire hello that would be these right there as you can see down here five selected so i'm just going to pass in five here so from one to five and we get hello if we were to pass in from seven to let's just go 99 then we get the end of the string reason at 7 is because this is 6 that's space that would be index 6. so we start at 7 and we just say 99 because we know it's the last word in the string and we don't really care too much about counting it specifically to get a character from ascii to a character we can just say string of char and here we could pass in the number so 65 that would be an a yes capital a don't know what ask is don't worry you don't need it to know lua or you don't need to know that you know lua but yeah if you do know then good for you so that's this path turn ascii character into the character we can also change it from ascii to or i mean from character to ascii by going fight and let's say we want to convert capital a into a byte now we get 65. the same thing but just the other way around now lowercase a and now should get 97. so as i can just kind of swap between them if you have more than one thing then you can let's say uh let's say this is sdr now you can convert that entire thing by getting one and it's just say 99 do that and as you can see the entire thing was converted so we just resized it a little bit as you can see there we go if you want to repeat something then it is rather simple you say rep for repeat what you want to repeat in this case let's say you want to repeat hello how many times do you want to repeat this i just want to repeat it ten times and you want to separate them by uh i'm just going to say space i don't really matter anyways you can also change this so let's say comma and now it's separated by commas to format a string it is rather simple so we can just say format and now this can be kind of useful so let's say we have pi now pi lets you want to get it to so this will allow us to get pi to the second value so it's 3.14 that will give us 3.14 i'll explain in a second on how that works and let's say i want to say my age and here i can say percentage i i believe and that will allow us to be an integer so we can do this we can go here and now we can add all things so i'm going to say math dot pi and that will give us pi and here i'm going to say 18 because i am 18. as you see we get three point one four and eighteen so percentage i will allow split an integer in and i think percentage is will allow strings and what not so if you like this way this is kind of like the c way of doing things then you can do this uh this right here that allows us to kind of format it so now we'll go free and if it wants to we could even make it 10 and now we'll go up to the 10th value so yeah that's kind of one way to do it i don't really use the see this format too often so i can't really tell you too much about it but there you go if you wanted to know about it then that's how you do it so you put in your string percentage and then what should be formatted and you you pass in your values relatively so integer is second so integer comes in second that's a number anyways so to find something in a string we could just say here and then go dot find i'm going to remove this and pass in string and then you can pass in what you want to find so if i want to find our owl so hello world then if i were to whoops about to just say that and run that and you see you get eight and ten so it starts at eight and ends at ten so then it starts here where which is 8 and it ends here which is 10. if you search for something that does not exist then you will get no you can also use dot match so match will allow you to try and match something to strength if i go or al that is going to return that string if it finds it so all url if it doesn't find it so let's go over srl it returns no so this one returns the position and this one returns where they are this can be kind of useful so let's say um we have local begin and ending let's try to do that and we probably want to put a comma there from correct and you can make that equal to string dot and we actually want to say that fine so this will be like that like that and then find url now we can go here and we can say uh where does it begin and then you can just save again and you can go back to work and go to a new line and say end ending now run that that has been stored in it has been stored into two variables which is kind of useful because you can see here it kind of returned two values just separated by commas so then we can split them into two different variables by doing this right there if we replace things inside of a string so let's just bring back our old piece of code here remove that we're gonna replace something in a string it is relatively simple it is g sub and this will replace fixed string so let's say we want to replace things inside of sdr and we want to replace all the lowercase o's with an exclamation mark do that and now all the lowercase o's have been turned into exclamation marks and this says how many so it returns the new word plus how many things has been changed uh let's say you wanted to change all of the hours into happy face you can't do that so doing good all right he happy face face happy yeah it is very very good feeling but yeah you can't do that if you wanted to so the else has been turned into happy face yeah and that's all i want to actually teach you all there's a lot more but these are just the ones i'm going to show you and i hope you all enjoyed i can now at least do a lot of string functions inside of lua so today in this little tutorial we'll be finally talking about if statements so this will allow you to kind of add some logic into your code make things happen so yeah let's get started so we talked about booleans which is either true or false and false is the same as no in a sense nil and false are not the same but both of them basically returns false and if statement basically work like this if true then re then do something so we go if then we'll actually autocomplete it for us and we need to say if true and here we're gonna say print statement was true if you were to run this it will execute like nothing has actually changed let me just go here and say lua made the lowest statement was true okay that's pretty cool but if we were to turn this into false then here as you can see it never executed because this only executes this if statement only executes if this right here is true and no of course no does not return true so it will automatically escape but put anything else in here and it will basically return true so if i go to for example that is the same as true the only false things inside of blue is null and false take note of that you might be thinking how can this actually be useful because if you say if it's true or false it's kind of not very useful well one thing we can do is we can use comparison operators so if i were to just go here we have different ones so we have bigger than which we'll be covering all or at least most of them less than bigger than or equal to less than or equal to not equal to take notes not equal to take note if you're coming from a different language this is not equal not this anymore so this here that's not not equal this is now not equal so we're coming from a different language taking out that and then there's equal to we'll be covering them right now so let's say here let's go and create a local variable and call it h and let's say age is 15. now here we can say if h is more than 17 then we can go here and say you may enter now if we run that it will not execute because age is less than 17 or age is not more than 17. say that if we were to make this a 17 it will still not execute because this means if age is this plus one so if it's more than this value here which means if this is 18 it will work you may enter if you wanted to work on 17 you can either go if it's more than 16 or you can say if it's more and equal to 17 and that will allow it to win at 17 also allow people to do it it was more than 17 and age is less than let's say 60 then you may enter we run this it will not execute and if we make this 60 it will not execute but we make this let's say 30 for example it will because this checks if this right here and this right here so if both of these return true because remember this that will return true if age is more than 17 and this will return true if age is less than 60 less than 16 not literally equal to if we want to include 60 you can say list or equal to 60. we can also do maybe something like this if age is more than 17 or or ages less than 10. we do this it will work if we make this and say this is 15 it won't work but if we say this is 10 i mean 9 because it has to be less then you may enter so this right here is basically if this is true or if this is true where if and it was because and is like this true and true and then that will execute but true and false not execute same with false and true they'll also not execute but with all it's a different story if it's true or true it will execute also here if it's false and false it will also not execute so this one appears a lot more string strict if it's true i'm just gonna copy all of this here right here otherwise i'm gonna have to type it around and then we can just paste it there so when we just change all of this to or and actually so true or false that will execute false or true that will execute false or false that will not execute with all there has to be one true value with and both of them or all of them has to be true of course you can use you can be more here so let's say or and then you can add more things but yeah we're not going to go that complicated we're going to stick with this for now if h is not equal to 20 then you may enter that should not be an exclamation mark that should be a tilde there we go it's not equal to run that and you may enter but as soon as i make age it could be 20 it will still give us it will now no longer allow us to enter we can always change do like this so um local name is equal to tom and for now let's just gonna say h is equal to 20 then you may enter so now you can you may enter and let's make this 10 now so now we'll no longer print that we can do the same with names so we can say if name so we can match strings and numbers so if name is equal to tom and you can see you may enter and we can even match them so the name is equal to tom and age is equal to 10 you may enter so maybe you're not creating a bot because you have an amazing tree house and it's so technological this can be your way to go if you're if the name of the guy is tom andy's 10 you may enter we can also so if we were to change this to 20 now it will no longer work but if we were to change this to or then it will so that's kind of how to use and or bigger and less than more than or equal to less than or equal to not equal to and equal to inside of if statements let's say we have another estimate here if not age is less than 18 print you may enter guess what the output will be and you just remember to uh there we go if you guessed you may enter you were correct basically this right here is it just means not so if so if this returns false then this will make you return true so not true that will equal false because false is not true same here if it's not false then that will equal true because if something is not false it's true if something is not true it's false so that's kind of how this not operator works so let's talk about if else statements so let's say we can go like this if h is let's say more than 20 then print you are old else print you you are young let's see what will happen you are young why is that because when this doesn't execute we can use else to execute this so we were to make this 21 then only the first one will execute so basically if this executes then this will not execute if this execute that means this did not execute so if this returns false so if this will if statement is false it means this can't be executed so then we can use else to tell okay if this is false thing please go to this that's kind of out so you can make more specific by making an if else so i mean else if i mean so then you can say h is more than 10 then print you are not old or young say that we get you are old change this to be let's say 18 you are not all origami change this to be nine you are young so this is basically a simple way of showing you how things get triggered you can actually remove this variable right there so basically if this one gets triggered then it will execute this and skip the rest if this does not get triggered it will try and execute this if it executes it will execute it and skip the rest and if all else fails if neither of the above if else statements executed then it will execute us you can add as many else if statements if you want else if age is more than 5 then print boohoo or whatnot and they gotta actually like that and then we can just format it to look a bit better and as you can see there run it we get boohoo because age is more than five but it's also less than 10 but you cannot have multiple else statements you can only have one else statement per if statement you can have as many if statements as you want as well so i'm going to go here and say if age is if type h isn't where we can use it so if type h is equal to and here we can say i think this should work number then print h is a number value run that and as you can see age is a number value as soon as we change this to string then it will no longer execute so you can have many statements that you want you can even have if statements within if statements so you can do that that is totally valid it's very you can do basically anything you want here print or read on your background we can just change this to like 21 or something okay one now that if statement will execute whenever that is there or we can just say 19 now that estimate won't even execute so there you go you can use a lot of if statement way or you can do a lot of things with base if statements in their own little way as you go from a different language you might be asking okay but what about the ternary operator ternary operator is basically just something like a one liner so let's say we create a variable and we call it old now this should be true or false are they old or they're not old true or false okay one way you can do it is by wrapping an f statement so we can say just local old and we can say if age is more than let's say 30 then alt is equal to true reading will make all equal to false by default so it will only become true if age is more than 30. do that and you can print all now if you were to run it then it will say false because age is not more than 30 we go here and say 33 now will be true determinate operator allows you to do it all in one line so what how you can do it is you can basically go here so if age is h is more than 30 and true or false that may look a lot of very confusing but basically it just checks hey is that true and that true so if both of these return true then this will be true otherwise we're going to make it false so we don't need the brackets first so you can just do that if you wanted to now let's see we get true because they are old because age is more than 30. but if you go here we can say 20 then it will be false because they're not old age is not more than 30. so that's one way to do it so yeah that's the term operator basically if true and true so we basically check if this is true as well so both of these two return true because one will always return true but this one would always return true then return true because then it doesn't have to execute this because that's an or otherwise if one of these two return false which would probably be this one then return false so then this is false a little bit confusing if you didn't like the training operator you don't need to use it at all if you don't want to but yeah that's a statement i hope you understood and know how to use if statements now so we have worked with if statements and stuff like that today we will be working with loops so lua itself is amazing i love language it's just perfect you know what makes the language even better the loops loops are basically everything in programming for the most part so let's talk about what a for loop is so basically a full loop is for let's say i is equal to one and then we can say 10 and one i'm going to explain this in a second don't be frightened just do and yeah you have to complete it after which is really nice and here i can just say print i never run this so just lua and then main.lua and we get all numbers from 1 to 10. that's amazing what did we do basically we said okay create a variable called i and it's this is by default it's local scope so which means we can't print i here that's not possible so you can see we get nil because i hear that's local scope it's by default anyways then you say okay start at number one and go um go up until 10 i think actually this value right here that's even optional so you can just do that and by default it will go and move it by one but anyways so and it says go through one to ten just print out the values one two ten and store it inside of this variable so this variable gets reassigned every time this loop runs so it goes here okay prints one it seems ends it goes back to the top it goes here okay now this turns into two because it goes plus one goes here print two and okay goes back here i becomes three goes back goes here print three i becomes four and it just continues like that by adding another number you can see how many steps as you go through the loop buster if we say two then we get one three five seven nine because that's a one two three four five six seven nine now it's one skip two three skip four five skip six seven skip eight nine skipping we also do three so you want to move it by three steps that's totally possible if you want to move it by two steps but you want to make it count only the double values we can even start here with two then now it's two four six eight ten this can go as far as you want to do this let's make it a thousand no problem with that you can even make the step one again and you can even make this like that and it can do it it can handle it yeah that's basically the basics of a for loop you can also reverse a for loop so let's say we wanted to count down we're going to want to count backwards you might think okay we get 10 and one but as you can see it actually really gives you an area like what's happening here because if we were to try to run this it just won't do it because it's starting at 10 and has to go to 1. so it's basically checking okay it says i to 10 then it says okay is i more or equal to 10. so basically before it runs anything it basically has an invisible if statement right here and it says if i is less or equal to and then the value that that's basically here so let's say that's one then it prints this one so it has an invisible if statement that we can't see it's been given to us and then it runs but the problem is 10 is not less or equal to 1. 10 is 10 it's more than one so this right here that would become an invalid if statement so what we have to do is we have to say the stitch should go backwards so now it's going to say ah okay i understand so if i is more or equal to 1 then you do it so that's basically what's going to happen here i'm going to move that statement because it's kind of unnecessary to have it so let's run this and count backwards now it's 10 all the way to one that's pretty cool i'm going to move this closer so that's pretty cool so this kind of account count that's kind of how you can count backwards you can of course i guess decrease this value so make a negative two and they will count down by negative two until wherever you wanted to of course you don't need to specify everything right here so if you wanted to we can go and set star and we're going to make it local local start bell and then end val and then we want to step about and i'm just going to put them all in one line so i'm going to go 1 10 and i'm just going to go one so of course you didn't have to put them all in one line i just did it because it's faster you could have made them all on their own separate lines so if you don't understand what's going on here basically this value gets set to 1 this value gets set to 10 this value gets set to 1 back here basically they get set to these values in their order so here we can just say start pal end val and step down the internet runs perfectly fine you can use variables because remember variables are just containers they just contain the value that's all that it that's all they are just contain a value just container so yeah you can also loop through in array so here we can go and create an array so let's go local r and we can make that equal to 2 3 and let's just add some random values here okay there we go random values added now what we can do is we can say okay i starts at one until the length of this array so i'm just gonna say hashtag r and now here base i mean that was supposed to be one anyways now here we can say r at index i i know i really haven't covered arrays or tables or anything like that but this is kind of a future reference basically eraser like variables but they can contain multiple variables you don't have to worry about them right now just i'm just showing this to you now so in the future you know what to do i'll also cover in my future probably run that and we get all the values inside of this list this array so basically this array right here is just like an array of variables it's like a bunch of variables it's stored inside of one variable that's kind of what is happening here not going to be covered in today that's not our goal here okay so you might be wondering okay but it's full of all we can do because that kind of isn't always that useful maybe we want like an infinite loop that's going to be more difficult to get with a4 loop well yeah we can so let's say we have a variable called peeps and peeps just stands for people let's say we have uh 10 people that's how many people is in the building so then we can say while so this is a while loop and while this value right here is true it will run let's say while peeps is more than zero so while peeps is basically not there while there's or no zero people in the in this in this location so let's say this is inside of my house we're having a party 10 people are there while there are still at least one person at the party we run this loop so we can say print people left at party and we can just go here and add peeps before that i just want to say peeps is equal to peeps my word peeps minus one now if we were to run this for loop people have to party minus it all the way until zero so once zero gets hit it's over of course we could infinite loop this so if we wanted to we could just in fact i'm just going to print out something here print and loop i'm just going to say this is true and that's going to be fiddly because while this is true so while that value there is true it's going to run this loop and since we don't set it to false it's just going to run this loop forever until i guess my pc crashes or something which has happened while i could lua so i don't want to attempt that so yeah that's kind of a while loop so you can always do something like if you think okay that's okay but you can do the more things like a game kind of uses a while loop an application let's say run so oh gosh that should be run run is equal to true right while the application is running so running so while it should run it should do the game the application things so we're going to create another one here and say local run time or something and i'm going to make that to zero and let's say we print oh gosh running here so this allows us to see that it's running then you can say if and let's say run time if runtime is equal to 10 then we want to break out of this loop so i'm just going to say run is equal to false because only remember this volume only runs while run is true so while this value here is true so we make it false it's not going to run anymore and then here we can just say runtime is equal to runtime plus 1. now if we were to run this it's going to run for 10 run times so yeah that's kind of a basic application loop here so you run until you hit the value or something that you want and you break out of it i can actually show you a real example of a loop like this if you want to see it if you don't you can skip to the next timestamp if i include it please feature me anyways so to do that i'm just going to open up a new terminal here and i'll show you an example of where you actually use it i didn't actually have an example in lua but as anyways so this is actually open source application i made i'm going to open up salon text because i don't really need to open up here and i'm just going to open up main.c plus i'm going to show you where i would use a while loop because this application basically just runs a program until it exited so here while running so this is a variable and it's while the application is running so you do all of this until it's running here gets returned and set to false then this volume is going to break out and it's going to break out of the application if you don't know c plus don't worry we're not going to cover c plus plus but basically this is an application dude if you want to see this application in action here we go there's the application in action now i'm going to choose the playlist to play i may want to cancel as soon as they exit here it's going to exit out of that application itself so exit so that's kind of our while loop can be useful you can actually create functional applications with it so we have one loot denied that you can't definitely find useful so you have to repeat until so let's print until their first so let's say until x is more than 10 let's just do that i'm going to put x there now and i'm going to print hey there and then i'm going to say well x is equal to x plus one or yeah plus one and i'm gonna create a local variable x local x is equal to one okay let's run this and see what happens we get hey there now you might wonder okay but what's the point we have a while loop i'll reuse this well let's see the difference between a wallet let's create a wallet here anyway there you go it just prints it out normally so basically what's happening here is we're saying well x is less than thin just print everything out now if you repeat we're saying okay printed this out while or until x reaches this so it's basically the same as one loop but this code will run at least one time so to give you an example so here it says well x is less than 10. so if we make x equal to 11 for example it's no longer less than 10. it won't even consider running that loop but if we were to comment that while loop out and do this it's going to run the loop at least one time at least once so basically it's saying okay run this and then check is x more than 10 oh yes it is then breaks out of the loop so yeah that repeatedly basically is just a while loop but it runs at least one time at least once so just repeat until it reaches this and just take note if we were to do this repeat and let's just say false here print hello okay you have five seconds tell me what's going to happen in this piece of code comment it down below do whatever time's up okay so we get an infinite loop but why is that because when we did it with the while loop when we set this value to true it continues it runs but this one if we set it to false it continuously runs what's happening here basically it checks until this value right here becomes true so once this re is true it stops the loop because you think about it so if we set x equal to one then it goes here okay is x more than 10 no x is not more than 10 continue with the loop because it's until then this is basically it works with a false this works a bit true so you should just keep that in mind that repeat loops work with false values while while loops are of true value but for most part if you practice them you don't even recognize it anymore notice anymore so do you want to hear a story about my childhood if you don't want to you can skip it but anyways when i was younger when i just started learning programming i really wasn't into programming at all i didn't really feel that you know attached to it i didn't really think i was going to do it for a living but do you know what was the thing that changed my mind that made me go like wow programming is amazing there is literally nothing better i was getting user input user input changed basically my entire view of programming when i was finally allowed or none loudly i finally learned how to get user input i was like yeah this is what i want to do this is what my future is going to be and today i'm a full stack with developer but anyways so today we're going to talk about getting user input user input is user input we get input from the user from let's say the terminal right there so if i want to get the user input from here it would be rather simple so io dot read basically this allows you to get user input i think you can just use it like that and yeah then you can get user input can't really do much of that but can you but it allows you to get user input what we can do is actually store this and let's call it ends for like answer or something sorry instead of a variable and now we can print that variable now let's say hello now we get hellu back or let's say print what is 10 plus 5 my bad but it's 10 plus 5. now we can actually put it in and we can be like uh we just print here your a little bit back sensor your answer and then we can just put your ends now what is ten plus five let's say it's nine your answer is nine what is ten plus five let's say it's fifteen your answer is 15. there we go we got user input that was very simple but what if we wanted to get this input right next to this output how do we do that because print statements they give you a new line by default so i have to insert it right there so what if i want to do something like this uh go input ten plus one let's say we made a math game and how would we how would we do this because now if we were to run this yeah we're still rather close but the problem is we're starting a neckline i don't want to do that i want to start here i want to start right next to it well one thing we can do is we can actually just say i o dot right for the most part you wouldn't really use either right and we're going to cover it too much right now but it's just a way to get to put something on the screen and it doesn't give you a new line by default so now i can be like 15. there we go so let's create a little game with this while we're learning how to get user input let's say we want to get that value so let's let's actually go like this num uh local num1 num2 and we're gonna make that equal to ten and five and that that can you make it more useful now we can do this we can go here and say num1 and here we can say plus and then here we can just know dot dot and say num2 basically the same thing but here we can actually change the number values but we're going to create game and it's going to be fun and then we want the user to get the input now if we're going to basically do it right now we get 10 plus 5 and we're gonna add ten but how the user know they're right let's say they're in grade one and they they're just learning how to add and subtract and stuff like that well what we can do is we can actually create an if statement so we can say if and actually we can go like here and say local true true anse for our true answer and here we can make that equal to num1 plus num2 so now we know what the answer is the computer knows what the answer is what we should expect so we can say if and we can say uns but take note we want to compare numbers no not strings so we actually have to say to number and here we can say ons so if answer is equal to the true answer like what we should really expect then print you are correct cool but what if you're wrong so then we can say else and here we can print this and say your answer and just put the answer there is incorrect try again okay now let's try it uh seven what my answer seven is incorrect let's try it again fifteen i am correct the answer was fifteen we basically created a basic input game today we will be discussing tables in lua tables represents things like arrays records sets lists cues all of those things if you have heard about them then you know what they are if you have not heard about these things yet don't worry we'll be covering them today so the basic concept around a table is that a table is almost like a container for multiple variables so let's give an example of maybe local x is equal to 10 and then you have local y is equal to 10 and they have local z is equal to 10 and the variable names they don't matter to you really but you just need these values that should make them different maybe like 15 and 20. but the the names of the variables they didn't really matter to you just need these values to be stored and you have so many of them because sometimes you can have hundreds upon hundreds of variables in your program or maybe just get data from outside your program and now you have to store it inside of variables and that's going to be a lot of effort because there's a lot of variables that has to be created a way around this is creating a variable it can be called anything i'm going to call it r because i know tables the best as arrays or lists you will hear me a lot during this tutorial say arrays lists sit all of these things the real name is table but because of my previous programming knowledge i might say array or even list but you can go here and by doing this by putting these brackets here you have created your first table tables can contain values so 10 15 20. we did the same thing here let's just add a comma there here as what would have been if we did this local x y and z is equal to 10 15 and 20. but the difference is we did not declare more than one variable which means we still reserve these names for maybe a different variable we'd want to use this is just setting all of the variables to their respective values the point i'm trying to make is that we put all of these values inside of that one variable instead of making multiple variables technically you cannot print arrays so we print array we save it we go here and we run the lua file you'll get this right here this is the value of this variable in your ram so this is their location so to get a value inside of an array you use these square brackets and you say what index so index would be index one index two index three so each one of these is an index if we want to get 10 that would be index one save it run it and we get 10. if you want to get 15 that would be index 2 run it whoops run it and we get 15. index 3 that would be 20 run it and we get 20. index 4 would be middle because there's no value there so you don't have to worry about going overboard because you will get nil if there's nothing there an array can keep anything so you might also know if we put our local x y and z here these variables you can keep things like 10 and let's say hello you can keep all these inside a variable so x would be 10 y would be true z would be hello you can do the same inside of an array so here we can make it true and here we can make this hello world and here you can probably go like 2.4 and you can put any data type value in here because an array is just another variable that contains multiple values take note that now this 4 will work because 1 2 3 4 index 4 is covered by 2.4 run it will get 2.4 if we do free then we will get hello world take note that you are not limited to a specific length as far as i know when it comes to tables you can put as many things in there as you need to get the length of an array because sometimes you need to know what link is you can go here to the front and just put a hashtag there just like with a string this will give you the length of an array so we refresh and go here we get four this is nice because if you want to get the last element in an array so the one right here is a 2.4 and you didn't know what that is you can go r and put this inside of square brackets because this returns four so array at index four is one two three four go here right and you get 2.4 because it's right there 2.4 let's create an array here with random values and this is random values if we were to list them out we just get the random values it's 10 15 23. what not but we can sort these values so what we can do is we can say table dot sort and this will sort these values for us and then we can just pass in the table because here it actually tells you what it needs but we can pass in the table in this case r and that is all it needs you can pass in a function to tell it how to sort it but it isn't required now if you print out r at index one you will be receiving zero because zero is the smallest value in this array do that and we get zero if we were to print out two then here that would be one because one is the second smallest value so it will be at index two and we just save that and run it we get one we can loop through an array as well so we can use a for loops for i becomes one to the length of the array and then we can just print the array at index i the reason this works is because i is a value it's a number so we put one in here it would be the same thing but i just changes with every loop so we do that we get all the values in the array in their order however if we were to sort this array then now we would get the array in its sorted order so this is a nice way to visualize what's in your array as well so that's how i can fall through an array you can also insert data into an array so you can just remove that and just run it to see the original value right there to insert the value into an array it is rather simple we can just say table dot insert and here we can specify the array or the table we want to insert things into the index or the position we want to insert something in so let's say you want to make 15 move upwards and put something there inside index two and then we can say what we want so let's say let's put a string value there with that there now if we run this we get 10 low 15 instead of 10 15. so we have inserted the value into the array we also remove a value from it so remove and we can specify what list we want to put here and in what index we want to delete so let's say we want to remove 50 because maybe that's a little bit too big one two three four and there we go 50 is that index four save that and if we were to run this we no longer have 50 in our array we also concatenate a array so i'm going to put a few strings here so i'm going to say hello world i am going to put these two together then you can just delete that and we can actually just remove that as well and here we can just say print table dot com cat and here we put the table we want to concatenate so r and then how we want to concatenate so spaces basically what this means is okay take every index here and put them into a string separated by spaces so in this case if we were to save and run this we'll get hello world i am steve separated by spaces let me change this to something else if i were to put commas here i could run that and now we have commas if we wanted to put a bunch of exclamation marks here for whatever reason we can so yeah that's how you can concatenate tables you can also have a multi-dimensional table if you're new to programming you don't have to worry too much about multi-dimensional things just yet until you really need to use them but to take note a multi-dimensional table would be a table inside of another table so in this case we were to add in our table here we could go one two three and here we can say seven eight zero and here we can say whoops then just fix that nine ninety nine and nine eight nine or something like that so this is an array within an array so right so this right here has a bunch of tables inside of it as well so this one table has three tables inside of it so currently if we say array then we get its location in the ram if we say array at index 1 [Music] we'll get the table inside of array at index 1's location in the room so we'll get this table's location because we cannot print out the table put a one here and now we will get this one right here so we're accessing this table so this one right there they're accessing the first table so this table right here inside of this table and then we're accessing the first value inside of the first table inside of the table so right there bit confusing okay then let's make this two we'll get six because now we're saying okay go to this array go to index two and give me one okay we get six if we were to make this three then we will get nine because now it's at nine if we were to make this let's say two so that's going to say okay here go to index three which is this right here and go to index two that's this right here do that we get 99. so that's a multi-dimensional list we can also loop through a multi-dimensional list or array or table by going four and we're gonna say i is equal to one to the length of the array and currently we can't really do much so we print out what is that array at index i then we'll just get a bunch of locations because currently we're just getting a bunch of tables back so we need to create another for loop inside of this folder so we'll make this one j at index 1 all the way to the length of the array at index i i'll explain that in a second we can move this upwards here paste it and we can make this j run it and now we can loop through the entire race it's one two three one two three six eight zero six eight zero and nine ninety nine nine eight nine we can add more values it doesn't have to stick with that specific amount i was just giving an example right again it still works perfectly fine but what i wanted to say is we here say look through this array right so that's what's going on here then we're saying okay for every array inside of this array loop through it so that is what's happening here so we're seeing if i were to give you a little or a visual example so i is equal to one right and j is equal to one when we set it so then we say okay print array so r at index i which is one so i'm going to make that one so we can visualize it and j at one so i'm just going to maybe say a one j and one i just so we know which is which right and then we say and then this right here one one that would be this one right there then we say okay now loop through this one so it's going to do this loop first and it's going to say okay then j is going to become two that means we're going to get two and then j is going to become free we're going to get free j is going to be on four we get eight j is going to become five and give it zero and then that is the end of that list so that's the end of this one's length so then j will become one again but i would increase to two so it's going to give us six and then j is going to loop again that's going to become two there goes eight and they're just gonna become three that will give us zero and then we reach the end of j again j becomes one and i becomes three and this just continues until it reaches the very end of the entire table wow that was a lot to cover so that was the basics of tables we'll be covering them a bit more in the future while we work on different things so here we have our terminal i decided to use the integrated terminal for one so you can just go to terminal and say new terminal yeah there we go then we have our integrated terminal here and here's our lua code we should actually remove that because we'll be getting to that cool so today we'll be talking about functions so a function you can think of in a sense as a block of code that you can basically copy and paste without having massive repercussions so imagine you have like a block of code so let's say we print out um print you are five years old and then we go and say okay now here we can put a few new things you will be and you can say seven years old in two years all right and in here we can say you and we can go were for whoops four years old last year cool now we have that so if we have that we can actually just print that so we can just say lua and then run it here so okay why isn't it in this folder location let me just quickly go there i believe if i just maybe drag and drop this in here it would probably no it will not auto complete it for me okay then go to documents trash lua and here we can execute our lua file all right so here it says you're five years old you will be seven years old in two years and you will be four years old or you were four years old last year perfect but now let's say here is 300 lines of code for something else let's say the game and this is what we'll print out and now let's say a year has passed or something like that and then you have to paste it again but now here you have to go get five there will be six uh you will be seven years old okay so that would be uh eight now oops at eight and you were four but now it was five and then here you have like another 100 lines oops and i have to do it again and you have to paste the same three lines of code over and over again which kind of adds up and this is a very basic example there are much more complex examples than this but for right now i believe this is perfect because it's just such a basic and small example so here we have it and now it's like okay now we have to update it again well it's not really updating that's bothering us it's more than about the fact that we have to copy and paste these free lines of code because some sometimes it will be 15 20 or even more lines of code that we have to copy and paste every time so if you do that bugs can happen and probably will and it's very bad practice so there's a way around this i'm going to keep this one at the top and that is functions so we can create a function by just saying function and here we can say for example display age and here we go so this is a function and in this function we can cut this code and we can put it inside of this function here and you can also make this function local if you want so this thing with functions is basically the same with everywhere it's just like a variable so if it's a local function it can only be used inside of this file if it's a global function or an underscore g function which is the same as a global function then that can be accessed anywhere but right now we're just going to keep it local now this function is special in the same step we can just say display h if we do that and we run it here as you can see it displays the code for us we might be thinking okay but that doesn't really help us much because we're actually using more lines of code but the difference is we can copy and paste this and have it print out multiple times as you can see here so instead of just being three times one two three four five so instead of just being three times five this is just okay we have this and we can have five of these so it saves a lot of lines and in the long run this will actually clean up the code and make it easier to understand as well because whenever you need to know something just go back and reference this function here so basically a function is just a variable in a sense that keeps a piece of code and executes it as soon as it is called so that is what a function basically is now let's actually improve our code because in the previous example we actually changed the age so here we can actually give it a value and let's say age and this value age here is called a parameter you can use this parameter to update your function here so i can say okay you are and it's five but this time we can just say dot dot dot dot and we could push age into this and now we have our h there's now there's a variable here same here we can just remove the seven and add a dot dot dot dot and here we can say age plus two and in here before we can say okay dot dot dot and here we can say age plus i mean minus one let's try and do it now so display age and here we can say okay this age should be 22. let's try it you're 2 years old you'll be 24 years old in two years you're 21 years old last year that's perfect because now every time we want to do this again but for something else it is start with let's say seven or yeah let's start with five and then let's do one for maybe 13. and then one for 18 and in one for 25 whoops that's 50 there now if we save and run this we get one for each one so your five years old will be seven or where 4 13 will be 15 12 18 20 17. so this is a way to just clean up your look because instead of copy and pasting this and then having to change that manually you can change it in here but now you might be thinking okay but what if i just don't want to actually add an agent here i just want to keep it clean unless i specifically want to change it later on well to do that you can create this parameter right there and in here you can say age is equal to age or and then let's say five so if there is no value passed into age it will default to five so if we go here and i'm just going to add an extra print here just to give us a clean line at the end as you see you are five years old so it defaults to five so if you don't want to add anything here you can just say okay take this and make it equal to itself but if itself is not really a true fee value if it's empty if it's nil then make it five so the basic thing age is equal to eight and if age is nothing it's nil or five and since five is true fiba nil is not true fee it will choose five unless age actually has something and just make sure that this here is first if you swap the two then it will always be five so if we go like this five or and then age and if you go here it will always be five so just make sure that age is first so that's how you also add default parameters here so now that we know the basics of it i think it's about time we can continue to something but more intense so i'm going to create another local function and this function will be called sum some will take in a number so num1 and num2 and what these names are they don't really matter you can call them whatever you want anyhow they can say return num1 plus number two return is a special keyword in the one or an action most programming languages what it basically says is okay return this put this inside of a variable because if you didn't have to return so we go here and say just local x is equal to sum and here we can say two and three so that should be five because two plus three is five so we can take multiple arguments and we can also add in stuff and if we say print x then if we go here we get five but as soon as we remove this return then it will just kind of say hey i don't know what's going on even if we add a variable here so let's say let x is equal let's sorry my javascript is coming in here local x is equal to num1 plus one two we'll get no i should probably have made that x i probably confused you let's make that y and i'll talk about global and logoscope right now and you see we still didn't know because this is being returned we can even go and return y and once y is returned we will be getting the output we want because now this can actually store something in this but take note once your turn has been thrown you cannot do anything after return so if we say hello then you'll actually see it gives us a rich quickly line because this nothing's expected after that so even if you're trying to run this we'll get an error because it was expected that we would find n but we found print take that return ends the function once you put it there you can't put anything after it now let's talk about a local and global scope whilst we're here so we did talk a lot about local scope and global scope and the difference between them is like it's right here so this local y it cannot be accessed outside of this function so if i were to just comment whoops i'll just comment that out and say y so even though y is here and even if we were to actually do this whole something so even though y is there we'll still get nil because y is a local scope so we cannot access it outside of this function but if we do this or just go underscore g dot then that will make it a global scope variable if you didn't have to put on scroll g there i'll just do it because it's considered good practice we're there and we can get five because now there's a global scope this can be accessed from anywhere so this is why we have the local and global scope thing so we can't access anything inside of it other file or function unless we want to so keep everything local scope as far as you possibly could so that this would happen where you can access what's inside of here outside here but take note you can access what is outside of the function inside of the function so if i were to go here and say local whoops local z is equal to 10 and i want to go here and just print out sum and let's go to three again but this time we're not going to use this parameter right here and i'll just say y is equal to one plus z then we that would actually be a valid function because z is outside this function so this function can access it but whatever's inside of this function can't be accessed outside of the function if you give what i mean because this here is like one big scoping so this file itself is a scope it might be getting a bit confusing but if i show you we get 12 because c is here so even if this was global scope it would still be accessible here as you can see here the only difference is that whatever is outside of this function can be accessed inside of the function but what is inside the function cannot be accessed outside of it so that is the meaning between local and global scope and we'll get more into this as time goes on there's more than one way to define a function for example we can create a local variable call it maybe um add 10 and this can be equal to a function and this function let's say takes in a number and here we can even return something or execute or what not so i'm going to just say return to plus num or actually it should be 10 because we're saying at 10 here so 10 plus 9. and here we can say print add 10 and then we can go five and that should add up to 15 as you can see there so there's another way you can create a function it's up to you how you prefer to do it you can also get multiple values returned in a function for example we can take a number and then outcome so here i'm going to say the outcome actually shouldn't take an outcome my bad just take it let's just take a number and you're going to make this local scope and here you can see that is equal to 10 plus number and here we can say we give the outcome right and this will give us what we expect but we can actually turn something extra so we can return let's say the number that was passed in the number and then the outcome as well so now here i'm just going to create a variable to put it in i'm going to say local and stored and output is equal to add 10 and i'm going to say 20 for example now here instead of print i can actually go here and say print out store and here we can add some extra strings to it uh head 10 added to it and here i can just say output say this and if you run this and you can say 20 had 10 added to it and we get 30. so we can return multiple values here by just separating the outputs by comma and then if we can store it by creating our variables like this if you're not planning on using one of these you can make this let's say an underscore and that basically says we're not planning on using it so it's just common convention to do that and if we run it now we don't even have to worry that our score does not need to be used next part we're going to be talking about recursion recursion is basically when a function calls itself until it reaches what the answer it wants for example here we can create a function maybe let's say a counter so local function counter and this counter can take in a number and then the number should end with and um i'm going to give that an underscore so it takes a number and the number you must end with now i'm going to create a local variable called count and this count will be equal to number passed in plus one and then we can just return count for right now and i'll continue with what it actually does in a second now we're gonna say print counter and let's say 10 to 15. so we want to start retaining we'll want to end at 15. run it we get a limit so it's a normal function that just adds one cool now here we can actually say if count is less than the end num then we want to do something so then and here we can just say return and we can call this function again in itself counter and we can pass in the number we want to count so in this scenario count and then how much we want to count it up with so end number and now if we were to print that out we get 15. so it counted all the way until 15 and we can actually put a print statement here as well so print and we can just say count run that and you can see we get 11 12 13 14 15. so what this basically did is we said okay create a local variable called count and that's the number plus one right then we check is this less than the number we want to end with if it is we're going to print count and if we just print out the count so the number it is in this case it would be 11 as you can see there and then we return this value so we are calling it again so it's going okay whoop now this number here is this value here so this was 11 now and they were saying okay 11 plus 1 that's 12. is it less than 15 yes it is print it out and it just continues like that until it finally reaches let's say 14 and you get okay increment 14 here and that's 15. okay and then is 15 less than 15 which is right here that's the number and it's okay you know it isn't so it's going to skip this part it's going to return the actual value so that's a recursive function it just calls itself over and over until it meets its condition so next i will be talking about anonymous functions so we'll yet again recreating counter but this time with an anonymous function instead and this counter will be a little bit different so here we can create again create another account variable there we can just make it equal to zero and this counter doesn't have to take in any value whatsoever and we can just for right now return count now if we were to print out count here then we get zero and we try again we get zero the whole time perfect now we want to whenever we call this counter we want to get that value so or whatever we call this function we want to increment that zero and that's where anonymous functions can come in handy anonymous functions are basically functions without a name so this function here has the name counter an anonymous function will not have a name so here we can just say return and actually you can just use the return that's already given to us okay so here you can see maybe click there return and that returns actually all right there we can return a function and this function doesn't need a name so i'm just going to go there it doesn't need anything to put into it and we can say count is equal to count so once we're incrementing count and then we return count so basically we are returning a function that will take this count right here that zero and add one to it and then we'll return that value so if we were to just go like this and as you can see we actually get a function here we don't really want that function location so we're going to say local and you guys say local x is equal to counter and this is a way to get around it because remember it's returning a function and that function is anonymous it will just return this that location we don't want the location we want this right here and putting it inside a variable is a way to get around that right there and we can just call the x because this is returning a function so we're getting a function back and stored inside of x so this x is this function right here now if we say execute that function then it's going to take this count and it's going to return the count that is why this x is also a function right now might be a little confusing but if we go like that we get one and now if we do this we get a counter from one to seven so that's pretty cool because this x has just once initialized discount zero that's what happened here so it initialized it and it got returned to this function so now x itself is a function and now we're calling the function that x is so this one right here and we're saying okay execute it and then it already knows what count is because it stored it here and now it has a memory of it and the saying count is now equal to count plus one it becomes one and returns one but count has stayed one so now we see when we call it again count is one because we stored it and then count plus one and it goes back here and now we have two and that is an anonymous function at all it's glory all right so i know it's becoming a little bit of a long video so what i'm going to do is just give you one more thing to look at so i'm going to create another function and this function will be called sum once again and here let's just say x and y for right now so x and y and here you can return x plus y and in here we can say print sum 10 and 5 and that will give us 15 right 15. but sometimes you'd want to take an infinite amount of arguments you don't know how many arguments should be passed in and the way you can do that is you can say here dot dot and this dot dot basically becomes a table and that table gives us what we want so if i say uh we can just actually just we're just going to for right now return 6 or something and we're going to just print out that dot dot we get returned as you can see there we get ten and five because ten and five have been past ten so if you go here and add nine and maybe zero and fourteen just save that and go here and run it again as you can see it just gets added to it so it basically returns a form of a table but not really quite a table but it's relatively similar so let's create a variable here and just call it sum so local sum i'm just going to put some so it's not the same as the function name here it's going to be 0 once everything starts and then i want to loop through this variable right there or through this for yeah through that we want to loop through that and here we're going to use pairs i'm just going to say pairs whoops before and and here actually got completed so pears is a way to also loop through tables so here we can as it is have a key a value and here we can pass in a table but this as i said before is not quite a table it's more of a string as you can see here we don't get return to table we get this which is basically a string it's just a bunch of numbers that's been added so it's not really a table so what we'll do is we'll just go like that and that will convert it into a table because we're putting it in table thingies right here now let's first print out what we get so let's print out key just so we can see what is key we want to six the key is the index so this would be index one index two index three four five and then the value would be the value associated with that index so here we can just run again and as you can see we get ten to four but and this six right here you don't have to worry about it it's actually just being printed out here so that's six so we can actually just change that to print and just so we don't get confused again because for a second that i wasn't sure what was going on but yeah if we run it we get one ten two five three nine four zero and five fourteen just as we put him in here so index one is ten index two is five index three is nine and here's printed and that's what we're printing out there for a second i did get confused because i wasn't sure why there was an extra value there anyhow and if we add more here it will automatically allocate these there so boom as you can see there now we don't really want the key so if you want to you can even make this in underscore because we're not going to use the key i'm just going to keep this key because i really care and here with sum i'm going to say sum is equal to sum plus value so we're saying i get this or the sums not sum sums is equal to sums plus value so it's going to take the zero and it's going to add the first value that will make it 10. then sums will be 10. so then 10 is equal to 10 plus and then 5 and it will make it 15. and on the end of the day if we print out sums we'll get this so it's just basically the sum of whatever in here so if i say oops 10 5 and 10 that should give us 25 as you can see right there so this is just to take in an infinite amount of arguments as you can see here and you can use this right here to transform it into a table and we can use this for loop to loop through that table and yeah there we go if you wanted you could have used a normal for loop so just four and i becomes like oops i becomes like whoops i just one to the length of and then you just do that and that will be perfectly fine but this pair here also gives you the key so you can use that if you want to as well so yeah that is functions i hope you all understood if you had any questions feel free to ask in the comment section below and i'll do my best to help out today we'll be looking at co-routines if you're not sure what i'm talking about in this introduction section you don't have to worry about it you can still follow along without a problem anyhow so and code routine is almost like in a sink await function where you basically have a function that can execute and it can actually stop mid process to let a different function complete before it continues a curating instead can wait before finishing and then let a different function run and let's say that's also occurring that that can wait before finishing and did a different function on until that has finished and you cannot finish them afterwards and it sounds a little bit confusing but it will be a lot easier if i just showed you what we're doing so i'm going to create a global or let's just create a local function this can be a routine called one and this will be equal to co routine dot create and this will create a co routine and as you can see here it says it takes in a function so basically in here you have two things you can do you can either create your own anonymous function and put it in here or you can create a function put it in a variable and then stick it in here we'll do both just to show you the differences anyhow so we can say function and in this function we can just create a basic for loop and let's say this for loop will return the values 1 up until 10 and it will print out the value so it's just a basic for loop there's nothing special about it now here is where the special part comes in so we say if i is equal and let's say five so right in the middle then then we want to actually stop the co routine from finishing we wanted to wait we can then say coroutine dot wait don't wait no that should be corrupting.yield and this will yield the current it will make it wait before continuing and you'll actually see what it does in a second let's just actually code it first so we know what's going on next up we can create another routine and this can be called routine two and here i'm just going to create a function just to show you the two different ways you can do it you can't create a normal function but i'm just going to store the functions of a variable just because why not and we can call this routine func so for routine function and that can be equal to a function whoops and here we can say four and then we can say i is equal to and let's say we wanted to continue counting so we can make that 11 all the way until 20 and here we can say print and this is routine 2 so i'm just going to say routine 2 and then we can just say here i and you can actually make that routine 1 just so we can easily see the difference between them so instead of just that we can say that and you can just make that routine one now routine two we can make that equal to co routine books co routine and dot create and since it takes a function we can just say routine func and there we go that should be all we need now down here we can actually get the status of routine so before we actually execute a routine let's get the status of it so print co routine dot status and here we can like say let's say we want routine status of one and here it actually gives you a nice what status is envious code so here we can see we have running and this is just if the program is currently running is that corrupting running is it busy working then you'll get this suspended is if the pro or is if suspended is if the co routine is not running so if it's kind of stopped or it just hasn't started yet then you'll get suspended is if it's active but not actually running and date is basically has completed or stopped with an error so you can actually go to the documents here so you can just review documents i'm not going to be doing that but you can if you want to know more about this if we save this and we just lure our main file here and then as you can see we actually we actually to run we should probably just do that and then we can do this my bad and yeah as you can see we get i just don't add the curly brake brackets here at the end it should be without it so yeah let me get suspended because this hasn't even started yet but what we can do is we can say curoutine dot regime and this will start or resume a current so it can do both and here we can just pass into routine so i'm going to say routine one and let's see what we get we get routine one and it only goes up to five even though we said it should go up to ten whoops uh again then this okay so even though we said it should go up to 10 we get five because we yielded it it's actually waiting for another one to execute but it still says suspended here that's because it's not running if it's not running then it's suspended but what we can do is we can then go like this and we can copy this so what we're saying is take this first co routine and start it and then we're printing out the status of it after it has gone up until i equal five then you're saying okay resume it continue with it even though we said yield and then we're printing out a status so let's see i now get suspended and it continues and you get bit because it has completed it doesn't need to continue anymore so this is one example of how you can do it if you want to actually see the action as running then we can do is we can take this print right here and i'm going to put it right here right underneath this it can't actually access itself so the way we can get this to access itself is to just make this global now if we were to run this so as you can see we get the running status after each one until it says dead so that's how you can just check if it's running so i'm just going to go back to our previous example here again here we go now the reason why keratins can actually be quite useful is let's say we we take this and we just create a copy of that line and we make that two now look what happens i'm just going to remove this that status right there so look what happens if we just clear that and run it we get routine one up until five and then we get routine two actually going to until 20 and then routine one is continuing this is why it's useful because you can actually stop the routine in its tracks and let another routine run you can think of it as a loading bar let's say you're installing a game and you're seeing that loading bar right there and let's say routine one is the main routine and you can make that yield until and let's say it extracts the file and that can be routine too once it has extracted a file your loading bar would go a little bit up and then let's say now it has to put that extractor data into its folder location i know this is sound like gibberish but we could actually take a good look at this by if we were to open up my file manager here and just copy a large file so i'm just going to copy a big anime file and a big one should probably be probably be a demonstrator we can then create a new document or go into documents and just paste it here and it we should actually see a little and here we go you can imagine this is a cool routine for each file it has copied over each megabyte it has copied the curating gets yielded and this moves up you can imagine it like that so yeah that is one way to imagine it it may be a little confusing but if you actually work with it it does become less confusing anyhow before we finish it off one thing we can do is also check if a cure routine is currently in a specific state for example let's say we don't actually know if curating one has completed what we can do is we can go here and say if coroutine dot status and we just put in the status of this routine or we didn't appreciate we just put in the routine so routine one if that is equal to and it actually completed the information i'm going to say suspended then so if it is suspended should we try to resume that code routine like that let's see what happens and there we go that is all for today it's just about curating and how you can use them to basically stop the function in its tracks and then run a different function whilst that first function is still waiting and once the first function has completed waiting you can make it run again and you can check its statuses and use that to your advantage and that is co routines today we'll be learning how to work with files so reading files and writing to them so there are two ways we can do both of these and i'm going to start off with the most simplest way possible the most straightforward is to just you do and it's done type of way so currently if i were to go here these are all the files in my current folder i don't know i should probably open up a file manager so you can see them i'll go to documents and i'll go to my trash folder into my lua folder and there you go so that's all there is inside of here just a lua file just wonderful so let's create an file so to do that the most basic ways to just go i o dot output [Applause] and this basic means input output and in dot output this is going to create a file and if the file already exists it's going to empty the file so we'll see what that means in a second so let's go here and say my file.txt right so this will also select the file so if you are selecting a file or if you are working with files and this one will also select the file for you so you can work with that file that is why the following piece of code io dot write will work without a problem because we have selected that file with io.output we have said we want to output to that file here we can then say something like hello or hello world right and then i o dot clone and this will close the file it's highly recommended that you do close files after you work it with them because closing not creating a file could cause some leaks in your program which you don't usually want so maybe you're trying to write to a file again and you forgot to close it you might get an error trying to write to that file because the file was never closed anyhow we can save that and now if we were to run this so lua and then main.lua then as you can see here is in my file.txt and if i bring this up my file.txt so this is just everything that's inside of this folder right here so i'm going to close that we can then open this and inside of it there is a hello world pretty cool now to show you what i mean by it will over it will empty the file is if i were to go here and add let's say hello world for exclamation mark this time save it run the program now if i open up this file the previous hello world is gone but this time there's a new hello world with an exclamation mark so whatever you put in here in here will overwrite the file so you should only use this if you're creating a new file or if you're emptying a file maybe you're you have a specific save file quickly io output i write io close done so that is when you would use that now let's say we want to read from the file reading from the file is rather simple so we can keep this iodide closed here and we can just say io dot input this time and this will get input from the file so it will read the file in this case myfile.txt right there and then io dot read and this will read the file and we can just save that instead of a variable so local file is equal to that and here we don't put text here there's something specially compared in here so there are a few things let's start with a number so let's say we put a number in here for example five this will read five characters so i'm just going to go here and say print file so you can actually see what's happening so it only read five characters so in the first five characters which would be hello and in this case it says hello world so if we were to say only two characters save it and now we're every two characters if we were to say a hundred characters and we knew it doesn't have that many characters because it's just hello world it'll just read everything then you don't have to worry about that too much so that's the first thing you can do the next thing you can do is you can go here and say in quotation marks star and then number and this will read a number currently we don't have a number in here so i'm just quickly going to put an 88 in here save and then go here save run it and we get 88. so we try to say file plus 99. let's see if that will do anything and as you can see because we read it as a number it went 1 8 it went up to 187 because we added 99 to it so as to specifically read it as a number you could also do it by putting a number in here and that will also allow a number read so it will still give you 187.00 because remember luwa doesn't really work with type so it will automatically guess what it is but with in this case for example if we were to say is there at the end now if we were to run this then we'll get an error and if we were to say here number instead then it will still work because it will only read the number it will not read that is so that is why number can be very useful so if there is any under text in there you can just use number anyhow so let's put hello world back in here and in here we can just say star line and this will read a line so if i were to just remove that at the end and run this file then we get hello world if we were to add another line here and we can make this hello jack this time if we were to then run this they will get hello world if you were to do this and let's actually just put either read in here i believe that would be the easiest way to go about this i don't have to do that i can just do this and i can comment that out and i can say cb let's see what will happen then first it reads hello world and then retailer jack so by saying eye to read and in star line this will move down the line after it has written that line so if we were to do this again we didn't have another line to be read so we'll receive nil so yeah that is how you can read multiple lines at once so we can just remove these and we can actually just go back to our original setup here cool and then last but not least we also have all and this will just read the entire file it wouldn't care about lines or numbers you just read the entire file and return it to us so that's the most basic version of reading and writing to a file so we can stay with this and we don't need that iron that input anymore so we can remove that now this local file we are going to say io dot open instead so this will open the file then we need to specify the file so in this case my file and they want to specify the action we want to use on it in this case we can let's say w so this will mean right as you can see here there is read mode right mode append mode and we can probably move down yes w plus update mode all previous status erased and you can just go read through all of these i'm just going to cover the most basic so in this case that will be right mode and remember to always have your clothes here but in this case we can't use our clothes anymore we can use file colon close and this will close the file so that's our new way of doing things it's file and then colon now in this case we want to say and here file go on right and this will write to the file my name is netsu save it and if we were to run this it will write to that file and i forgot to put it.txt here so it created a file as well so we can just delete that file so if the file doesn't exist it will create it as well save it and now if you do this it will have rewritten this and now it's my name is netsu so we'll overwrite it just keep that as note yeah so that's how you can write to a file next up if you want to append to a file you can say a so if let's just rewrite this file okay so now it says uh my name is netsu and let's just do that cool now if you want to append to the file you can use a and i will painted the text so it won't re it would overwrite the file like up until now we can only overwrite the file we can't add to the file so this will allow us to add to the file and in this case let's say i'm going to add a backslash n so it can go to the new line to the next line because currently if i add it it will add to the back here i wanted to add to the bottom of this so i'm going to create a new line and i'm going to say jack he is old and let's put a little dialogue here and here we can say netsu yeah i know cool save that if we were to run this file then now we'll see this my name is nitin and jack he is old let's see yeah i know and we can actually if we were to run this again if we were to run that level again as you can see it gets added again you'll do it again it just gets added to the bottom so a is for appending and w is for writing so overwrite it if it's w it won't overwrite it if it's a so it's about pinned next we have r for read so this will read the file in this case we don't use file dot folder right here we use file dot read and we don't have to put anything in here that like this so it works the same as before we have our same arguments we have our star all for all we have our line for a line we have our number for a number it's up to you which you use depending on the scenario you might do something else anyhow i'm going to say star and just all because i want to get everything and we do stores in a variable local red is let's just go red so it doesn't mess with anything and here after file has been closed you can do this before the file has been closed i just do it after because why not do that if we run now we get everything that is out of it only once a line then of course you can say line here now if you were to run it now you get only one line today we'll be covering the os module the operating system module now the os module allows us to work with the operating system as it kind of suggests now some of the things that operating system can provide us is maybe the time or code it takes to execute or maybe the time since 1970 it will also allow us to get environment variables which will be getting to what that is and execute commands from the terminal but not from the terminal from lua remove and rename files and stuff like that i will be covering everything however if you do want to know more about the os module because it is an amazing module in any programming language then you can probably just go check up for the luas documentation and you should be able to find quite a lot of interesting things you can do with it perfect now let's get started so first let's try and just get some time so let's print os.time and this will return the time since 1970 so all the seconds that has passed since then as you can see there that's how many seconds has passed since then up until now every second we execute it it will change we can also specify a specific date so from 1970 up until a specific date so let's say here or let's break it up into pieces here just to make it easier here and we can make year equal to 2 000 and we can make the month equal to let's say the 10th month we can make the day equal to the first you can stop here if you don't want to continue but if you want to then you can even say the hour and then let's make that like 1 p.m and you can say the minutes and let's make that equal to let's say 20 minutes and in the amount of seconds and let's say that should be 10. save that so now it is from 1970 up until the year 2000 or whatever you want to go here with that date so yeah that's about the amount of time that has passed since then up until now in terms of seconds of course you can make it more or less specific should i rather say by doing something like divided by let's say 60 and that i believe should give you the minutes and you're good now you have the minutes because there is 60 seconds in a minute now let's say you have let's put this in a variable so let's go local local yeah past or something good edits out there now let's say you want to work out the time since this past time here up until now one way to do it is let's just go print here and say oh is the time minus past there's one way to do it as you can see here but there's also another way to do this and it's built into us we can just say dot death time and you can pass in os dot time which is right now and past and it's built into lua and it will just give you the same or not the same result because seconds has passed since then but it will give you the same sort of output but it's built exactly for that equation cool now let's say you want to get the current date so you can just say os date as easy as that and you can get the current date friday july 23rd it is 7 p.m and it's 20 21. pretty cool so that is how you can get the date pretty simple now next up we can get to the environment so this can be things like your environment variables on your computer for example and i believe most of this should be the same on windows but i can't really confirm that since i don't use windows and same on mac anyhow so dollar home and if we can just echo home now this is an environment variable it specifies where my home folder is when i open up my file manager as you can see here it opens up in it but if i do this you can see there's that which is just a slash it's root home netsu if i go here as you can see it's home netsu so you can use this dollar home to know where the user's home folder is which as you can see here there's mine same with if you use echo ended user and this will echo the user that's using this pc as you can see they're netsu if i say who am i it will say i'm net because that's my username on this computer now to use these environment variables or to get them in lua you can just let's go print here and of course you can store it inside of a variable if you want and just say os.get and here we can just say what environment variable we want to get for example if you want home here we can just say home save that and if i were to run this lua file i see we get the same result as up here if we want to get user then we'll get the same result as with that what we got here i didn't know too many environment variables so unfortunately i can't show you more environment variables but you could probably go search up environment variables for your system next up we have a way to rename and remove files so let me open up my file manager in this folder as you can see here let's say i want to create a new file and let's call it myfile.txt give okay anything i'm just gonna connect my file.txt just for simplicity now here i want to say at least i want to rename the file i can just say os.rename i can see which file should be renamed in this case my file the txt should be renamed so we can say myfile.txt and to what it should be renamed so let's say new name dot js so we change the extension everything save that and if we run this lua file then now you get new name.js instead of new file.txt so we renamed that file so that's one way to work with files another way we can is we can remove it this is a free name we can say remove and we can just pass in the first one here new name.js run this and now it is gone it's no longer here because we deleted it let's do a fun experiment let's let's say main.lua because i i just want to see what's going to happen if we delete the file we're calling this command from uh okay so nothing interesting happened it seems yeah nothing interesting happened unfortunately but it would have been kind of cool okay now we have another main.lua here and yeah it's empty because we deleted the previous one but as i can remove a file and rename it now what i'm about to show you should never ever ever actually be used unless you have no other choice it is executing commands from inside of here but executing terminal commands so os dot execute and now we can execute commands you could have executed here so i say who am i and we already know we can just get the environment variable user but if i say that then we get netsu because that is if i say who am i so this directly pastes this piece of code into the terminal now there are two reasons why this right here is bad practice first reason is if you type your code wrong in here instead of just using something that's built in for you already then you could possibly destroy your entire system for example if i were to say rm dash rf slash if i were to execute this inside of here currently when do you do anything because it will need root permissions because protection from linux but if i were to execute this here instead of here then my old system can be destroyed immediately like no kidding it can boop it's gone so that is why this is so dangerous because let's say i just wanted to remove a file let's say slash tnp because if i remove that my system won't break let's say i wanted to do that but i forgot to add that that is one reason because it's very very dangerous the other reason is something you execute here won't execute on a different platform per se see how this this is how you clear the terminal in windows so clearing terminal so what i've been doing like this that's how you clear the terminal or although on linux it is clear so you can just clear or you can press control and l but on windows it is cls you'll notice that i didn't have that command and on windows you don't have the clear command because you have cls so if we were to execute this lua file as you can see it can't find that command it doesn't know what it is but if i were to say clear then it will clear my terminal so this is another reason because one thing type here won't work on another system and you might be telling yourself that okay i didn't have to worry about that because i'm just going to code for windows or i'm just going to code for linux or just going to code for mac problem is if you ever in the future decide okay no i want to code for x platform as well and not just this platform right here then you have to go everywhere where you said execute and you have to find a workaround because you didn't do it before so just never ever ever ever use this unless you have to and then let's say we want to time our code it is fairly simple up here we can say local start this will be our start time and we can just make that equal to os.clock and this will start a time rule or we will kind of like save a snapshot of when this happened then let's say i do a for loop here and here i can just maybe say uh i don't have to do anything i just can just maybe go like local x is equal to 10 and then i can just end okay and in here i can just say print and then we can say os.clock minus start and this will give us the amount of time that has passed since this up until this so it will count or calculate how long this takes this is great if you want to time your code so if we do that as you can see we get this this isn't seven seconds or seven minutes it's actually a really really really small value you'll see if we were to bring this up to let's say this amount which is what i believe 1 million so if we run this forward 1 million times as you can see it took 0.02 milliseconds if we add another zero or another two zeros here let's see how long it takes now it took 2.29 seconds to execute so yeah that is how you can time your codes and boom boom and the next one is if you want to exit the program os dot exit and this is if you don't care you can just say os dot exit so let's say here you say you create a for loop so four and you can say if i is equal to five then and then we can just os dot exit end and here you can just also end and here give me block print and we can print i there's one way to do it so once it reaches this value right there it will exit the program so boom now you see up when once it got to five it's exiting the program it doesn't continue if we were to uncomment that or if we were to comment commentator uncommented they will go up until 10. and that is basics of the os module there's a lot more you can do and i do recommend you do go search up more of it if you are interested in learning more about the os module because it is a very useful module today we will be covering what a module is we'll be creating our own module and if you're like wait module then it is basically what i've been talking about for like a while i talked about the math module the os module for example os dot exit so here we go this right here gets the exit function from this os module that is what it's basically doing and here you can see a bunch of things we can get from rose module so yeah this is the os module the same with map if we go math dot and let's say abs for absolute i mean you are getting apps from the math module so let's create our own module so we can actually get a deeper dive in what's going on in here so let's create our own mav module one of just basic functions but it can do what we need so here's my lua file it is i can open up my uh folder here here we go that's my lua file about to create a new document and let's let's create a custom module here so i'm going to say mymath.lua whatever you call it it's up to you save that and now as you can see we have a mymath.lua file so let's go in here and before we continue let me just tell you a module is basically a lua file that returns a single table when called this sounds maybe like mumbo jumbo to you it's just a table it just returns things that's inside of a table so we're just returning a table to the user you can also do it better with oop and functions and i will probably show you how to do that in the next video where we do talk about oop but even if i don't you should be able to kind of figure it out even on your own but we'll be working with tables for today just to keep everything simple and a package is a collection of modules so if you have like let's say five modules then that those five modules together is called a package all right so let's start here now we're going to call this math so it's going to have two m's and then f just for math so and that is going to be our table and we're going to set that to an empty table because we want to return a table on the end of the day then here we can return math there we go so this is our module the most basic version of module it can't actually do anything but now we have a module and this return here at the end this works perfectly fine because this file right here is now a module so it will return this when we call it and i'll show you an example of this in a second so here we can say maybe a function and let's call this function add to add two things together let's say x and y so you're going to add two values together x and y we return x plus y thing is this is not part of the module to make this part of the module you'll have to do this you have to copy that but you don't have to copy you can retype it if you want you just have to say dot and this just adds that function it would have been perfectly fine as well if you would you just go here and just i guess paste it in there as well i guess this would have been perfectly fine in a sense and just you know make it more table friendly so you know do that and just say that is a function [Music] and whatnot just how you usually do it i just find it kind of tedious so i like to do this just to make it look a bit cleaner and easier to read now take note that this has to be a global function some of you might want to grind your teeth because you can't say local here at the front so local but if this is local then if we return this in math then we cannot use it because it is only local to this file so local functions can only use identifiers as name which means we cannot use local here and in here with this you can make that local it's there's no problem with it although it is what gets returned so this right there that return at math that we can keep that global because anyway is going to be returned to outside files so there's no reason in making it a local only item because if it's local only and we return it to an outside file anyways then there's no point in making it local because it's going to be used globally anyways anyhow let's stop talking and actually use this now we want to import this unlike with os which you can just say os and then dot what we want to do here we actually have to import it so we can say local and this you can make local this doesn't matter because it's going to be local to this file and i'm going to call this mf just because i want you don't have to call it math you can close whatever you want in fact it's called mod just just to make it easy and we can say require and here's the module name and this module right there it expects a lua file so it expects a dot lua so we can just say my mac and that will import this module from the file my math and it's our own custom module the reason we have to import it is because it's our own custom one if it wasn't our own custom one and it was provided by by lua itself we didn't have to but this is also what you have to do if you maybe use a package manager for lua but that's i've just covered this video let's continue now let's actually try and use it so let's say print and we can say mod dot add and as you can see it even auto completed so mod dot and as you can see add and here it says what it does it takes a number x a number y and then here it says what it does it also returns a number so we can say mod dot add and here let's say five and ten save that and now whoa i almost rebooted my system there wow louis and this should be lua now we only need to execute main.lua here because we are running main.lua and main.lua is running mymath.lua that's why we only need to execute main.lua here run that and we get 15 because our add function right here worked perfect li so just so we can get the hang of it let's create another function here so one of the more common functions you can also find in math libraries so math and it's the times function you can times usually with it or even power let's do power power and we can take in num1 because you don't need to say x or y that can be whatever you want and here we can just return num1 times or to the power of num2 so this carriage symbol right there just basically means make them one to the power of num2 let's save that and use it here as well also if you want to get rid of that blue line there you can just say underscore g dot and whoops and that will make sure that lua knows this should be global but it's optional now here let's try and do something else let's go and and we can't keep that as well we can use the this module anywhere any amount of times we want so now if we say dot this and as you can see we get power now we go here as you can see this it just gives us the same thing so it's very very nice let's say 2 to the power of 5. save that and let's see what we get we get 32 because here we go here so that definitely works that is how you work with modules you can add more modules here and bring them all in one module can even require a different module as well just like how i might want to use os dot exit somewhere here you can also make create your own module and just use it in area as well but yeah that is about it for modules so now you also have a little bit of a back side on how os dot exit works or um what other math things math.math.abs or seal you know know what's going on behind the scenes today we will be learning about oop so what exactly is oop oop is object oriented programming up until this point we have been working on procedural programming or functional programming where basically we have a bunch of functions and those functions create our application for example if we had a function and we called it main and inside of this main we did everything our application will do that is considered functional programming because we will have multiple functions and all of these functions will do something so do and we need to call this something and here we can call do and something what not and that is functional programming where you have a function and that function executes other functions and it's just a bunch of functions to create your program that's what we've been doing up until now now we will be learning about object oriented programming and it is just a way of thinking because let's say you're developing a game now you could do procedural programming and game development there's nothing wrong with it but when you create a game the easiest way to think of that game is as an object-oriented programming way where everything in that game is an object that trash can that the player walks by that's an object the player itself is an object the bike the player rides on to get to their hood it's an object everything in the game the houses the streets everything that's an object and that is what we are thinking of when you think of object-oriented programming object-oriented programming isn't just useful in games oop object programming is also useful in real life situations where you can have a bunch of objects to create an application instead of using functions it's up to you which you prefer doing i usually switch between the two depending on a project so now that we've got that out of the way we also need to get out of the way that lua is not an object oriented programming python c plus plus java c sharp these are object oriented programming languages they are meant to be used in an object-oriented way you create objects classes to make things work lua however is just like c it doesn't have objects so to create objects you have to simulate it and because of this that means there's also a lot of ways you can simulate objects i'm only going to show you one way of doing it because this is the easiest way to do it and to be honest i can't understand any of the others because my brain not big enough but let's get started so if you don't understand what object oriented programming is you still have a little bit of a foggy mind i believe if we go into the code right now it should be able to help you get around to understanding it so to simulate object under programming we are going to use functions functions in order to simulate it those functions will return a table and that table is what's going to help us create the objects because with a table we can create basic objects for example if i go and create a local table then this table can have an object that has like the name inside of it because a jack and it can have another object and this can have the age and the let's say friends you can make that something like 18 and the friends we can make that another list and that just keeps the names of his friends union has one friend so we can call him jack i mean freddie we can call his friend fred there we go this itself can be considered an object if we use it right because if we go to print then you can use t dot name as an example to get the name of this object so if i say lua and i run my file then we get jack so we can use this to our advantage to create oop so let's create a function i'm going to call it a local function and it's going to be called pit because we're going to simulate a pit this pit will take in a name because we need to give that pit a name it's optional it doesn't need to take in data but it would be nice if we can create a pit and that page let's say we can call it ginger and this should be a function to a function and it will be pit and then here we can just say end now what we can do is we can return a table and what's ever inside of this table is part of the object that means we can put functions in here we can put normal just like names and ages in here so for example let's say name and this name can be equal to the name we give it so we give it a name on initialization we didn't need that for let's actually for right now let's just do that and call our cat or our dog or whatever we can call it louis now here after that we can maybe give it a a status like how is it feeling right now in this case we can say it's hungry by default because you need to feed it and we can use this now as an object so down here we can say local cat is equal to pet we have created an object and this cat right here basically is part of the object so now here we can say cat dot name and we can get the cat's name so if we were to wrap this inside of these and we would say print i'm just going to remove everything up here because we don't need it anymore then we get louis because the cat's name is lewis now the reason i want to put a name in here is because they can give your cat a special name so here we can instead of saying louis we can say name if you wanted to you could say name is equal to name or lewis which means if they didn't enter name is going to default to luis and here we can just maybe say kitty and then we can create a dog just for the heck of it and say dog and dog's name can be nothing that will be the default name given then here we can have a name for the cat and the dog so we can print out both of them now if we save and run this you can see we get kitty for cat and lewis for dog and that's object-oriented program these two are two different objects they they basically use the same object this pet object to create their own little object in fact we could have used a car in here and say this is a car and it can be and these are the parts inside of it and you can give the tires a specific type and whatnot but that's too complicated so i'm going to stick with the simple stuff here and now we can modify both of these and they will not be taken into modification like if we were to change the name of cat here it will not change dog and we'll get to that in a second now as good practice we also capitalize the first layer of the function this is just so we can actually know this is an object so if you want to you can even go patent at something else and you can use camel case where the first letter is capital and not lower case but yeah now let's say we want to be able to feed our pets now to do that we can create another thing and it's not necessary to add an extra space here as i'm doing right now but i just like to do it so i can keep my functions and my these out of the way and feed can be equal to a function that takes in itself and then we can just say end and in here we can write something that should happen now this self right here that allows us to access things inside of this table so if i want to access this name then inside of this table i can just say self.name this case we want to change the status so i'm going to say self dot status and this will allow us to use the status if we don't have self here we might get errors but then we can say is instead of hungry you can say full because they have been fed you can just make that a capital h as well then so now let's just remove dog here and we can say and we can also remove that name there then you can print cat dot status this will print what their status is we're gonna put three things here and the second one is where we're going to feed the cat so we're going to say cat.feed and here we can show the cat status again so let's see and my bad that should not be dot that should be colon to access that feed now if we run it we get hungry and then we get full because the cat was fed and now we can even maybe add extra details so we know the cat is being fit so again here we can print and maybe want to put a name here name is fed now whenever we want to run it we'll see kitty is fit and the nice thing here is if i were to create another object here let's make a dog again and we don't have to give it a value here we can just keep it default if i want to show the dog status so dog eight would still be hungry because cat and dog are their own objects so if you run this as you can see right here it's still hungry because dog is not cat so whatever happens to cat here does not happen to dog and that is the power of object-oriented programming you can have multiple objects but with different values so yeah that's one way to do it now i'm going to show you how to do it in another way and this is going to be inheritance sometimes you want sub classes for your classes classes that inherit of something else like let's say you have a dog class you want then you can inherit from a different object so here i can maybe say and create a local function here and this can be called dog now dog we might want to take in the name and maybe even the breed of the dog inside of this we can say local dog is equal to pet and we can give it a name what we are doing here is called inheritance are taking this dog and we are making it this right here that is what's happening here and later on we can just call dog the local l is equal to dog just so we can have it here and call it maybe doggy and it's a poodle or something okay so we are inheriting from this pit we are making this dog equal to this pit value right here now we can create our own little dog object that's part of the pit object by now saying something for example like dog dot breed so we are adding to this we're adding to this table right there so dog dot breed and here we can pass in the breed and that should be equals because it's not a function it's a value i can do this like with a bunch of things we can even say loyalty because we all know dogs are pretty loyal and it starts by zero because if you just get a dog who's to say that dog's going to be loyal and now we can say dog dot is loyal and we make that equal to a function that takes itself because we need to access this loyalty right there and we can return whoops return self.loyalty and bigger than or equal to 10. so this just returns true or false so if the loyalty is more or equal to 10 then a dog will be loyal if it's less than 10 then dog will not be loyal also it doesn't have to be camelcase you can do that as well i'm going to use camelcase for right now and you can even maybe go dog dot bark dog.bark can also be a function i mean this function is executed we need we can also test take itself it's just you don't need to take itself here but it is recommended just so you always know this is part of the self it's part of this right here it's just optional you don't need it here in this scenario but it's good practice to get it there now you can say print and you can say with wolf now it will bark so here if we want to go uh lassie for example then now we can call the dog lassie and that was more specific object because this object is specifically a dog and now we can specify here more what it is now here we can print the dog i mean lassie dot and let's say breed we can pass that in so now we can get the breed of the dog so we go here my bed i was supposed to go here and say return dog because remember it still needs to return this table right here because we created a table we modified that table now we need to return that table just like we did here but now it's just putting some of the variable first and then it's passed in so we didn't even need to do this we could have just created a table here but then inheriting would have been a little bit more difficult anyhow so we can save this and then we can try to run it and we get a poodle because it's of breed poodle now next up let's say we want to make our dog bark so you can say lassie dot park actually should get colon bark and there we go if we want to access a function we'll need to use colons we want to access a variable then we can use a dot do that and now we get with now let's go here we can maybe even go if lassie is loyal or actually is loyal so if the dog is loyal then we want to let's say print will protect against intruder so let's say if there's an intruder in your house then they will else we can print because they're not loyal they will not protect you against intruders well not protect let's save and run that and as you can see will not protect against intruder now let's say we wanted to increase this loyalty right here now what we can do is we can say dog and let's give it a nice function we can use something that will make sense because usually if you feed a dog they also become more loyal so we can say feed and what we're doing now is we're over writing this feed right here we're overwriting it so we're changing it to be something else so you can do that if you want to and that is equal to a function and we can say self and now we can override that function so instead we can actually copy this for right now so instead of it's changing the status and also just so you know if we comment this out we can still access the status reprint lassie dot status if you print last it'll stay this will still get a status hungry because status is what's inside of this table and that table is being passed in here we're just adding to that table so we can still use status and this is where self really comes in handy because now we can reference to the status we can't see here but we know it's part of this specific object so now we can say they were fit we can change their status to full and now we can add to it now we can say self dot loyalty is equal to loyalty i mean self did loyalty plus five so if we were to feed our dog twice then theoretically they will increase their loyalty so now i can say lassie and then feed they will still not be loyal if you do that nazi was fit not loyal but after the second feeding they will become loyal will protect against intruder and that is actually all i want to show you so i'll go through this once again so we're creating an object and this object can be used on its own you do not have to create this sub-object we have right here you don't need it but this object can be used to create other objects or to create variables that allow them so if we want to do we could say cat is equal to pit and we're going to say less and that's perfectly fine there's nothing wrong with that but what makes object-oriented programming so good is that we can create other objects to go over that object so here's a translate table and we can put that table instead of a variable advantage so local tbl for example is equal to and that and then just return tbl there will be nothing wrong with that if i were to run this will still get the same output as you can see here but i just like doing it like this because there's no reason putting it inside of a variable first but once we created this object and we create another object of it we can treat it as its normal object and add more functionality to it so for example let's say we have chefs now a chef that specializes in a fish for example a sushi chef they won't know how to make ratatouille for example but the chef that is in a restaurant or a restaurant chef will probably know how to make ratatouille so you could have a function for that shift that says make gratitude and you can have a base shift class which is all the default things all the shifts should have then you can have a sushi shift as a sub class and a restaurant shift as a subclass where the restaurant chef can create rancid chili but not sushi and where the sushi can create sushi but not gratitude and you can use those and listen your game for example where where every object has a different way when you interact with it but they're all part of the base object for example you might have an entity object and your player your enemies and any other npcs in the game will maybe extend from that entity object and what we did here is extending we created a subclass i hope you understood this oop can be a pretty difficult concept especially for beginners to grasp so if you didn't quite understand it what i highly recommend you do is to create a game create a game where you have a bunch of things in it you need a player you need enemies you need npcs or just people you can talk to in the game it doesn't need to have a gui you can just it can be in the terminal here you can have like a bunch of yes or no answers and stuff like that and you can do it with functional programming where truck created rpg that's probably the best idea you can do the functional programming and if you find out the functional programming makes it difficult try to do it with food because i can almost guarantee you oop will in some sense make it easier to create that game today we'll be learning about meta methods so sometimes you want to modify how some things work in your code for example if you add two items together you want to change what add does so you want to change what the plus symbol does for example if you say five plus five you actually wanted to execute five times five that is completely possible so we're going to learn how to basically do that so let's get started so let's say we want to add a value in two tables together to that we can create a local function and we can call this add tables table values okay and then here we can just specify x and y and now in here what we want to have happen is it should return x so this is the table we are passing two tables in so x dot num plus y dot num so we're basically saying okay add these two values inside of a table together when we add two tables together now the two tables can be local tbl1 for example and that can be equal to something like num is equal to 50 and we can copy this and make table two here have a num equal to 10 so in total we should get 60 from this now the thing is we cannot just say let's say local anse for answer is equal to tbl1 plus tbl2 because by default lua doesn't know what to do here so if we were to say print ons now we will get an error because you can't just do that do that as you can see it cannot open over that's a python whoops lua and you just say as you can see it cannot do it because we don't have a plus symbol to work with tables so lua doesn't know how to work with him so here is where our meta tables comes in so actually i didn't notice where we could do that there we go so here we can say local meta table and it's just something to specify what everything does so here we have an underscore underscore add this is for plus symbols is equal to add table values that should be an equal symbol basically what we're saying here the plus symbol the add symbol that plus there should do this when we're working with tables although it doesn't know tables just yet we're just saying this ad should do that now if you want it to work what we should do is we should say sit meta table and in the table we want to change so tbl1 and then the meta table so this just means whenever table 1 is involved and actually we should then put this here so whenever table 1 is involved we should execute with this values so in this case if we do plus here we run it and we get 60 because what's happening is we're saying okay is table 1 in here yes it is now table 1 has a meta table of underscore underscore add which means we're changing the plus symbol this underscore undergrad that means plus and we're making it equal to this add table values this function right there so we're changing that plus here of course you didn't have to use it you could have just said local on subject alt 2 here is equal to add table values and you just say tbl one dot or would you you could probably just pass in tbr one and tb l2 but there are some cases where doing this could actually be what we want so we can just use plus symbols because it just makes sense of course you can do more for example underscore underscore sub which means subtract so you're subtracting from something and we can make that equal to and here we can even specify an anonymous function so function and then we can just say what i should do instead of passing in the function name here so it's completely optional and we just x and y again and here we can go x dot or return x dot num minus y dot num so it will do exactly the same thing but it will minus so now if we want to now it's already been added actually because table 1 is set to this right here so it's changing the add and the subtract so now if we say minus here and we run this now we get 40 because it is 50 minus 10 which is 40. and you can do a lot more here i'm going to go through all of them you can kind of go through them as you need them so here are all the ones you can use so there's underscore underscore add which is equal to plus then you get underscore r sub which is equal to subtract underscore r square mol for multiply underscore div for divide underscore mud for modulus and if you need a refresher what modular modulus is basically it is whenever you divide something then what is left over is basically being given to you for in case you may have forgotten so five modulo four will return once if i were to just open up my interpreter here five modulo four as you can see we get 1 because when 5 is being divided by 4 we have 1 left over and that's what's going on here so 6 divided by 4 will give 2 7 divided by or 7 modulo 4 would give us 3 and an eight modulo four will give us zero because four or eight divided by four will give us two with nothing left over anyhow so that's modulus for in case you may have forgotten pow for power or power there we go and then we have concat and this is basically for these two things so when you cut two strings together there's two dots that's concat then we have len and that's the same as this hashtag so it will change that hashtag symbol what that does there's a lot of things you can do for example eq as well basically if something is equal to something else so this can compare you can maybe make a compare two tables together by looping through it and whatnot so you can change what that does larger than which is basically or less than not larger than my bad less than and also less than or equal to which is this and you also have i believe greater than which is equal to this and then greater than or equal to probably which is equal to this so there's a lot of things you can change i'm not going to go through all of them because as you can see there's quite a lot so yeah so let me maybe give you an example what let me make encounter here so let's say both of these has an x and y value so x which is equal to let's say 33 or they both now have a different one and the y which is equal to 20. and in this x we can make that equal to 55 and that y we can make that equal to 9. okay so you can kind of imagine these as vectors where basically you have an x and y value and like a coordinate and then you can add them together so let's say we want to add these two quarters together so you can change this add table values so you can do that so here we can maybe even return a table and we can say or actually let's not return television return two values so we can then say and i don't want to call them x because that's going to be very confusing let's make a v1 and v2 here so v1 dot x plus v 2 dot x and also then v 2 dot y or actually v 1 dot y plus so basically it's adding these two together so you can get an x and a y value if you wanted you could have passed them into a table like this they would have been perfectly fine i just didn't want to and in here we get our answer so i'm actually going to say vic x for vector x and vic y for vector y or actually it doesn't basically be the locations so then here we can say x and we can just pass in thick x and here we can say y on a new line and you can just pass in vic y save that and now here we can run our lua file and this should be add not subtract otherwise we'll be getting an error because subtract doesn't do anything currently now if you're trying to run it and actually since i didn't really want to figure out how to fix this let's actually put this inside of a table here and we can just call this x and make x equal to that and go here y and make y equal to that so now we can just have one table that can have all of those values so you can just call it vector so just pick and we can say pick dot x and vic dot y run that and now we get x 15 and y 29 because 10 plus 5 is 15 and 20 plus 9 is 29 so this is another way it can be very useful so especially especially in game development this can be very useful so instead of having to create a function or constantly having to rewrite how to add these two tables together because you might be using tables a lot you can write a function that will modify how tables are added together so you might be wondering what now we have basically finished learning lua and you might want to continue with it well first off i would recommend checking out lua rocks and looking at some of the packages some of the libraries you can use in lua so this means like the lua file system for example a file system library for lower program language probably allows you to work better with the file system than what os allows you or lua esquivel allows you to work with sql there's so many things you can get if you try and go to for lua you could even maybe try and do some roblox coding of lua because they use a form of lua for most of their games so you can use lua for that as well if you're into roblox one of the courses i would have given or would have taught but decided against because i have i don't do game development anymore i have figured out that's what i want to do it's kind of boring at least for me it is but it was fun while it lasted i was actually about to create them about three videos before i finished my course here and then i was like okay i'm just not going to anymore but if you want to do game development in lua which is something that a lot of people do because lua is truly a great a great language for game development and i myself really loved it before i just decided it's too much effort and that's not really something i'm interested in because it just takes too much time for a very small amount of things even if you're doing something like go dot or unity it's just not something i'm interested in but if you are i would recommend checking out love it is free and it allows you to create some basic and pretty cool games i can actually show you something i probably have here here we go so it's basically the game i made in delphi but it's a little bit you know less difficult but it's interesting because you can make some pretty cool games this is just a very basic example of a type of game you can make and later on there will be more balls coming at you as you can see there if you click it as you can see we get a replay menu quick go to menu we go here we can go to settings which doesn't really do anything because i never really got there because i like to work a bunch of games at once and yeah now if we go to the asteroids and just love dot here now this game is not complete but it has some basic functionality so you can't really shoot but i do believe you can explode i'm not quite sure and it with all screens let me just bring it back here let's see and yes you can explode and yeah so this is just a game i'm i was also a bit working on but decided to drop because it's not something i'm really too interested into anymore and yeah so you can make a lot of cool things with love and you should just try it out because it really is amazing if you love game development or at least if you want to try it this could also give you kind of an idea of what's happening in the background because it basically the whole concept is you draw you update and you load that's the whole concept you do everything in there and it just it just works but if you don't really want to continue learning lua if you're happy with what you have then i would recommend learning c and or c plus plus i do have a course coming out right now as we speak because why not so i'm doing a c bus plus course right now and if you want to i recommend learning c plus because lua is actually kind of meant to be built or to kind of be used inside of c or c plus plus a little bit of an extension of language to make things a little bit easier maybe create a settings file and then you parse that settings file with c plus plus or c that's one way to do it but if you don't want to learn that i do recommend learning python because python is really similar to lua and python is basically the lua that took off that became popular and now everyone wants to use it so i would recommend learning python because python is basically lua just more popular so yeah these are some of the things you can do if you now want to continue if you want to continue with what you have also have a python course on my channel crash course is also coming out soon and yeah that's about all there is to it uh thank you for watching and joining in on this lua course lua is truly one of my favorite programming languages probably in my top three it is sad and i decided i didn't like game development otherwise i would have continued to teach game development for a while at least but yeah thank you for watching i hope you all enjoyed this whole course we had and i will see you all again in the next tutorial