Getting Started with Lua
Once Lua is installed, it is possible to start writing Lua scripts. Lua can be written in a simple text editor, such as Notepad, however, an Integrated Development Environment, or IDE for short, that supports Lua, will offer additional features such as syntax highlighting and maybe even code completion.
Code Commenting
When programming in any language it is useful to add comments to the code to describe what is happening and why. This is useful when the code is revisited at a later date to make enhancements, or for debugging purposes. In order to add a comment in Lua it needs to start with ‘--’, as shown below.
-- This is a single line comment in Lua.
It is also possible to have comments that span multiple lines. Multi-line comments must start with ‘--[[’ and end with ‘--]]’.
--[[ This is
a multi-line
comment
in Lua.--]]
First Lua Script
The Lua code written below simply displays the words ‘Hello World!’ in a console or terminal window.
print("Hello World!")
Note that the word ‘print’ in blue above is known as a reserved word. Reserved words in a programming language have a specific purpose and therefore cannot be used for anything else. In this case ‘print’ displays a message in the console or terminal. If the above code is saved into a file with a ‘.lua’ extension, for example, ‘hello.lua’, it can then be run.
Running a Lua Script
Running a Lua script can be done from the command line as follows.
lua hello.lua
This assumes that the user is in the directory where the script resides, otherwise the full path would have to be provided before the file name.