2

When I was working with Lua in C#, I would add attributes to the methods I wanted to register with Lua, something like this:

// place tile marker
[LuaExport("settilemarker")]
public void SetTileMarker(int x, int y)
{
    TileEditor.X = x;
    TileEditor.Y = y;
}

Then, I would use reflection at runtime to find all these functions and register them, automagically, with a short piece of code that didn't have to be updated for each new function. This could also be used to automatically build a list of all registered commands easily etc.

My question is, is there any way to do this with Lua in C++? Like some kind of macro, or something. The information doesn't have to be available at runtime, I just want the convenience of marking each function right where it's defined and have them all be registered automatically, rather than having one long section somewhere of lua_register(..)s.

I've already found some tools that appear to be capable of generating code for this, but I was hoping there might be a way that doesn't require any extra manual work besides hitting Build. If not, I'll have to investigate those tools further after all.

Using Visual Studio 2010, by the way.

3 Answers 3

2

C and C++ do not have reflection. Therefore, any tool you use will require at least some manual work. You're going to have to type something for every function you want to export, or you're going to have to put the prototypes into a special location or something.

There is a list of Lua binding utilities available. Some of them are semi-automated, including parsers that can read some subset of C++ and semi-automatically generate binding code for them. SWIG, toLua, and toLua++ are among these. Others require more work, but make it a bit easier to explicitly declare behavior (like ownership, etc).

Sign up to request clarification or add additional context in comments.

1 Comment

I know they don't have reflection - I was simply pointing out that it makes it really easy to auto-register functions, and I was hoping there might be some similarly easy way, not necessarily done at runtime, just less tedious work for the programmer. But yeah, using those utilities seems to be the way to go.
1

The syntax you want to write is similar to what you'll write if you use Luabind to export your C++ functions to Lua.

I used to use the Lua C API directly to integrate Lua with C++ but, since I discovered Luabind, I never do that.

In your program initialization, you might have something like this.

#include <lua.hpp>
#include <luabind/luabind.hpp>

lua_State * L = luaL_newstate();
luaL_openlibs(L);
luabind::open(L);
luabind::module(L) [
    luabind::def("settilemarker", &SetTileMarker)
];

If SetTileMarker is not a free function but a method of class Foo, then you would do this instead.

luabind::module(L) [
    luabind::class_<Foo>("foo")
        .def("settilemarker", &Foo::SetTileMarker)
];

This isn't as nifty as being able to distribute your registration all over the place but I find it easier and more syntactically approachable than using the Lua C API directly.

Comments

0

Have you looked into SWIG I have used it, and while a little complicated, it does automate many things for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.