Statements, what are they?

Statements are easy. It's basically something that you want to do. An action to be carried out.

The main ones I'll be going over in here is if, then, else, end

And to be fair, they're pretty self explanatory


if is the starting statement for when you want to do something IF something is true, or false.

For example:

statements.lua
test = true
if test then
  print("This is a test!")
else
  print("This is not a test!")
end

As you can see above, it reads out quite nicely. IF test IS TRUE THEN do something. In this case, print out something to the screen that says This is a test!

Otherwise if it is NOT TRUE, print out This is not a test!

In lua, it can be very readable, just like reading a book. English-like, easy to read.

Next up, local

What is local you may ask? Well it is something special and useful.

Consider this (you haven't learned about functions yet, but that's next!):

statements.lua
test = false
function test()
  local my_test = 4
  print(my_test)
  print(test)
end
test()

print(my_test)
  

What does it do? Well, we've made a function (more on that next), that has a local statement in it.

Now here's what's so special, you see how there's a print(my_test) at the bottom of the file? Since my_test is declared in the function above, we CANNOT print out my_test because it only exists inside the function.

What will happen is it will error out, my_test does not exist outside of that. But, test does! test is a GLOBAL variable. Meaning we can use it anywhere we want throught the file/files.


But pixel, why is that relevant? Well, you see reader, sometimes we don't want other files, or functions, or PEOPLE to touch our variables inside of things. That can mess things up. A good rule of thumb is, always use local when you can. It makes for better practices and just makes things easier.