Question

I have a C++ application and I would like to design and offer Lua APIs for this application, there is some tool that can help me with that? Maybe there is a way to mark some method and expose them to the Lua API layer? For other languages I have seen tool that can generate APIs after parsing the code, there is something similar to this for Lua?

Was it helpful?

Solution

I truely appreciated the very lightweight approach of LuaBridge which consists in just 1 (ONE!) header file to include in your application

https://github.com/vinniefalco/LuaBridge

https://github.com/vinniefalco/LuaBridgeDemo

/** Declare LUA binding for this class
 *
 * @param global_lua
 */
void c_entity::lua_bind(lua_State* L) {
    getGlobalNamespace(L)
        .beginClass<c_entity>("c_entity")
            .addFunction("getSpeed",&c_entity::get_linear_speed)
            .addFunction("getName",&c_entity::get_name)
            .addFunction("getMaxSpeed",&c_entity::get_max_linear_speed)
            .addFunction("getAcceleration",&c_entity::get_max_linear_acceleration)
            .addFunction("getHull",&c_entity::get_hull)
            .addFunction("getArmor",&c_entity::get_armor)
            .addFunction("getShield",&c_entity::get_shield)
            .addCFunction("getStatus",&c_entity::getStatus)
            .addFunction("logTrace",&c_entity::log_trace)
            .addFunction("logInfo",&c_entity::log_info)
            .addFunction("logDebug",&c_entity::log_debug)
            .addFunction("logError",&c_entity::log_error)
        .endClass();
}

OTHER TIPS

Check out SWIG. Depending on your needs and how "clear" your C/C++ headers you can just feed entire .h files to SWIG or select functions/classes you want to export to Lua(like in this basic example):

%module example
%{
#include "example.h"
%}
int gcd(int x, int y);
extern double Foo;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top