No history yet

Introduction to Lua

Getting Started with Lua

Lua is a lightweight and fast programming language, often used in game development because it's easy to embed into larger applications. Its syntax is simple and forgiving, making it a great first language to learn.

Lua is a powerful and fast programming language that is easy to learn and use and to embed into your application.

To get started, you don't need to install anything. You can write and run Lua code directly in your browser using an online interpreter. This lets you experiment without any complicated setup.

We recommend using an online tool like the official Lua demo or Replit to run the code examples in this article. Just type the code in and hit 'run'.

Your First Lines of Code

Let's start with the classic first program: printing "Hello, World!". In Lua, you use the print() function to display text or the value of variables.

print("Hello, World!")

You can also add comments to your code. Comments are notes for humans that the computer ignores. In Lua, a single-line comment starts with two hyphens --.

-- This is a comment. The program ignores it.
print("This line will run.") -- This is an inline comment.

Storing Information

To do anything useful, programs need to store information. We use variables for this. Think of a variable as a labeled box where you can keep a piece of data. You give it a name and put a value inside.

message = "Welcome to Lua!"
playerScore = 100

print(message)
print(playerScore)

Notice that you don't have to declare the type of data a variable will hold. Lua figures it out automatically. The main data types you'll encounter at first are:

Data TypeDescriptionExample
numberAny kind of number, including integers and decimals.10, 3.14, -55
stringA sequence of characters, like text."Hello", 'Lua'
booleanRepresents true or false.true, false
nilRepresents the absence of a value. It's like 'nothing'.nil

Lua has one powerful structure for holding collections of data: the table. A table can act as an array, a dictionary, or a mix of both. For now, let's just see how it can hold a simple list.

-- A table used as a simple list (or array)
-- In Lua, indices start at 1, not 0!
items = {"sword", "shield", "potion"}

-- Access an item by its index
print(items[1]) -- Outputs: sword

Making Decisions and Repeating Actions

Programs often need to make decisions. For this, we use if statements. An if statement checks if a condition is true and runs a block of code only if it is.

playerHealth = 75

if playerHealth < 100 then
  print("Player needs healing!")
end

You can create more complex logic using elseif and else.

score = 88

if score > 90 then
  print("Grade: A")
elseif score > 80 then
  print("Grade: B")
else
  print("Grade: C or lower")
end

-- Outputs: Grade: B

To repeat actions, we use loops. A while loop repeats as long as its condition is true.

countdown = 3

while countdown > 0 do
  print(countdown)
  countdown = countdown - 1 -- Decrease the counter
end

print("Liftoff!")

A for loop is great for repeating a set number of times or for iterating over the items in a table.

-- A numeric for loop
for i = 1, 3 do
  print("Launch sequence: " .. i)
end

-- A generic for loop to go through a table
players = {"Alice", "Bob", "Charlie"}
for index, name in ipairs(players) do
  print("Player " .. index .. ": " .. name)
end

The .. operator is used in Lua to concatenate (join) strings together.

Building Blocks with Functions

Functions are reusable blocks of code that perform a specific task. They help organize your code and avoid repetition. You define a function with the function keyword, give it a name, and then you can "call" it whenever you need it.

-- Define a function
function greet()
  print("Hello there!")
end

-- Call the function
greet()

Functions can also take inputs, called parameters, and give back outputs, called return values.

-- A function with a parameter
function greetByName(name)
  print("Hello, " .. name)
end

greetByName("Alex") -- Outputs: Hello, Alex

-- A function that returns a value
function add(a, b)
  return a + b
end

sum = add(5, 3)
print(sum) -- Outputs: 8

As your projects grow, you can organize related functions into separate files called modules. You can then load these modules into your main script to use the functions they contain. This keeps your code clean and manageable, but we'll dive deeper into that later.

Time to check what you've learned.

Quiz Questions 1/5

How do you write a single-line comment in Lua?

Quiz Questions 2/5

What is the primary data structure in Lua used for creating lists, arrays, and dictionaries?

You now have a solid foundation in Lua's basic syntax, data types, and control structures. With these building blocks, you're ready to start creating more complex logic for games and applications.