Jump to content

Lua Programming/comment

From Wikibooks, open books for an open world

Comments are pieces of text or whitespace that can be included in a program to make the code more understandable to a programmer. Comments have no effect on the running program and are ignored by the lua interpreter.

Comments start with a doublehyphen

[edit | edit source]

In lua, comments start with a doublehyphen and run until the end of the line:

-- This is a comment
print "Hello World!"

Block comments

[edit | edit source]

Block comments start with doubleshovel and run until doubleclosebox:

--[[Comments can be spread
across several lines ]]
print "Hello World!"

Enabled code within comments

[edit | edit source]

By adding a hyphen to a doubleshovel to form a long handled doubleshovel, this enables code within the comment to become enabled, rather than commented out:

---[[The long handled doubleshovel means that this code will run
print "This will print because it is not a comment!"
-- We can still include comments by prefixing them with a doubledash
-- print "This will not print because it is commented out"
]]

Nested comments

[edit | edit source]

A limitation of block style comments is that it is not possible to nest existing code containing comments without stripping the doubleshovel and doubleclosebox symbols from the nested code:

--[[
  This will not work. Nesting of comment blocks will cause a syntax error.
  --[[
    By nesting existing code containing a comment, a syntax error will occur. This is because the nested symbols will confuse the interpreter, so a modification is required to remove the symbols
  ]]
  print "Hello"
]]

The code needs modification as follows:

--[[
  The nested comment block markers have been removed
  @ This existing comment has had its block markers removed to prevent a syntax error
  @
  print "Hello"
]]