Lua Comments


Before we get into Lua itself, let's first talk about comments. Comments are text annotations in code that are ignored when the script is executed, but help people reading the code understand it.

We use comments throughout our examples to explain what is happening a different points in the code, as well as to show the values of variables and other important information. Because comments are used to help document this behavior, let's start with explain what they are and how to read them.

There are two types of comments in Lua:

Line Comments

Line comments are delimited with two dashes, as in --. When Lua encounters this delimiter, it ignores any content to the right of the delimiter. For example:

-- This entire line is a comment

print(123) --- The print statement executes, then this is ignored

We will often use comments in examples to either:

  1. Explain the operations performed on a line, or

  2. Show the output or value of a variable on a line

Block Comments

Block comments are similar to line comments, except they apply to entire blocks of code. When there is an entire block of code to comment out, there are two choices. First, every line in the block case be commented with a line comment:

-- local a = 123
--
-- print(a)
--
-- a = 456
--
-- print(a)

which is perfectly valid, though often less convenient than using a block comment:

--[[
local a = 123

print(a)

a = 456

print(a)
]]

In the second example, the block comment starts with the --, which is followed by double-square brackets [[ which define the start of the block comment. Everything between the start of the block and the end of the block, defined by the closing double-square brackets ]], is considered a comment and ignored.