lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi,
I see you got it working, but I did something similar a while back.

If you don't need to create instances of your class form the script , you can set an instance created in c++ global doing something like this:

Lunar<myClass>::Register( L );    // Register myClass
myClass* myPointer = new myClass();    // Create new instance
int index = Lunar<Account>::push( L, myPointer, false );    // Push to lua stack, tell lua gc to ignore it, by giving false

// Set as global
lua_pushvalue( L, index );                            // Push the stack index of the instance to stack
lua_setglobal( L, "myClassInstance" );        // Set as global with name "myClassInstance", removes the index from stack

You would then use it in the script like this:

myClassInstance:method();

Where method is a function defined in the myClass::methods[] table.

Note that you need to delete the instance yourself or give true when calling the push() function to have lua gc delete it.

- Ilkka