I think I got it to work, finally, I feel It didnt even worth thou, so much time spent on hat .__.
//------------------------------------------------------------------------
// Lua Binding Stuff
//------------------------------------------------------------------------
static const char className[];
static Lunar<LuaAIAgent>::RegType methods[];
static LuaAIAgent* currentAgent;
static int push_object (lua_State* L){
Lunar<LuaAIAgent>::push(L, currentAgent, true);
return 1;
}
And then, on the constructor of my class I init the static pointer to the this pointer:
//------------------------------------------------------------------------
// ctor
//------------------------------------------------------------------------
LuaAIAgent( const char image_p, COLOR color_p, const Coord & pos_p )
: BulletAIAgent(image_p, color_p, pos_p){
...
currentAgent = this;
};
Then theres this method:
void InitLuaScript( const char * szLuaScriptFilename_p ){
m_szLuaScriptFilename = szLuaScriptFilename_p;
// create a Lua state and register the exposed methods
m_pLua = lua_open();
luaL_openlibs(m_pLua);
Lunar<LuaAIAgent>::Register( m_pLua );
lua_pushcfunction(m_pLua, push_object);
lua_setglobal(m_pLua, "push_object");
// compile script
if( luaL_loadfile( m_pLua, m_szLuaScriptFilename ) != 0 ){
throw std::exception( "problem loading Lua script" );
}
// execute the file
int err = lua_pcall( m_pLua, 0, 0, 0 );
if( err != 0 ){
// 2 = runtime error
// 4 = mem alloc error
throw std::exception( "problem running Lua script" );
}
...
And on the lua script:
b = push_object()
printf(b:Lua_GetHP())
=) thanks a LOT
On Mon, Nov 12, 2012 at 11:00 AM, Ignacio Burgueño
<iburgueno@gmail.com> wrote:
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).