lua-users home
lua-l archive

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


I really like luabind (http://luabind.sourceforge.net).  It basically does
what tolua does, but uses template metaprogramming and some binding
functions, rather than parsing a "cleaned header" which generates code.  You
could do it yourself (as the other posts describe), but (for my purposes at
least) why bother if a compiler can make the same thing for you via
templates.

You would simply do this:

void register_objects( lua_State* L )
{
	using namespace luabind;
	module( L )
	[
		class<vector3>("vector3")
			// bind vector3 properties / methods for use in lua
		,
		class_<NPC>( "NPC" )
			.def( constructor<>() )
			.property( "health",   &NPC::Health )
			.property( "position", &NPC::position )
			.def( "Move",          &NPC::Move )
	];
}

Now you can simply use it in Lua:

npc = NPC();
npc.health = 1;
npc.position = vector3(0,0,0);
Npc:Move();

-Evan


-----Original Message-----
From: Jose Marin [mailto:jose_marin2@yahoo.com.br] 
Sent: Wednesday, December 31, 2003 9:39 AM
To: lua@bazar2.conectiva.com.br
Subject: Fastest way of mapping a C++ object (or C struct) to Lua


Hi.

What´s the fastest way of mapping a C++ object (or C
struct) to Lua?


In C++ 

class NPC{
 int health;
 vector3 position;
 // other attributes

 void Move();
 // other methods
};


In LUA

 enemy1.health = 100
 enemy1.Move()



If is there some way of map fast a C struct to a LUA
variable, this will be enough.

Note:
 I wont use tolua, great tool, but I want to learn how
to do this myself


______________________________________________________________________

Conheça a nova central de informações anti-spam do Yahoo! Mail:
http://www.yahoo.com.br/antispam