lua-users home
lua-l archive

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


Please, take note that what I posted was a toy example so it is bound to be broken.
I tried to show you how you can simply push an object to Lua.

So how do you get it from your script? Well, that dependes on how you *invoke* your script. 
It can be pushed on the stack before lua_call'ing it (or lua_pcall) and later retrieved with "select".
You can register a function that returns instances of your class, etc.

Let's see an example of the latter.

static int push_object (lua_State* L)
{
   Account* account = new Account;
   Lunar<Account>::push(L, account, true);

   return 1;
}

lua_pushcfunction(L, push_object);
lua_setglobal(L, "push_object");

So in your script you will do:

local account = push_object()

And now you have an instance of Account on your script. Note that when I called ::push, the last argument is "true". That indicates if what you have pushed is to be garbage collected. That basically means that you're in charge of storing that object somewhere in Lua or else it will be collected (and its destructor will be called).