4
\$\begingroup\$

I've implemented LuaPlus in my engine eventmanager successfully and really like the flexibility I gained. But I'm still not exactly where I want to be, because I can't link my c++ classes to a Lua class.

For example, I have a Actor class in C++, and I want to be able to create the same class in Lua and gain access to members with LuaPlus, but I can't figure how I can achieve that.

Is this actually LuaPlus built-in functionality, or do I have to write my own interface that exchanges data tables between C++ and Lua?

My current approach would be to fire an event in Lua that creates an new actor class in C++ code, and transfer its ID and the data I need to back to Lua. When I modify the data I send the modifications back to C++ code again, but I actually thought there's something in LuaPlus that exposes this functionality already.

\$\endgroup\$
6
  • \$\begingroup\$ This is why Lua sucks. \$\endgroup\$
    – DeadMG
    Commented Feb 25, 2011 at 0:09
  • 2
    \$\begingroup\$ i actually like it. could you provide why you dislike it, or any alternatives? \$\endgroup\$
    – cppanda
    Commented Feb 25, 2011 at 2:19
  • 1
    \$\begingroup\$ I'd also be interested in why you think Lua sucks. \$\endgroup\$ Commented Feb 25, 2011 at 3:56
  • 1
    \$\begingroup\$ The interoperation with C++ is truly terrible- the language was only designed to interop with C and it shows. Then, when you spend your life finally making the damn thing work, the language itself is very poor- hard to debug, limited powers from Lua, etc. For example, how come full userdata can have __gc but not tables or light userdata? It's a mystery! \$\endgroup\$
    – DeadMG
    Commented Feb 25, 2011 at 9:38
  • \$\begingroup\$ @DeadMG: Every language's interoperation with C++ is terrible (including C++ itself from different compilers) - that's usually C++'s fault, not the other language's. \$\endgroup\$
    – user744
    Commented Mar 17, 2011 at 16:14

2 Answers 2

1
\$\begingroup\$

I don't know how to achieve this with luaplus, but I have been using luabind and the behavior that you explains is very easy to program.

http://www.rasterbar.com/products/luabind.html

In my experience, LUA has incremented the workflow efficiency, especially on things like AI and things that change a lot in development.

For example, you could reprogram the AI on the fly, with the game in execution, reload the AI and see the changes. All this without recompile or launch the game again.

\$\endgroup\$
3
\$\begingroup\$

tolua++ uses all the metatable features to allow a very straightforward manipulation of your c++ types in lua.

For example, this declaration :


// tolua_begin
class SomeCppClass
{
public:
   SomeCppClass();
   ~SomeCppClass();

   int some_field;
   void some_method();
};
// tolua_end

Will automatically generate the lua bindings to allow this lua scipt :


#!lua
-- obj1 must be deleted manually
local obj1 = SomeCppClass:new() 
-- obj1 will be automatically garbage collected 
local obj2 = SomeCppClass:new_local()

obj1.some_field = 3 -- direct access to "some_field"
obj2:some_method() -- direct call to "some_method"

obj1:delete()

The advantage of this technique is that your lua code will ve very consistent with the relying C++ code. See http://www.codenix.com/~tolua/tolua++.html

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .