Sign in to follow this  
SiS-Shadowman

Passing a pointer to a class to LUA

Recommended Posts

SiS-Shadowman    359
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 this post


Link to post
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]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this