Hello,
anyone can suggest me a simple lua wrapper with clean code?

I want to call from c++ lua functions but I dont really get it..

I only know a little LUA and C++.

Example lua function I have:

Get the level:

function Lv(a)
    local lv = GetChaAttr( a , ATTR_LV )
    return lv 
end 

Get life points:

function Lp(a)
    local lp = GetChaAttr( a , ATTR_LP )
    return lp 
end

How can I create now a "GetChaAttr" function in c++ which fetch the player level/life points?

ATTR_LV = to get level
ATTR_LP = to get life points

Any idea which lua wrapper would be the best for me?

I always prefered the LUA Wrapper from the LUA.org, it's quite simple...Here's an example:

static void AddLUAFunction(lua_State* L, char* FuncName, lua_CFunction Func)
{
    //Adds a function to the lua global table
    //FuncName is the name of the function(in the LUA code)
    //Func is the function in C++
    lua_pushcclosure(L, Func, 0);
    lua_setglobal(L, FuncName);
}



static int ExecuteLUAFunction(lua_State* L, char* FuncName)
{
    //FuncName = LUA Function
    int iErr;
    if ((iErr = lua_pcall (L, 0, LUA_MULTRET, 0)) == 0)
    { 
         lua_pushstring (L, FuncName); //Pushes the function name
         lua_gettable (L, LUA_GLOBALSINDEX);   //Gets the function in the global table
         lua_pcall (L, 1, 0, 0); //Call the function expecting 1 result and 0 params
         int result = lua_gettop(L); //Gets the result in the top of the stack
         printf("Result In C: %d \n", result);
         return result;
    }
}



static int printMessage(lua_State* L)
{
    const char* msg = lua_tostring(L, 1); //Gets the first parameter
    const char* smsg = lua_tostring(L, 2); //Gets the second parameters
    printf(msg, smsg);
    return 0; //Returns 0 parameters
} //AddLUAFunction(L, "printMessage", printMessage);

//In LUA:
//printMessage("Testing is: %s", "OK");

For more details check lua.org

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.