Lua is a fast, powerful, lightweight, embeddable scripting language. It was designed around being portable and easy to use. Lua is used around the world in various applications, including CGI webpages, powerful configuration files, and a language for creating extensions to other programs, especially games, such as Roblox, Garry’s Mod, and the ComputerCraft mod for Minecraft.
Download software: The Lua programming language interpreter. (Download source for compilation from http://www.lua.org/download.html OR download installer package for Windows from https://code.google.com/p/luaforwindows/)
Cost: Free (Licensed under the MIT License)
Instructions to setup software: Compile source code via the makefile or run installer package for Windows.
Hardware requirements: Any platform that has an ISO/ANSI C compiler, some others.
Operating systems supported: Any that are an executable target from any ISO/ANSI C compiler, some others.
Difficulty level: 2 out of 5
1. Create a folder on your hard drive – call it “Lua Demo”.
2. Copy this Lua code and paste into your favorite text editor. Windows notepad works fine.
-- A Lua program. -- Double hyphens indicate comments, which aren't actually run as code and are notes for other programmers. --[[ if you use the double hyphens and then double square brackets, you can have comments that span multiple lines, as long as you end it with closing brackets! ]] -- Write a greeting onto the screen io.write("Hi there! What's your name? ") -- Create a global variable called 'name', giving it a value assigned from keyboard input name = io.read() --[[ Write a message on the screen, using the value within name. The double dots (..) tell Lua to concatenate (or combine) the string with the value from name. The "\n" tells Lua to start a new line ]] io.write("Hello, "..name.."! I'm your computer!\n") io.write("Let's do some math.\nYou give me two numbers, and I'll add them up.\n") -- Read in a value into number1 io.write("What's the first number? ") number1 = io.read() --[[ Check that number1 really is a number. The tonumber() function will return a value that will cause the logical while statement to succeed and continue if the variable is a number. Otherwise, continue to ask for a new value until it is a number. ]] while not tonumber(number1) do io.write("That's not a number!\n") io.write("What's the first number? ") number1 = io.read() end -- Do the exact same thing with a new variable called number2. io.write("What's the second number? ") number2 = io.read() while not tonumber(number2) do io.write("That's not a number!\n") io.write("What's the second number? ") number2 = io.read() end -- Add up the numbers answer = number1 + number2 -- Display the final result and a good-bye io.write(number1.." + "..number2.." = "..answer.." !\n") io.write("It was great talking to you, "..name.."!\n") io.write("See you later!\n")
3. Save the File as “Lua-Example.lua” in the new folder you created in step 1.
4. Double click on the Lua file to launch a Lua interpreter, if you installed for Lua for Windows package. Otherwise, use the command line to run Lua. (Enter ‘lua Lua-Example.lua’ in the command line)
Enter you name and a couple numbers and see what happens! Try several different names and numbers.
1. Change the math operation that the computer uses for the numbers.
2. Change the wording of the phrases.
3. Add another text input –- ask for a third number.
Reference Manual for Lua 5.2: A full manual for the Lua language.
Programming in Lua: A book written by one of the Lua language designers that covers many different topics about Lua.
www.lua-users.org: A user-created wiki about Lua containing many tutorials and other documents.
RESOURCES, TIPS, TRICKS AND HINTS
Notepad++ is a great free text editor designed specifically for programmers. Check it out here.