What is a variable?

A variable is a type of container for you to store your data in, it provides you with a way to dynamically handle your data.

How can I use variables?

You can use variables in your code like so:

a = 0

The code above first assigns 0 to variable a, and then uses the value stored in variable a to print 0 to the console.

What are local variables?

Local variables are variables that can only be accessed from the current scope, or block of code. We'll talk more about this later, but for now just know that using local variables is faster than using normal (global) ones.

You can use local variables like so:

local a = 0

This (minor) speed boost is provided by using registers instead of a table for variables, but this means that you can only have a limited number of local variables (200).

Using local variables is the same as using global ones, except that their lifetime is limited to the scope they were created in.

Can I get return values from functions with variables?

Yes, actually, that's one of the best uses for functions. To get return values from functions with variables, just do:

variableName = functionName(arguments)

The variable, variableName, will now store the return value from functionName!

Does a variable start off with a value?

Yes, all variables start off with nil as their value.

What about variable names, can they be anything I want?

There are a couple of rules about variable names:

  • Variable names cannot be keywords, but can include them
  • Variable names are case sensitive
  • Variable names cannot start with a number
  • Variable names ending with a number shouldn't be used as tables

What words should I know when it comes to variables?

There are a couple of words actually:

  • Assign/Assignment - Changing the value that a variable stores
  • Scope - Basically a block of code, very important when it comes to the lifetime of a variable
  • Lifetime - How long the variable will last, related to scope
  • local - The keyword used to make your variables local to the current scope