Lua: Variables and User Input

This tutorial will consist of two main sections. Firstly, we're going to talk about variables and storing data in Lua, and then we're going to move on to getting input from the user and making use of that in our scripts.

Variables

A variable, in Lua, is essentially just something that can store a value which changes. I like to think of a variable like a box - we can put something in the box, see what's in the box, and change what's in the box.

The creation of a variable is called the declaration, this process can be compared to creating a box and giving it a name, and the first assignment of that variable is called the initialisation, this is like putting something in the box for the first time. We can declare and initialise variables in Lua very easily. You simply type the name that you want your variable to be called by, this needs to be something unique because we need to use the variable name every time we do something with it, followed by an equals sign, followed by the value you want to first set it to. For example, the following declares a variable called 'variable' and sets its value to the string of "value":

1
variable = "value"

Notice that as we discussed in the previous tutorial - we use double quotes to surround string data (a bunch of letters/characters).

It's worth noting that you can actually detect the data-type of a variable by passing it into the type function. Doing this gives us a string which reads the data-type, so we can do what is called nesting functions where we put one function into another, and put the type function call into the print function to output what data-type a variable holds. In this instance, the type function will be called first, and then the result will be passed to print to output. For example, the following would output "string" because 'variable' contains a string:

1
2
variable = "value"
print(type(variable))

If we ever want to change the variable's value, we can simply do something to it via its name. So if you, most basically, just want to set the variable to something completely different - you can write the variable's name, followed by an equals sign, followed by the value you want to set it to. Before I show you the code snippet - let's very briefly talk about commenting.

Commenting code is basically just leaving little notes in the code, usually describing functionality or what will be implemented next, that don't actually affect the execution of the code. You can leave single-line comments in Lua by putting a double dash - -- - and then writing whatever you want to comment for the rest of the line. For example:

1
print("Printing this text") --This is a comment, it's not executed

But back to where we were: a nice example of changing variables and using the type function would be to change the variable to various values and check the type of variable after each change. The following is a great example of those (and note the use of the string concatenation operator):

1
2
3
4
5
6
7
8
variable = "value" --Declare and initialise 'variable'
print("Double quote surrounded text: " .. type(variable))

variable = 5 --Set 'variable' to 5
print("Constant number: " .. type(variable))

variable = 10 + "100" --Set 'variable' to the number 10 plus the string "100" (Lua converted the string to a number for addition for us!)
print("Number + String" .. type(variable))

On running this, everything should output as expected. There's one other kind of data-type which we should talk about in this tutorial before we move on to user input - this is the boolean data-type. A boolean is simply a true or false value - and these values are represented in Lua, surprisingly, as the true and false keywords. You might not see great use in these right now, but once 'if' statements and other things start coming into the mix, hopefully you'll begin to appreciate them.

So now we can store pretty much any values we want in variables, let's learn how to get the user's input and put this into a variable for manipulation!

User Input

So in the previous tutorial we used io.write to write things to the screen. Luckily for us there is another cool io. function, and this one takes input from the user! It's named io.read. It doesn't take any parameters and it's extremely easy to use!

Wherever you put the io.read function call, the value that the user enters will take its place. So if you want to put whatever the user enters in a variable, you simply set a variable to the io.read function call! For example:

1
user_input = io.read()

We'd probably want to also prompt the user for a value and will also probably want to do something with the user's input instead of just storing it somewhere. A good example of this might be outputting the user's name back to them with a greeting using the string concatenation operator!

1
2
3
io.write("Enter your name: ")
name = io.read()
print("Hello, " .. name)

On execution, this should do exactly what we expected! You can (theoretically) have as many io.reads as you like in a Lua file, so we could even ask the user for more information!

1
2
3
4
5
6
7
8
io.write("Enter your name: ")
name = io.read()
io.write("Enter your age: ")
age = io.read()

print("Hello, " .. name)
print("You are " .. age .. " years old!")
print("In ten years, you will be " .. age + 10 .. " years old!")