lua-users home
lua-l archive

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

Ben Sunshine-Hill wrote:
I actually have to agree with the "manual binding" approach. However,
I also have to warn you: By asking on the Lua mailing list (a list
which has an inordinately large number of incredibly intelligent and
helpful people) you may see a sampling bias towards more flexible yet
difficult approaches. That, I think, is responsible for the gulf
between the advice you've received here and the advice you've received
on GameDev: the people here can bind an interface to Lua code with one
hand tied behind their backs. (I can't, of course, but I still
slightly prefer the manual approach for its flexibility.)

With respect to LuaBind and tolua++, I strongly encourage the latter
over the former. LuaBind is a heroic effort to overcome the limits of
C++ metaprogramming; but this has made it fragile and difficult to
debug. ToLua++ does not try to do things as "elegantly", and because
of this it is more robust and easy to tweak. Also, I seem to recall
that LuaBind has not been updated in awhile, and requires an older
version of Boost to function properly. I haven't taken a look at it in
a couple of months, though.

I am in the process of packaging up my Lua-C++ binding system, called Fusion. Our goal is to release this to the Lua commumity in early December. Creating a C++ object that is accessible to Lua is a simple matter of having your class look like this:

    class MyClass : public Fusion::Object {

        DECLARE_CLASS(MyClass, Object);

        Object* someFunction(double num);

To make someFunction() visible to Lua, you add this to the initializeClass() method (which every class needs to implement):

    registerFunction("someFunction", someFunction);

This does all the work of pushing params onto the lua stack and then getting the returned value (an Object* in this case) back onto the stack. I borrowed from toLua++ to perform this trick!

To use this new class in Lua, you introduce it into the Lua global namespace (or as a property of some other Lua object if you like:

    registerGlobalObject("MyClass", MyClass::newClassObject());

Now, in Lua you go like this:

    local obj = MyClass()
    local obj2 = obj:someFunction(2.75)

Fusion objects look like userdata objects to Lua. They have an embedded table, so you can go like this:

    obj.newfield = "hello"

Fusion objects follow the Javascript prototype delegation model, so you can go:

    MyClass.newtoo = 1.25

and then every instance of MyClass will have a newtoo property:

    if obj.newtoo == 1.25 then print("yes, it does") end

You can create objects that fit in this hierarchy in C++ as well:

    class MyClass2 : public MyClass {
        DECLARE_CLASS(MyClass2, MyClass);

And now you can go:

    local obj3 = MyClass2()

and it will have a newtoo property as well.

Anyway, the system is pretty simple and might fit your needs, if you can wait a little longer!

chris marrin                ,""$,          b`    $                             ,,.
                        mP     b'                            , 1$'
        ,.`           ,b`    ,`                              :$$'
     ,|`             mP    ,`                                       ,mm
   ,b"              b"   ,`            ,mm      m$$    ,m         ,`P$$
  m$`             ,b`  .` ,mm        ,'|$P   ,|"1$`  ,b$P       ,`  :$1
 b$`             ,$: :,`` |$$      ,`   $$` ,|` ,$$,,`"$$     .`    :$|
b$|            _m$`,:`    :$1   ,`     ,$Pm|`    `    :$$,..;"'     |$:
P$b,      _;b$$b$1"       |$$ ,`      ,$$"             ``'          $$
 ```"```'"    `"`         `""`        ""`                          ,P`
"As a general rule,don't solve puzzles that open portals to Hell"'