Luna Four

lua-users home

LunaV4 is an extension of the existing LunaWrapper. It brings significant changes but should still be backwards compatible.



The code has fairly terrible indentation, but it is commented. You can find it at LunaFourCode. If you do not have luaL_testudata, you'll also need to include it. It can also be found at LunaFourTestUserdata.

Usage - Lua Modifications

To use properties, you need to create a __setindex and modify the __index meta operators. __setindex needs to be called even if the property already exists, and __index needs to be called even if the property exists.

Usage - Setting Up

To use Luna, you must declare the following static variables in C++ classes that you want to wrap:
// declaration
static const char *className;
static const Luna < T >::FunctionType Functions[];
static const Luna < T >::PropertyType Properties[];

bool isExisting; // This is used by Luna to see whether it's been created by createFromExisting.  Don't set it.
bool isPrecious; // This is used to tell Luna not to garbage collect the object, in case other objects might reference it.  Set it in your classes constructor.

// implementation
const char *T::className = "YourClassNameInLua";
const Luna < T >::FunctionType T::Functions[] = {
	{"myFunction", &T::updateAll},	
const Luna < RPhysicsManager >::PropertyType RPhysicsManager::Properties[] = {
	{"myProperty", &T::getProperty, &layer::setProperty },
Replace T with your C++ class name.

Usage - Declaring Functions

Your functions must have the form:
int myFunction(lua_State* L)

Usage - Declaring Properties

Properties are more complex than functions, and require two C++ functions; one for getting, and one for setting. Both C++ functions have the same declaration type as a function. An example of get and set functions is outlined below:
int T::getProperty(lua_State* L)
	return 1;

int T::setProperty(lua_State* L)
	myCPPClassVar = lua_tonumber(L,-1);
	return 0;

Usage - Metaoperators

You can declare metaoperators for your class. Metaoperators have the same declaration as a function. If we wanted to declare the equals metaoperator, we would add:
{"__eq", &T::myComparisonFunction},
to our class (see setting up). The object on the left is stored at position 2 on the stack, and the object on the right is stored at position 3. (Position 1 is reserved for the self object in functions).

Usage - Initalizing our class

In order for the user to be able to initalize an instance of our class, you need to call:
Luna < core::RColor >::Register(L,"Namespace");
somewhere before your dofile or dostring. You can replace Namespace with the namespace you want to load the class into, or leave it blank to put it into the global space. Namespace must be previously defined as a table. When creating classes under namespaces, they can be initalized like:
myobject = Namespace.MyClass();

RecentChanges · preferences
edit · history
Last edited January 11, 2009 3:40 am GMT (diff)