【问题标题】:LuaBridge and Member FunctionsLuaBridge 和成员函数
【发布时间】:2012-11-01 14:22:07
【问题描述】:

我今天刚刚下载了 LuaBridge,到目前为止对它非常满意。我注意到的一件事是,我能够规避将 lua_State 作为函数参数的正常要求。

我可以这样做:

//C++ files
void love(int i) {std::cout << i;}

luabridge::getGlobalNamespace(lua)
  .addFunction("love", love);

-- Lua file
love(8)

它会工作得很好,但如果我做任何事情:

//C++ files
struct Tester {
  int number;
  void MemFunction (int i) { std::cout << i;}
  static void Register(lua_State*); 
};

void Tester::Register(lua_State *lua) {
  luabridge::getGlobalNamespace(lua)
    .beginClass<Tester>("Tester")
    .addConstructor <void (*) (void)> ()
    .addData("number", &Tester::number)
    .addFunction("MemFunction", &Tester::MemFunction)
 .endClass();
}

--Lua file
  c = Tester()      -- works...
  c.number = 1      -- works...
  c.MemFunction(10) -- nothing!

我在文档中读到的任何内容都表明无法注册具有非 lua_State 参数的成员函数,并且我已经看到一些 LuaBridge 代码可以毫无问题地做到这一点。我在这里做错了什么?

【问题讨论】:

    标签: c++ class lua member-functions luabridge


    【解决方案1】:

    你必须使用方法调用语法

    c:MemFunction(10)
    

    我建议您使用newer version from github,它有大量文档。它还允许在输入参数和返回值方面具有一些额外的灵活性。

    【讨论】:

    • 作为一名本地 C++ 程序员并且编写的实际 Lua 代码很少,我“忘记冒号”的次数比我想记住的要多。我将在 LuaBridge 文档中添加一些内容。
    猜你喜欢
    • 2018-08-11
    • 1970-01-01
    • 1970-01-01
    • 2013-12-12
    • 2014-04-02
    • 2017-01-05
    • 2023-03-14
    • 1970-01-01
    相关资源
    最近更新 更多