Passing a pointer to a class to LUA

Recommended Posts

I've a problem: I've tried out tolua++ to have classes in LUA, but the extra building process and the extra cleaned header file just doesn't feel comfortable. So I just downloaded luna, wich seems to be easier for me. But I'm not shure how I can pass a pointer to a class from c++ to lua. I've got a class CNPC that gets exported to lua with luna. Currently, I'm just exporting one function (along with ctor/dtor). But calling GetPlayer() from within lua returns "test.lua:3: attempt to index global 'Player' (a userdata value)
--test.lua programm
Player = GetPlayer();
Player:test();

int GetPlayer( lua_State *L )
{
int n = lua_gettop( L );

if( n != 0 ) {
lua_pushstring(L, "Incorrect argument to 'GetPlayer'");
lua_error(L);
}

lua_pushlightuserdata( L, g_pPlayer );

return 1;
}


What do I need to do?

Share on other sites
Aph3x    288
ed: hmm metamethods eh?

[Edited by - Aph3x on August 15, 2007 3:48:41 AM]

Share on other sites
Merick Zero    151
*edit*

Gah, I had this whole big post written out, but when I hit the reply button my browser hiccuped. When I was finally able to get back in here I found that my post was empty. I don't really feel like typing it all out again, sorry. However, if you download the lua player source code: http://luaplayer.org/staticpages/index.php?page=20070708120150241

Take a look at the macros in the luaplayer.h and how they're used in luagraphic.cpp to see an example of how to set metamethods to userdata

[Edited by - Merick Zero on August 14, 2007 7:36:03 PM]