Sign in to follow this  

Calling C++ Functions from Lua, complex paramater types?

This topic is 3314 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, I'm currently playing with Lua in C++, I can call Lua functions from C++ and C++ functions from Lua... as long as they expect bools, ints, floats and strings. But what is the best way for my Lua script to call a C++ function that expects (for example) a 2D Point, a pointer to a Goblin and an array of pizza stucts as parameters. Also what if i had to create and instantiate these objects in my Lua script before passing them in as parameters? is that possible in the script alone? or do I need to write a lua wrapper for some kind of factory in my C++ that chucks out pointers to said objects, call that from my script and then use the returned pointer as userdata in my parameter? (i'm still trying to get my head round userdata and metatables) Previously I've used C# and LuaInterface to do such things and it was quite easy... though in C++ just using Lua it seems not quite so simple. Thanks.

Share this post


Link to post
Share on other sites
There are several libraries you can use: swig, luabind and tolua++ are very popular.
For a comparison see this thread.
Since the last update luabind works again with the latest lua version without any modifications. It has very good documentation and it's easy to use. You bind classes like
module(L)
[
class_<testclass>("testclass")
.def(constructor<const std::string&>())
.def("print_string", &testclass::print_string)
];

luabind has also some helper functions to call lua functions from c++ and using lua threads.

Swig is also an interesting library, It can generate bindings for many different languages. To create a binding you need to write an interface file and then call swig to generate the c/c++ code. An Interface file looks like this:
%module tree
%{
#include "tree.h"
%}

class Tree {
public:
Tree();
~Tree();
void insert(char *item);
int search(char *item);
int remove(char *item);
static void print(Tree *t);
};


(I have not used tolua++.)

Some tips:
*luabind has very complicated template bases headers, the result is long compilation time but you can use a precompiled header to speed up the compilation.

*Most IDEs support custom build steps, you can easily configure the ide to call swig to generate the cpp files and compile them.

Share this post


Link to post
Share on other sites
Quote:
Original post by Guns Akimbo
Just to clarify: are you suggesting these libraries because what i'm trying to do is too complex without them?

Yes, whatever those libraries do can also be done with Lua's C API but it may require much more work.

Share this post


Link to post
Share on other sites

This topic is 3314 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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