Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Lua Quick Start Guide

You're reading from   Lua Quick Start Guide The easiest way to learn Lua programming

Arrow left icon
Product type Paperback
Published in Jul 2018
Publisher Packt
ISBN-13 9781789343229
Length 202 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Gabor Szauer Gabor Szauer
Author Profile Icon Gabor Szauer
Gabor Szauer
Arrow right icon
View More author details
Toc

What Lua is

Lua is a powerful, fast, lightweight, embeddable scripting language. The Lua virtual machine and interpreter are written in C. As a language, Lua is easy to learn. It contains 21 keywords, which makes the language rather small. Lua is also easy to read and understand, as its syntax makes it similar to English. For example, consider the following code snippet:

if not hero:IsAlive() then
GameOver();
end

This code is easy to read, and I bet you can take an intuitive guess at what it does. Lua is not only easy to read, it is also very powerful. The real power of Lua comes from its extensible nature. Programming constructs such as object-oriented programming (OOP) can be implemented in Lua, even though the language has no native support for objects.

At the time of writing, Lua has 14 versions; this book will focus on Lua 5.2.4. The latest version is 5.3; the main difference between 5.2 and 5.3 is that 5.3 contains support for explicit integers and bitwise operation.

While this book covers everything needed to get started with Lua programming, it never hurts to have more resources. The first edition of Programming In Lua can be read online, for free, at https://www.lua.org/pil/contents.html.

Source code and binaries

Lua is open source software published under the MIT License. You can browse Lua's source code at https://www.lua.org/source/. Additionally, you can download both the source code and reference manuals for Lua from https://www.lua.org/ftp/.

At the time of writing, no pre-built binaries are downloadable from the lua.org website. Pre-built binaries can be found on SourceForge at https://sourceforge.net/projects/luabinaries/. In this chapter, we will be using SourceForge to download binaries for Lua.

Installing Lua on Windows 10

Follow these steps to install Lua 5.2.4 on Windows 10. These instructions are written for Windows 10, but the steps needed to install should be similar on older (and future) versions of Windows as well:

  1. To download Lua 5.2.4, visit https://sourceforge.net/projects/luabinaries/files/5.2.4/.
  2. Click on the Tools and Executables link.
  3. On a 32-bit version of Windows, click the lua-5.2.4_Win32_bin.zip link to start downloading Lua. On a 64-bit version of Windows, click the lua-5.2.4_Win64_bin.zip link to start downloading.
  4. Once the file is downloaded, unzip the file. Unzipping the downloaded file should create four new files: lua52.dll, lua52.exe, luac52.exe, and wlua52.exe.
  5. Create a new folder inside C:\Program Files, and call this new folder LUA. Copy the four files you just unzipped into this directory.
  6. Rename lua52.exe to lua.exe. If your Windows installation is set up to hide file extensions, rename lua52 to lua:
  1. The path to Lua needs to be set up as an environment variable in Windows.
  2. Right-click on the Start/Windows menu button and select the System option.
  3. From the System window, select the Advanced Settings option.
  4. Having clicked the Advanced Settings option, you should now see the System Properties dialog. In this dialog, click on the Environment Variables... button.

  1. In the Environment Variables window, with the Path variable selected, click the Edit... button:
  1. Inside the Edit environment Variable window, click the New button and add C:\Program Files\LUA as a new path. Click the OK button to save changes and close this window. You can close all the windows we have opened up to this point.
  2. Lua should now be successfully installed on your computer. To verify the installation, you need to launch a new Command Prompt. You can launch Command Prompt by right-clicking the Windows Start/Windows button and selecting the Command Prompt item.
  3. In the newly opened Command Prompt, type lua -v. If everything is set up correctly, the command should print out the installed version of Lua (5.2.4):

Installing Lua on macOS

Follow these steps to install Lua 5.2.4 on macOS. These instructions are written for macOS High Sierra, but the steps are the same on previous (and future) versions of macOS as well:

  1. To download Lua 5.2.4, visit https://sourceforge.net/projects/luabinaries/files/5.2.4/.
  2. Click on the Tools and Executables link.
  3. Click on the lua-4.2.4_MacOS1011_bin.tar.gz link to start downloading Lua.

  1. Once the zip file has downloaded, unzip it. The archive should contain two files, lua52 and luac52:
  1. Create a new folder in your ~/Documents directory, and name this folder LUA. Move both lua52 and luac52 into this new directory:
  1. Rename lua52 to just lua.
  2. Launch a Terminal window. The Terminal app is located at /Applications/Utilities/Terminal.app. You can also simply type Terminal into the universal search on macOS.

  1. With the new Terminal window open, type sudo nano /etc/paths and hit Enter. You will be asked for your password; this is the password for your user account. The password will not show up as you type it. After the password is entered, nano will open; nano is a Terminal-based text editor. You should see something similar to the following window:
  1. You can navigate the type cursor with the arrow keys. Don't worry if your paths file (the file we are editing) already has text in it. We will be adding a new entry into this file; where in the file you add the new entry does not matter. On a new line, type ~Documents/LUA:
  1. Press Ctrl + X to exit nano. The program will ask you if you want to save the changes you have made to the file. Press Y to save changes.
  2. Nano will ask you to confirm the filename. Just hit Enter to accept the default path.

  1. In order for the changes made in the paths to take effect, you must restart the Terminal app. To do this, right-click on the Terminal icon in your macOS dock and select Quit. Then, launch a new Terminal window.
  2. In the new Terminal window, type lua -v. If everything is set up correctly, the Terminal should print out the installed version of Lua (5.2.4):

Installing Lua on Linux

Follow these steps to install Lua 5.2.4 on Linux. These instructions are written for Ubuntu Linux 16.04. The steps needed to install Lua are the same on Ubuntu Linux 12.04 and higher:

  1. The entire installation of Lua can be done using the command line, with the apt package manager.
  2. Open up a new Terminal and type sudo apt-get install lua5.2:
  1. Provide your password when prompted and wait for the installation to finish:
  1. Type lua -v. If everything is set up correctly, the Terminal should print out the installed version of Lua (5.2.4):
You have been reading a chapter from
Lua Quick Start Guide
Published in: Jul 2018
Publisher: Packt
ISBN-13: 9781789343229
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime