将另一个类的C++静态函数绑定到Lua。

我的问题很简单:我有一个在extendedgamefunctions类中的函数:

在头文件中:

#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*);
        };

代码如下:

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

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

我想在另一个名为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");
        }

虽然另一个类中的函数是静态的,但我得到了这个错误:

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

我不想为了获取messagewindow而在gamefunctions中添加附加函数(除非我没有其他选择),因为我直接将extendedgamefunctions写成不会导致无限字符字符串的形式。

备注: 几乎忘了:Lua 5.2和c++11。

点赞
用户1632532
用户1632532

lua_CFunction 被定义为 typedef int (*lua_CFunction) (lua_State *L);,因此您需要将 void A::messagewindow(lua_State *L) 更改为 int A::messagewindow(lua_State *L)

2015-11-06 14:19:49