What is Lua?

Lua is an exstensible scripting language designed to be embedded into other programs. It is straightforward to add features to the language which are specific to your application. As well, it is rather quick for an interpreted language written entirely in portable ANSI C. (Rather, it is compiled to bytecode to be run by the Lua-specific virtual machine written entirely in portable ANSI C.)

Lua is built on the idea of being as syntactically simple and orthoganal as possible, while providing "meta-mechanisms" to allow for the implementation of various language features that other languages would make explicit. Lua can be programmed in an object-oriented way. Lua can also be programmed in a functional way. (Functions in Lua do not explicitly have names; a "function definition" in Lua is simply syntactic sugar for assigning a lambda function to a variable.) Lua can be used as a simple configuration file.

Lua has been used in a number of successful commercial games; it's interesting to look over the list of projects which use Lua available on the Lua site.

Basic Lua Concepts

Everything in Lua is built around the concept of the "table". A table, in this case, is basically a hash table; though it may also contain a sequential array alongside of it. This is a major source of both Lua's speed and flexibility. You can access a table entry by any type of key -- not just strings, but also things like table references, or black-box "userdata" given to you by a C function you've added to your interpreter.

Here's some random example code:

foo = {};        -- create a table and place it in a global variable named foo
foo["bar"] = 10  -- the semicolon at the end of a statement is optional, 
                 -- in most cases, Lua can determine the end on its own
foo.bar = 10;    -- this statement is exactly equivalent to the previous one.

-- Let's do something OO, at the simplest level
foo["increment"] = function(self, x) -- functions are created without names and assigned to variables
  self["bar"] = self["bar"] + x;
end
-- Now we'll do the exact same thing with some nice syntactic sugar
function foo:increment2 (x)
  self.bar = self.bar + x;
end

-- Let's call the function with and without syntactic sugar
foo:increment(5);
foo.increment2(foo, 5);
foo["increment"](foo, 5);

What's Bad?

Because Lua began as a configuration language, there are some odd holdovers which can make using it for a programming language a bit error-prone. With each new version, many of these problems are slowly being addressed.

Web Resources


CategoryLanguage

LuaLanguage (last edited 2008-07-09 05:47:57 by localhost)