【问题标题】:Bind c++ static functions from another class to Lua将 c++ 静态函数从另一个类绑定到 Lua
【发布时间】:2016-02-06 19:34:28
【问题描述】:

喂!

我的问题很简单:我在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

我不想在游戏函数中添加一个加号函数来获取消息窗口(除非我没有其他 coises),因为我直接编写了扩展的游戏函数,而不是制造一个无穷无尽的字符串怪物。

编辑: 差点忘了:Lua 5.2 和 c++11

【问题讨论】:

    标签: c++ lua bind lua-api static-functions


    【解决方案1】:

    lua_CFunction定义为typedef int (*lua_CFunction) (lua_State *L);,所以需要将void A::messagewindow(lua_State *L)改为int A::messagewindow(lua_State *L)

    【讨论】:

    • 谢谢。你是我的英雄:)
    • 都在错误信息中 :P 还需要注意的是,你返回的 int 是你作为 lua 端函数的返回值压入堆栈的值的数量。
    • 我想通了。是的,就是我。我编写了大量的代码,并搜索了几个小时拼写错误的“==”。但没有更大的错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-14
    • 2012-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-19
    • 1970-01-01
    相关资源
    最近更新 更多