lua-users home
lua-l archive

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


Check out the "Simple C++ Binding" on the wiki.
http://lua-users.org/wiki/SimpleCppBinding


Also, in short plain English... (well almost plain)

Make a Lua table, add your C++ functions to it as methods, (include a __gc
method etc.), set it's __index method to the table itself.  When creating
userdata's for your object instances set this table as their metatable.  The
C++ functions you use will need to be static, from in these functions
themselves use lua_userdata_cast (or similar) to get the userdata instance
(to map to your C++ object instance via contained pointer or directly as the
address depending on your preference.  Use that pointer to call a non-static
member function.  Its very simple to write a macro that generates the static
version of the member function to call a particular non-static version.

Properties (such as your "enemy1.health = 100") can also be accomplished
with just a little more work.  For this you will probably want to write up a
pair of functions to act as the userdata's __index and __newindex methods.
I recommend keeping a pair of tables within the metatable for use with this.
One to hold a set of "set" functions and one to hold a set of "get"
functions.  From __index check the table of "get" functions, if you find one
then the requested member is a property so call the "get" function, if it is
not found then look it up in the main metatable as a "normal" method.
Similar for __newindex.  Again you can make macros to aide in this (though
its a bit less obvious and is definitely more time consuming).

For expediency you could make your own function that takes an array of
structs and generates a metatable along with its associated property set/get
tables.
Once you get into the "properties" rather than just methods this gets a bit
involved and its not terribly time efficient to do yourself, but it is a
good exercise for working with Lua and you can write up and maintain as much
or as little as you need for repeated use in your projects.  I have to admit
that I've experimented along these lines, but have never thoroughly tested
my work or compared it to what is done by tolua or other bindings.





-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br]On Behalf Of Jose Marin
Sent: Wednesday, December 31, 2003 8: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