The foundation for programming is variables,
it is what we use to store different types of data. In Lua you do not need to declare the type
data or value that your variable will contain, unlike a lot of other programming languages. There are 8 different types of data that you
can store in a variable: We are going to focus on these four. The first one is nil, it is the absence of
data, that is an important thing to understand, it does not mean 0 or false, it is the lack
of anything. Next up we have Booleans, a boolean variable
can be either true or false. An example would be a variable that keeps
track of whether or not the player is able to jump. Then we have Number variables, that can store
any type of numbers, both integers like one, two, three and so on, as well as floating
point numbers that have a decimal point. An example where we could use a number variable
is to store the health of a Player character. Finally a String is a way to store a sequence
of characters, this could be used to store the name of the player or instructions that
you want to display on the screen. To declare a variable you simply type something
followed by an equal-sign, and the data that the variable should store. Variable names need to start with a letter
but may contain numbers and the underscore character. All variables are case sensitive. Here are 4 examples of variables, the first
3 variables are correctly declared and will work just fine, while the forth one breaks
the rules by starting with a number. This would cause your program to crash. We are able to perform different arithmetic
operations on variables, or in simpler terms, do math to them. Let's say that I want to find out how much
money I have left from my monthly salary after paying all of my essential expenses. We can do that by declaring some variables. First I will create a variable that will contain
my salary, I will name it, “salary” and set it to be equal to 1000. Then I will create some variables for my expenses,
starting with a food variable that I set to be equal to 300, then a variable named ‘rent’
that I set to be 400. Finally I will create a variable that keeps
track of how much money I spend on Fortnite skins, lets call it ‘investment’, and
set it to be equal to 600. Now we can create a new variable called “result”
and set it to be equal to the salary variable minus rent + food + investment in parentheses. To be able to see how much money we are left
with we can use the very useful function called “print()”, inside of the parentheses we
pass (or in other words type in) the result variable. If we now run the program you will see a number
appearing inside of the developer console that we enabled in the last episode. Unfortunately it doesn’t look like our economy
is working out. Have you ever tried to read spaghetti? it’s
hard! Reading spaghetti code? Even harder. Good variable names and clean code go a long
way but sometimes it can be useful to leave messages to another programmer or your future
self to explain what your code does. The way to do this is by creating a comment. Typing two “-” at the beginning of a new
line will tell your program to ignore all of the text that comes after. Here we could type a message explaining that
this code calculates our financial situation. Tables are the data structure in lua. A lot of other programming languages require
you to pick a specific data structure depending on what you want to accomplish, but Lua just
needs tables. You can think of a table as a variable that
can contain multiple other variables. A table can even contain other tables, and
that table can contain a table * 5. Declaring a Table is very similar to declaring
a normal variable in that it follows the same rules. You simply name the Table and set it to be
equal to curly brackets. To add a variable inside of our table we simply
type the table name, followed by a period and then the name of the variable. Now we can set this variable to be equal to
some data, for example the number 10. To create a table inside of our table, we
do the same thing as with the variable example, except we set it to be equal to curly brackets. Alternatively we can declare variables and
tables inside the table, as we create the table, like this: For each new table or variable that we add,
we need to separate them with a comma. Imagine you have a player who has the ability to take damage. When the player's health goes to 0 or below,
you want the Player to die. To accomplish this we need Relational Operators,
which we can use to compare different values. This is usually done inside of an “if”-statement,
which we will cover in just a moment. There are the 6 relational operators that
Lua supports: “==” We can check if two values are equal
“~=” or not “<” If one value is smaller than another
“>” or greater “<=” Finally we can check if a value is
smaller than or equal to another value “>=” or greater than or equal to. Sometimes you will want to run specific pieces of code only when certain conditions are met. To do that, we can use an “if” statement,
which looks like this: I have declared a variable named money and
set it to be equal to 150. For example, the condition can be to check
if our money variable is greater than 100. If that is the case then the code between
the start of the IF-statement and the end is triggered. To test this out I will print(money), don’t
tell anyone. If we run the code you can see that it prints
the value in the console since our money variable is above 100. If I change it to be 90 and run it again,
then it no longer prints anything. We can add multiple requirements for the condition,
let's say that I want this code to trigger if the money variable is greater than 100
but not if it is greater than 200. To do this we type ‘and’, followed by
the new condition. Now the code will only be triggered if both
of these conditions are met. If I set our money to be equal to 210 and
run the program, you can see that nothing prints, but if I lower it to 150 then you
can see that the code gets executed and the program prints the value of the money variable. If we wanted the code to trigger when at least
1 of these statements are true, then we can use the keyword ‘or’ in place of ‘and’. However, in this scenario the code would run
no matter what since the variable would necessarily be greater than 100 or less than 200, so let’s
change it back. If we want to do something when we have 100
or less money, we can create an “elseif”-statement and check if the money variable is equal to
or less than 100. Inside of this “elseif”- statement I am
going to print(“I am poor :(“). If we set the money variable to be equal to 50 and
run the program, you can see that we are indeed poor. Finally we can add an “else”-condition
that will trigger if none of the previous conditions are met. Note that “else” is inherently a catch-all
statement and so, we don’t need to type a “then” keyword when using it. I am going to print(“yay I’m rich!”)
within our else statement. If we now set the money variable to be equal
to 1,000,000 and run the program, you can see that I am very rich. So I am quitting my job as a YouTuber with
20 subscribers, good bye! Functions are the main way to abstract code
in Lua; with functions you can group code together in order for it to be reused at a
later point in time. This greatly improves the structure of your
code by reducing code repetition. We have actually already used a function;
print(). Print() is a function that is declared in
Lua that does a bunch of different stuff for us in order to display text in our console. It does this stuff so we don’t have to,
leaving us with the simple task of just typing “print()”. Let's take our money “if”-statement. If we want to trigger this code multiple times,
we could copy paste it. Woah, this became a lot of code very fast. We can clean it up by creating a function
instead. In order to create a function we need to type
“function” followed by the name of the function and parentheses. I will name the function checkWealth. This indicates the start of the function. In order to indicate the end of the function,
we need to type the keyword “end”. Now we can move the entire “if”-statement
inside of our newly created function and remove the duplicates. Now if we simply type “checkWealth”, it
will trigger all of the code inside of the function. As you can see now when we run the program. Now that we know what functions are, we can move on to a couple of Löve specific things,
starting with the 3 main Löve functions. These are called: love.load(), love.update(dt)
and love.draw(). love.load() will trigger when you start the
game; this is where you want to load all of your data and assets. love.draw() is used to display graphics on
the screen. Finally there is love.update(dt). This is basically where you will program the
actual game logic; anything that needs to update will in the end be handled by love.update. This function will trigger 1 time per frame
produced by your computer. Depending on how powerful your computer is,
this can happen hundreds of times per second. This variance causes some problems though,
let's imagine you have a player that moves at a speed of 100 pixels to the right every
frame. On a computer that has 10 frames per second,
the player will move 1000 pixels in 1 second, while a computer that has 100 frames per second
will move the player 10000 pixels. To prevent this we need to program our game
logic to be framerate independent, and we do it by using Deltatime. You might have noticed that love.update(dt)
has the letters dt inside of the parentheses. That stands for dt and makes delta time accessible
inside of the entire function. Deltatime is the time it took to produce the
frame. If you have 10 fps then delta time is 0.1,
meaning each frame took 0.1 seconds to produce. So if we multiply the player's speed by delta
time while we have an fps of 10, we will move the player 10 pixels, 10 times in a second,
or in other words 100 pixels in 1 second. If we have 100 fps, then delta time is 0.01
and the player will move 1 pixel, 100 times in a second, producing the same 100 pixels
in 1 second as before. Multiplying by deltatime ensures that your
game runs the same way on all hardware, regardless of performance. That is the end of this crash course in Lua,
with a little bit of Löve. Don’t worry if you didn’t fully understand
everything, that is not the point of this video. In the next video we are going to start creating
an actual game, and as long as you have just watched this video you will have a much easier
time following along.