Back

Activity 1.1: Meet Lua

divider

The Idea

Lua is small on purpose. The whole interpreter is about 32,000 lines of C and compiles down to a few hundred kilobytes. That number is not trivia — it is the reason you keep running into Lua. It is cheap enough to drop inside another program, so that is where it usually lives.

Places you have probably already used Lua without knowing: Roblox (its Luau language is a Lua dialect), World of Warcraft addons, the Neovim text editor, and LÖVE, the game framework you will use in Unit 2.

The name is not an acronym. Lua means "moon" in Portuguese — it was created at PUC-Rio in Brazil, and it is pronounced LOO-ah.

One consequence of being small: you can actually finish learning it. That is not true of most languages, and it is most of why this course exists.

Saying something

print writes a line to the terminal. It is the whole of your output toolkit for now.

main.lua
print("Hello, Lua!")
Output
Hello, Lua!

You run a Lua file by handing it to the interpreter. From a terminal in the same folder as your file:

Terminal
lua main.lua

Notes to yourself

Two dashes start a comment. Lua reads to the end of the line and ignores all of it. For several lines at once, use --[[ ]].

main.lua
-- Lua ignores this line.
--[[
And every line
inside this block.
]]
print("Only this line runs.")
Output
Only this line runs.

Printing more than one thing

print takes as many values as you give it, of any type, separated by commas. Watch the spacing carefully — this catches people.

main.lua
print("Ada", 42, true)
print("Ada" .. " " .. "42")
Output
Ada 42 true
Ada 42

Commas insert a tab, not a space. If you want exact control over the spacing, join the pieces yourself with .., which is Lua's way of gluing two strings together. Activity 1.3 is all about that operator.

divider

Build

Make a folder for this course, and inside it create a file named main.lua. You will run it after every task — getting into that habit now is worth more than anything else on this page.


Task 1: Get something on the screen

  • Print the line SKYNEST TERMINAL.
  • Run it with lua main.lua before you write anything else.
main.lua
print("SKYNEST TERMINAL")
Output
SKYNEST TERMINAL

Task 2: Label your file and add a rule

  • Add a comment at the top saying what the file is.
  • Print a line of dashes underneath the title.
main.lua
-- main.lua - Skynest terminal readout
print("SKYNEST TERMINAL")
print("----------------")
Output
SKYNEST TERMINAL
----------------

The comment produced nothing, which is the entire point of it.


Task 3: Report the ship's status

  • Print three more lines, each passing several values to one print call, separated by commas.
  • Mix the types on purpose — text on some, numbers on others.
  • Look at where the gaps land in the output. Those are the tabs.
main.lua
-- main.lua - Skynest terminal readout
print("SKYNEST TERMINAL")
print("----------------")
print("Status:", "ONLINE")
print("Reactor:", 98, "percent")
print("Crew:", 4)

Challenge (Optional)

  • Rewrite the Status line so the output reads Status: ONLINE with a single space instead of a tab. You will need .. and one print call.
  • Comment out the Crew line without deleting it, and confirm it stops appearing.
divider

Check Your Work

Running your finished file should produce this.

Output
SKYNEST TERMINAL
----------------
Status: ONLINE
Reactor: 98 percent
Crew: 4

If you got an error instead, read the line number it gives you and check that every string has both of its quotes. Lua's error messages are short, and they are usually pointing at the right line.