Lua Programming Tutorial | Episode 1 | The basics

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

If you insist on making a video, please provide a written transcript. I can read faster than the video plays back, and IMO more importantly, there is no way to 'search' a video - you have to keep jumping around until you're lucky rnough to find what you are looking for.

👍︎︎ 2 👤︎︎ u/SoraFirestorm 📅︎︎ Sep 01 2017 đź—«︎ replies
Captions
hello guys my name is Dustin also known as Razer game dev and welcome to the first episode of this Lua programming tutorial so today I'm gonna talk about what do it is what it was used for and we're gonna get started on some of the basic concepts of Lua the programming language so like I said Lua is a programming language and if you want to go to their main websites I have to do is go to WWE org and Lua is a scripting language that's written in C that's extremely performance people say it's probably the fastest scripting language on the market which makes it perfect for things like application development and games I think the biggest use of Lua rain now is actually in the gaming market and field it's used to embed in C++ projects it's used to as a scripting language as like a alternative to something like a JSON file you know so Lua is a very expressive very interesting very simple and easy to learn programming language but surprisingly there's not actually a lot of good tutorials out there on the language itself it's a little bit hard to get into so that's what I'm gonna try to do today and in the following episodes so let's give you a little taste this is what Lua looks like so as you can see it's very simple it's very it's not very verbose it's very expressive as in you use words to express concepts instead of symbols a lot of times which is kind of nice it's kinda nice if you're learning so yeah if you want if you don't like this on my voice you don't want to you know deal with all this you can go over to the manual but sometimes easier just to watch a video and get an explanation but yeah the manuals right here it's it's very good man you know it's very dense so give that mind so to get started with Lua click the download button and scroll down you know it's not this big download button unless you want to actually build from source but you have to find a binary a binary is something a sort of compiled at your computer to run so we're gonna go click get a binary right here we're gonna go down to our platform I'm on a Windows machine though Lua runs very well on all platforms really all platforms on Linux you shouldn't even have Lua probably already in anyway so we're gonna click that windows link and we're gonna scroll down to the five point three point four release one then we're gonna click this top download right here so this is the newest version of Lua loop five point one is still used um but I'm gonna be using the newest version of Lua to shop all the features and all that so let's open up our Explorer is there a file explorer right and we actually have to install it so what this is what this package gives us is the executables to run Lua but that's not very useful right now so we to be able to actually use it we want to go into our C Drive we want to go into our program files and we're gonna create a new folder called blue on inside that folder we're gonna drop drag and drop all these uh these executables in the DLL right in there and then we should be set to actually well will be set to continue forward to actually get Lua to run though because we want to be able to run it from the command prompt like this we want to stay type of Lu and be able to run it to do that we actually need to append this directory to our path now the global path if you just type in path in your search bar right here the global path is something that allows your command prompt to basically find applications and programs so you can run them so go look for something that says path down here right here and I have a lot of things right here you probably won't have as many but you want to click new and you want to copy this right here copy this directory so just right click copy click new paste it right there now I already did this so I'm not gonna actually paste that in but you will click OK and you should be good now to actually run your lua you want to click your windows key and then our and then start CMD command prompt so you just type it in right there and you get your nice command prompt and if everything works out you might need to restart your computer if if you don't see anything but you should be able to type in Lua and you'll get this nice interactive Louis shell so one thing to do right now is just print type print then hello world and then click enter and as you can see you can run commands in this shell right here you can do one plus two three and it'll actually evaluate it and you have a very powerful calculator here okay so now let's talk about tools they can use to edit your Lua code so there's several really cool good tools out there but the three that I recommend are Visual Studio code this is the one I recommend if you're brand new to programming if you're a brand new to lure programming and all that so you just want to go to visual studio comm and then you'll see this big green download button you can download super easy to install um also I highly recommend sublime text 3 or 2 either one um both are very good I used to use this as my main is my main editor for years and then if you want to get a little bit more hipster and look and you want to up your programming game then I recommend down like the bim editor now them is not something that's very easy to get just get into and there's a running joke that if you want to generate a random sequence of characters you just ask someone to get out of them or save a file in bim or something it's a running joke but once you learn them you'll find it to be very powerful and that's actually the tool I'll be using in this tutorial series alright so we're gonna get started writing our script so what we what I did was I created a folder in My Documents and named it Lua tutorial what we're gonna do is we're gonna click new and we're gonna click text document and we're gonna I'm just gonna name this my like script alright and then you should see this extension if you don't see this extension you want to go over to view and click filename extensions tech tip that you should see that two text file we actually want to change this so we're gonna right click this click rename then we're gonna rename it to dot lua and then that's our lua script now you want to open this up in your editor of choice i'm gonna open it up with them and now we have an empty file that you can text file type in alright so how do we actually well list this first type in print hello world like we had before now how do we actually get this to run and print on our screen well if you open up your if you type up here we're in your directory that's CD they'll open up your command prompt after typing Lua and then the name of the file so script Lua you'll actually run your code so that's really nice that's really nice so that's how we can actually build our applications back so the first thing we talk about is variables alright so what is a variable how are they used in kind of what is the idea of a variable so you can think of a variable as a container for a value so a container for a value that you can poke around in you can change the value you can put that value in other containers and it's basically the universal way to to layout data so what does that look like so we're gonna say we're gonna give our variable a name we're gonna name it my bar and we're just give it the value of 10 all right so we now basically assign a variable named my bar and put in the value 10 so how do we check what's in that container well we can use the prints keyword so type in print and open in closed parenthesis and then type in my bar just like that now if we go back to our command prompt I mean type in Lua scripts we should see 10 so just basically printed what was in this container so let's create another one this other just name it other and give it the value of 40 sure why not now what we can do is perform math on these variables so we can say my bar plus other so if we run this right now we'll see that we get the number 50 so as you can see we can perform many many mountain math operations so this creates um men result and make it equal to Maya bar minus other Oh now make sure that you get the capitalization of your variables correctly as you can see I'm putting a capital o in front of things and thing needs to be spelled out exactly how it was before or else Lua doesn't know what very bleep talking about so we're gonna print out what's in the container result and as you would expect it's negative 30 because we take my bar and we subtract which is 10 we subtract 40 from it so this is a little confusing you know a lot of people say that they they're not gonna be any good at programming because they're bad at math and I find that kind of funny because in math you don't tell the variable what it is you find what value the variable is where's this you get to explicitly tell us what math to do and what value it is which is really nice if you think about it again we can divide the operators are the multiply you can multiply them out so these are the operators that you can use so you can use plus minus divide times you can do I believe you can do power let me check if not there is a way to do power oh the second I'm gonna put a couple symbols right here and I'll explain why in a little bit so let's run our script and yeah as you can see you can do power alright so as you can see when I put these two minus signs right here let's get rid of a lot of this right here when I put these two minus signs right there everything else turned gray and that's because we can actually document our code so if we want to write like a sentence in our code and not have it run we put two minus signs and then you can say hello well this won't run right now if you're trying to run a script nothing will happen if we remove these characters right there you'll see that and though it gives us an error syntax error near-field or near world it doesn't know what this code is so let's say we want to do well like as you can see you can type things out and all that but if you go down to the next line you know it's not commented out so if you want to do multi-line comments you'll do two minus signs followed by to open square brackets then you go down and you do two closing square brackets now a lot of times your editor help you with this but now you can do multiple line fonts and all that it's really important to document your code very thoroughly okay so we learned about how you can perform math operations on your variables but what if we want to store something like text like if we want to we want to prompt the user like type in your name or something like that well we can store text but not yet we need store text in a variable also but we need to do a couple things so we're gonna say text is equal now you can't just type out the word that you want to store you actually have to put it in quotation marks just like earlier if you remember we did print hello world we have to do the same thing it has to be in quotation marks and that is because that signifies kahlĂşa that this isn't code that's in here this is a string so now if we've taken print and then text and I wrote a little lowest bash batch script to help us run the code easier but as you can see this prints hello world out so another thing can do with strings is you can do multi-line strings so if you want a really long string so my long text you can do something you can do to open bras square brackets but remember to follow it up with two closing square brackets now you can do like you can let's copy this text and paste it like a ton of times in here so that yank there we go so as you can see you know we can can really fill this up with a lot of text which can be useful and then you can print it out another thing you can do is combine text so we have let's just name this part one we're just make it hello then part two is equal to world now if we want to create full text we can say parts one dot dot these two dots right here part two now let's print out full text and as you would expect it's the whole world word right now hello world so this is called the concatenation operator this takes the first string on the left and combines it with the string on the right so you can do things like you know you could even do it right like this and then put passing a value right hello zero and then world and as you would imagine it says hello zero world like that so that's how you can kind of combine text and this will automatically turn into a string so yeah that strings in a nutshell there's a lot of other things you can do with them but for just a simple entry-level tutorial that's what strings are used for okay so now the last variable type that we're going to be talking about right now is something called the bull lean so what a blue lean is is basically a binary value that either says that's either true or false so these are used to check logic and are used everywhere in programming so if you want to set something to true so we're gonna say my blue lean it's equal to true if we print my baleen is equal to true well we'll see that sequence appeared so that's not very useful but what can look in these was was this boolean used for it really so let's do bar 1 is equal to 100 then bar 2 is equal to let's say 32 so we're gonna say bar 1 is double equals bar 2 so this says is bar 1 equal in value 2 bar 2 now this this looks different so regular like if we want to assign a value to a variable if you want to put an object in a container essentially we have a single equal sign but if we want to check the value in a container is equal to another value in another container we would do a double equal sign so that's something that you can you can kind of mess up on early on so really hammer this in to your brand is that double equals means compare the two values single equals mean a sign of value all right so if we run this we'll see that it's false because var 1 is equal to 100 bar 2 is equal to 32 which means bar 1 is not equal to bar 2 which means it will be false we can do a couple other things we can do greater than so if we run this it is true if I'm actually gonna speed this up a little bit so I'm gonna cut this and paste this in here right and I'm gonna copy and paste this down here so we're gonna do equals then rather than less then we're gonna do great greater than or equal to we're gonna do less than or equal to and then we're gonna do not equal to all right so not equal to is a tilde which is right to the left of the one key and you have to click tilde it's a little bit weird but you'll get used to it real fast and yes those are all your binary operations that you can do well release most of them unexpected symbol so as you can see we have a little error and it's because we left this in this isn't valid little coat so we're gonna comment that out as you can see we get printed out false true false true false true and you can you can deduce if that's all correct yourself okay one thing I mentioned that if we gonna mention about the strings before we move on is that you string so let's make variable my spring can either be double quotes or single quotes but the key but it can't be both right if you look down here it still thinks that it's a string now you can use um the reason why they allow you to use single quotes and don't focus because then 9 you can put quotes inside of your string and that should be valid good see that anyway so what we're gonna be talking about right now is reading values from the user because right now our program isn't very good our program isn't very useful we want to be able to get input from the user so let's create a variable and call it input and set equal to something called IO dot read and then make sure you add these two parentheses and now we're gonna print by that in inputs alright now first up here is a prince type your name just like so so we'll run this and as you can see instead of closing our program is still running right here and we can now type in a bunch of text right here and then click enter and it's gonna print the body right there so quick let's talk about what this actually is so this is something called a function call you notice this right here and we'll talk about this in more detail right now it's going to give a siq idea what this is so somewhere out somewhere on your computer there is some code written somewhere else and a function call is basically a way to run that code outside of this file so basically this is this is some code that says take input from the user and then store it into the value input so that is very useful so now we can kind of combine what we use to make a very simple program so right now we're gonna talk about something called if so it is something that checks the value in a container or a variable and then dues and then does some operations all right and you can use this to kind of control your code and do certain things right so we're gonna write a program we're gonna say we're gonna print type your name right and then we're gonna say again we're gonna get our input we're gonna name it name I'm gonna make it equal that IO dot read now we want to check what the user typed in so if you if you remember we can say my bar is equal to name is equals to hello right and this will become a a boolean right so if the user typed in name then we'll get boolean's so let's let's try this out or attention hello so if we type in some random characters is gonna say false because what we typed in is not equal to the hello we type in hello we'll see that is such a true all right so now we're getting somewhere so now we can actually use this so we're gonna use the keyword it so if name is equal to Dustin then we're gonna open up this block so the then keyword just says then do this next set of instructions and then the end says this is the block this is the end of the block that the if statement opened up alright so we're gonna say print nice name dude and we're just gonna end it there so if we run this we're gonna type in Joe yeah you know nothing printed and now protecting Dustin will say it says nice name dude so what happened when we why did it not print nice name dude when we typed in the name Joe well it's because it says if the name is equal to dust and then do this code if it's not evil to dust and it's gonna skip everything that's inside this end statement so you can actually have another ear statement inside here and just keep nesting these and then you know you can do more code in here like declare a variable sure Twitter anyway so whoops so yeah so this everything that's in here gets skipped over if it's not if the value that is being here is not equal or but you can also do you know the second not equal right so if the name is not able to Joe so we can find Dustin it will prints that and then you can do you can't do greater than or equal to when it's a stream that you can with numbers so let's let's explicitly say it the name is equal to Dustin then do this otherwise or else do this alright so if you run this we'll get some of the same results will say Joe yeah I guess that's okay alright and then now we click if you type in Dustin remember it has to have the same cat same case as what you had in the string this is nice name dude alright so we can mix so we can say do this or do this basically we can also do an else if so if you type in else a name is equal to Joe you can do yo Joe now if you tapping Joe let's say yo Joe and it will skip this LLL skip this because it this is the path that I've found so that's really useful that's our control flow structure of this program so what happens if we type in a variable right here so my bar is equal to 100 and then now down here I'm gonna type it I'm gonna print what's in my bar well as you type in the name as you can see it printed something really weird it printed something called a nil what is now well Neil says that there's no value in my bar but clearly we set my bar equal to 100 so this is where things get a little bit interesting so since the skipped over all the code right here nothing actually was assigned to my bar I was all prepared to give this explanation but then I forgot something very key so what will table this will table this right now so we're gonna comment this out and we're gonna be talking about local block actually no no no no I change the mind so we're gonna type in Dustin so now we can see that the value is equal to 100 that makes that's right so one thing that is important in Lu is that and in just about all programming languages is we want to contain our variables in a smallest scope as possible now what does that mean so as you can see me to find something in my bar and then we can print it out here but out here what if we don't want people to have access to my bar what if that contains your bank information what that contains by you guys if someone changed it would break the program so that's where local variables come in so right now we've been creating everything blue that means everything in this program is available everywhere but we want to keep things local so that it doesn't escape out here it stays inside this block so now if we try printing this right here we type and Dustin will see that as nil and that's because local can finds the variable to the sky that's in and this block right here this block of code his cell is called a scope all right so anything between and then and then an else--if or between a then and an else or between Alice and N is a block so we can do something like my bar and then we can create a nut if 100 is equal to 100 we say local other is equal to 32 now we can try printing other and dust it as you can see we get to Nils because other is defined inside this block and as soon as it goes out it's no longer fine but if we move it inside here we can see that we get our value and this will become way more apparent later in on in the tutorial I believe so for e now make every variable that you have local because it's very bad it's bad practice to keep everything local right now all right then I think the last big concept that we're gonna cover in this tutorial this episode is the loop construct so we want to be able to repeat actions so right now everything is linear we basically type in code and it performs the steps from top to bottom right but what happens if we want to do something many many times let's say we want to print the number 1 through 100 well obviously we could just do 1 then copy then copies value change it to 2 3 4 5 6 and then you know this was this was work but this would take absolutely forever looking at print thousand numbers so that's where loops come in so loops come in many different flavors and varieties there's the for loop and then the while loop so first we're gonna talk about the while loop so we're gonna put while true what do they're going to say print hello all right so a do is just like the the van keyword it opens up a block which means we also have to end it which also means there's a local scope right here meaning my bar is confined to this local scope anyway so if we run this we'll see that we're gonna be printing hello for eternity forever if you have to want to stop it from running click ctrl and then C and that should cancel it and then click guess if it prompts you yes so what happened so right here says while the value in this while the boy leaned in here is equal to true do this action so we can say 100 is equal to 42 if you run this it's not gonna do any code because this isn't true so it's not gonna run so let's create a little about you say equal to 100 local others equal to 128 and this safe vowel is greater than other well val is not created another so just gonna miss out it's gonna skip this so val is last because val is equal to 100 and others go to 128 so now this is gonna run forever all right so now let's take what we know about this and let's make it count from zero to a thousand all right so once a local counter is equal to zero actually first off I want you guys to see if you can figure this out if you can pause the video right now and try it out if you can't figure it out then continue watching and I'll give you the solution all right so let's continue on so we're gonna say local Max is equal to a thousand-piece we want to count from zero to a thousand all right so we want to say while counter is less than max then we must say do okay never print this will go on for eternity so we need to actually add we want to make counter go up in value every single time so right now it's also print we run that you'll see that printing all zeros so let's actually move counter up towards max so we sink counter is equal to counter plus one now if you run this we'll see that it counts all the way up to 999 because because it says if counter is less than max which 999 is less than max because max is a thousand thousand it's gonna print it let's go back to the top is gonna say is 1000 less than max well 1 thousands actually equal to max so it's not gonna print that so if we actually want to print the thousand we're gonna have to say less than or equal to and then now it will print our glorious 1000 number so this right here might be book this right here might be a little bit complicated to it might seem a little weird so what we're doing we're saying take the value that's encounter and set it equal to the value that's a counter plus 1 so it's going to start off it's gonna say counter what's encounter right now counter is equal to 0 so counter plus 1 so that's 0 plus 1 is equal to 1 so now if when we go back to the top counter is equal to 1 so now counter is equal to 1 it's gonna set it equal to 1 plus 1 that's gonna go back which is to of course it's gonna go back to the top now counter is equal to 2 and it's just gonna keep incrementing the value so this is nice we can do a lot of stuff with that while loop but we're gonna want to count up and count down numbers all the time and we don't want to have to create a local variable every single time and then do a check and then increment it or document it right so there's a constructive little called for loop so we're gonna say for my gal is equal to let's set it equal to 0 and we're gonna say 100 right here and we're just I do don't forget the column and again do opens up block so we want to end up so now let's try printing my Val we print Y value will see that prints everything from 0 to 100 so what this says is create a variable name um that starts by you 100 and then count up till it's equal to 1 or Earth's sorry let me just restart that create a variable and give it the value of 0 now I want you to count up that variable until it's equal to 100 so if we want to set this we can set this to 50 and we could count from 50 to 100 as you can see we started at 50 and we count up to look where to go do one thing alright now we can also count backwards so let's set this equal to 100 and count down to zero now we have to know this is gonna still count up and it's not gonna do anything right so if we want to tell Lua how many play to count we're gonna say we're gonna put a new comma right there and then value so we say minus 1 right so it's gonna count minus 1 is gonna start at 100 and count down now this is also useful so let's go back to our zero to 100 example now we can also leave this in it let's set this to 2 so by default it counts up by 1 but we want to count it by twos now it cans of 0 2 4 6 8 etc you can also count by threes so you know 0 3 6 9 12 15 18 come on you know and you can do any value right here so that's very useful for going over yeah going over values in a loop alright guys so the last thing I want to talk about is the repeat until contract so basically this works exactly like the while loop but it does this block first so in the while loop you'll remember that we do watch well Falls do and then let's say we want to print a by you well if we run this it's not gonna print anybody let me get rid of this right here so let's just put this in a block on it okay so if we run this as you can see it didn't print anything because it's false right so it's gonna skip over this bad this block of code so what if you want it to print hello world at least once and then check if it's false well that's the repeat until command so if we copy the code that's up here just copy that there we'll see that you know it indeed whoa let's try this yeah there we go so you'll see that it will indeed print it once and then you'll check if you know it's true until it's true and since its until it's true when it's equal to true it will break out of the loop that's the last thing um so in the next episode we'll be talking about more ways to control control strip control flow and then also some other more powerful data types so far we covered numbers boolean's and strings but in the next row and the next row will become our functions and then tables so I hope you guys enjoyed if you did please like share this video please if someone is interested in learning Lua and yeah give me feedback because I want to know how I can improve these maybe later I'll redo this tutorial series and add some more information if I missed it you know I want to create a very good in-depth tutorial series on how to actually write a little code anyway guys thanks for watching and I'll see in the next video bye
Info
Channel: SkyVaultGames
Views: 130,180
Rating: undefined out of 5
Keywords: Lua, Programming, tutorial, coding, beginner, basic, computer science, how to, variables, game development, code, tech, RazorGameDev, SkyVaultGames, indie games, video game development
Id: 4NKMtazVNe8
Channel Id: undefined
Length: 34min 43sec (2083 seconds)
Published: Thu Aug 31 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.