Lua Intermediate Introduction
Lua Intermediate is the deeper level of Lua that follows Lua Beginner. It gives more control over tables, functions, and larger programs.
A name and a level print as two values
Beginner code still runs at this level. A table holds a name and a level.
local player = {name = "Mia", level = 3}
print(player.name, player.level)A loop can total a list of numbers
A loop adds each score to a running total. The total shows the loop worked.
local scores = {10, 20, 30}
local total = 0
for i = 1, 3 do
total = total + scores[i]
end
print(total)A simple function returns a doubled number
A function takes a number and returns twice that number. The printed result proves the return value.
local function double(n)
return n * 2
end
print(double(21))Example
local player = {name = "Mia", level = 3}
local function gain(p)
p.level = p.level + 1
return p.level
end
print(player.name, gain(player))Key points
- Starting point: this level assumes Lua Beginner is complete.
- Depth: this level goes deeper into ideas met at beginner level.
- Control: this level gives the reader more control over how tables behave.
- Programs: this level works with larger programs built from small parts.
- Practice: each lesson ends with a short check of the main idea.
In this example, a table holds a player name and a level. A function adds one to the level and returns the new level. The print shows the name and the new level together.
Note
beginner tools stay useful and appear in every later lesson.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does this level assume about the reader?