I recently stumbled upon Lua which is a scripting language and wanted to learn it. This blog is going to be similar to a cheatsheet, documentation style, which makes this a great resource for someone who already knows coding or coding concepts and just needs a reference to get started in Lua, which is also what I will be using it for.
Basics
We can’t start without Hello World so this is how you print something:
print(“Hello World”)
And for commenting a simple -- will do the trick like so
--this is a comment
Declaring a variable does NOT need to be done with a type, like ruby. The basic format is: local NAME = VALUE
examples:
local x = 1
local y= nil
local johnName = “John”
When working with numbers you can use the standard + - * / operators, ^ for power, and % to get the remainder.
examples:
local a = 1+ 1
local b = 5
local c = a * b
Updating variables is as easy as reassigning them without the “local” keyword
examples:
local a = 5
a = 7
local raining = false
raining = true
To attach strings together you use .. instead of +
examples:
local first = “Hello ”
local second = “World”
print(first .. second)
Conditional Statements
If Statement
If statements have a syntax that uses a few keywords instead of the usual {}. “if” to start it, followed by the condition which is NOT in (), followed by the word “then”, then write everything that needs to be executed, and finish it off with “end”. Optional “else” that doesn’t require “then” and “elseif” that does require “then”. In the case that more than one condition exists, the “and” keyword can be used. If you want to check for the opposite of a condition use the keyword “not”
example:
if number < 10 then
print(“The number is less than 10”)
elseif nunber > 10 and not number == 10 then
print(“The number is more than 10”)
else
print(“The number is 10”)
end
Compararison Operators
Although almost all of these are standard, the comparison operators are as follows:
equal ==
less than <
more than >
less than or equal <=
more than or equal >=
not equal ~=
Functions
Functions have similar to standard syntax except, once again no {}, instead using the “end” keyword. Start with “function” followed by the name and in () the variables that will get passed in.
example:
function timesTwo(number)
local newNumber = number * 2
print(“The New Number is” .. newNumber)
return newNumber
end
local number = 2
local numberTimesTwo = timesTwo(number)
This also supports multiple variables being passed in:
function timesN(number, amount)
local newNumber = number * amount
print(“The New Number is” .. newNumber)
return newNumber
end
local number = 2
local numberTimesFour = timesN(number, 4)
Just like in many other languages, the scope of a variable is not global. If a variable is defined in a function it cannot be accessed from outside the function. If you do need that variable it must be returned when the function is run.
Loops
While Loops
While loops are a simple way to loop and it needs a condition so it knows how long to run for and how many times. After the conditions use the “do” keyword to tell it what to do in between the “do” and “end”
To set a number of times use something along the lines of this:
local count = 0
while count < 10 do
count = count + 1
end
For loops
For loops are a bit more complicated. After the “for” keyword, we can write three things. The first is init which is where we can declare and initialize any variables we need for that loop. Second is the maximum or minimum value. And third is the increment. Lastly, end the line with the “do” keyword and you can write whatever you need to execute in the next lines. Finish it off with the “end” keyword like before.
To better understand what this all means and how to use it, let's make an example. Let's say we need a variable that starts at 0 and it should increment by 2 up to 10. Our init is 0, our max/min is 10 in this case maximum, and our increment is 2. This is how we can write that:
for i=0, 10, 2 do
print(i)
end
Conclusion
As someone that came from ruby, the syntax is very similar for most things, and in general, it is a very easy to understand and write language. This is only the very basics of Lua, so there are a lot of things to use and explore. I will be making more blogs on this so check out my medium for those in the near future if interested.