luabind - 将 C++ 转换成 Lua 再转回 C++。

我在luabind方面遇到了一个问题,或者至少我认为是个问题。

我有一个实体类,已经在lua中进行了注册。理想情况下,我想对其进行子类化并覆盖其函数。然后我想将其发送回C++并存储它。此外,我希望能够从存储的对象/指针的C++中调用其新函数。然而,我现在甚至无法让C++重新接受cEntity*类型的对象?在lua脚本中,我可以加载类、调用其变量和函数,我试图发送它到takeClass或takebOject,但它以空白类的形式出现,没有任何设置。例如,foo->name是""而不是"Entity1",id是0而不是1。你有什么想法吗?我已经在Google上搜索了至少一个星期,但没有理解这个问题,这完全阻碍了我的项目进展。

```

点赞
用户3778365
用户3778365
#include <iostream>
#include <string>
#include "luabind/luabind.hpp"

extern "C" {
#include "lauxlib.h"
#include "lualib.h"
}

class cEntity
{
 public:
  std::string name;
  int id;

  cEntity();
  ~cEntity();

  std::string getName() { return name; }
  void setName(std::string n) { name = n; }

  int getID() { return id; }
  void setID(int n) { id = n; }

  virtual void testFunction(){};
  static void luabindClass(lua_State* L);
};

//#######################################################################
// Test function
//#######################################################################
struct luaTest {
  void TakeClass(cEntity* foo)
  {
    std::cout << foo->name << std::endl;
  }

  void TakeObject(luabind::object foo)
  {
    cEntity* foobar = luabind::object_cast<cEntity*>(foo);
    std::cout << foobar->name << std::endl;
  }

  void luabindClass(lua_State* L)
  {
    //Somewhere else
    luabind::module(L)
        [
            luabind::class_<luaTest>("luaTest")    // < "Animation" how we want to name the Class in the Lua runtime
            .def(luabind::constructor<>())
            .def("TakeClass", &luaTest::TakeClass)
            .def("TakeObject", &luaTest::TakeObject)
         ];
    luabind::globals(L)["test"] = this;
  }
};

//#######################################################################
// Entiy Class
//#######################################################################

//#######################################################################
cEntity::cEntity()
{
  name = "NotSet";
  id = 0;
}

cEntity::~cEntity()
{
}

void cEntity::luabindClass(lua_State* L)
{
  luabind::module(L)
      [
          luabind::class_<cEntity>("cEntity")    // < "Animation" how we want to name the Class in the Lua runtime
          .def(luabind::constructor<>())            // < Binds the empty constructor
          .def_readwrite("name", &cEntity::name)
          .def_readwrite("id", &cEntity::id)
       ];
}

char const* script =
                 "entity = cEntity();\n"
                 "entity.name = \"Entity1\";\n"
                 "entity.id = 1;\n"
                 "\n"
                 "test:TakeClass(entity);\n"
                 "test:TakeObject(entity);\n";

int main() {
  lua_State* L = lua_open();
  luabind::open(L);

  cEntity::luabindClass(L);
  luaTest test;
  test.luabindClass(L);
  /* run the script */
  if (luaL_dostring(L, script)) {
    std::cout << lua_tostring(L, -1) << std::endl; // 输出错误信息
  }
  lua_close(L);
}

输出:

$ ccache clang++ -lluabind -llua -g3 ~/test.cxx && ./a.out
Entity1
Entity1

因此,在加载脚本时可能出现问题。 有任何错误信息输出吗?

(很抱歉我无法在 IRC 中回答这个问题。创建测试环境需要一些时间。)

2014-06-26 07:58:53