Home | About | Forum | Manual | Download | Screenshots

Lua Scripting in AGen

Lua is a general purpose scripting language. Entire books have been written about it. The purpose of this article is to highlight the peculiarities of writing games in AGen while using Lua. It is presumed that you are already familiar with Lua itself and if not, please refer to the official Lua 5.1 documentation.

Working with components

As you probably know, the primary data structure in Lua is an associative array, called table. AGen is object-oriented and all of its components are represented as tables in Lua. Each component exports its properties as well as its functionality through the table. To create a new AGen component, you need to call its "pseudo constructor". I have used the word "pseudo" since Lua doesn't have constructors in the C++ sense. Basically, you will be calling a global function within Lua which returns a reference to the component of the respective type. The reason why I liken these functions to constructors is because they are named like the type of components they return. Now that I've confused you enough, the following example should clear things up. It shows the creation of a "point" object using two different constructors... or rather "pseudo constructors"

my_point = Point ( )
my_point2 = Point ( 300, 400 )

Properties and methods

The different types of AGen components have their own unique properties. The point component for example has both an "x" and "y" property. These properties belong to the point object itself and (in the case of the point) their value can not be anything other than a number. Apart from properties, most AGen components also have methods or as C++ programmers would say, "member functions". The methods of a component are always called using the colon ( ":" ) operator. On the other hand, its properties can be accessed either via the dot ( "." ) operator or square bracket delimiters ( "[' ']" ).

my_point.x = 100
my_point['y'] = 200

dist = my_point:distance ( my_point2 )

The assignment operator

The assignment operator ( "=" ) in Lua is so easy to mishandle that it deserves special attention. Let us say that you have two separate tables "stored" in two variables and you tried to assign one of them to the other. Lua will simply turn your first, destination variable into a reference pointing to the second, source variable. This rule applies for AGen components too, since they are tables themselves. So how can you then duplicate an AGen component? The easiest way to do so is by using the "assign" method, which is implemented in all AGen components.

my_point = my_point2 -- reference

my_point:assign ( my_point2 ) -- copy