Search code examples
c++luabindlua-apistatic-functions

Bind c++ static functions from another class to Lua


Hy!

My problem is simple: I have a function in the extendedgamefunctions class:

In the header:

#include "Gameitems.h" 
extern "C" {
    #include "lua.h"
    #include "lualib.h"
    #include "lauxlib.h"
};

extern std::vector<std::vector<Gameitems>> items;

     class A
        {
             A(); 
             ~A();

        public:
              static void messagewindow(lua_State*);
        };

and the code is:

  Functions extfunctions;
    A::A()
    {}
    A::~A()
    {}

    void A::messagewindow(lua_State *L)
    {
       string message =lua_tostring(L,0);
       extfunctions.Messagewindow(message);
    }

and I want to bind in an another function called Gamefunctions :

#include "Externedgamefunctions.h"

A egfunctions;
lua_State* L;
        void Gamefunctions::luainit()
        {
            L = luaL_newstate();

            /* load Lua base libraries */
            luaL_openlibs(L);
            lua_pushcfunction(L,&A::messagewindow);
            lua_setglobal(L, "messagewindow");
        }

Rather the funtion from another class is static, I get this error:

Error   5   error C2664: 'lua_pushcclosure' : cannot convert parameter 2 from 'void (__cdecl *)(lua_State *)' to 'lua_CFunction'    C:\Users\Bady\Desktop\MY Game\basicconfig\BlueButterfly\BlueButterfly\Gamefunctions.cpp 170

I dont want to make a plus funtion into the gamefunctions to get the messagewindow (unless I have no other coises) because I directly write the extendedgamefunctions to not make an endless stringmonster.

Edit: Almost forgot: Lua 5.2 and c++11


Solution

  • lua_CFunction is defined as typedef int (*lua_CFunction) (lua_State *L);, so you need to change void A::messagewindow(lua_State *L) to int A::messagewindow(lua_State *L).