c++ to lua to c++

c++ to lua to c++

本文关键字:c++ to lua      更新时间:2023-10-16

我有一个luabbind问题,或者至少我认为这是一个问题

我有一个实体类,我注册了lua,

理想情况下,我想子类化它并覆盖它的函数,从那里我想把它发送回c++并存储它

另外,我希望能够从c++中从存储的对象/指针

调用它的新函数

然而,我目前正在努力甚至让c++采取cEntity*类型的对象?在lua脚本中,我可以加载类,调用它的变量和函数,我试着发送它来调用takeClass或者takebOject但是它会以空白类的形式出现

例如foo->name是"而不是"Entity1", id是0而不是1

你知道我做错了什么吗?我在谷歌上搜索了至少一个星期没有运气理解这个问题,它完全停止了我的进步在我的项目上?
//#######################################################################
// Test function
//#######################################################################
void luaTest::TakeClass(cEntity* foo)
{
    cout << foo->name << endl;
}
void luaTest::TakeObject(luabind::object foo)
{
    cEntity* foobar = luabind::object_cast<cEntity*>(foo);
    cout << foobar->name << endl;
}
void luaTest::luabindClass(lua_State* L)
{
    //Somewhere else
    module(L)
        [
            class_<luaTest>("luaTest")
            .def(constructor<>())
            .def("TakeClass", &luaTest::TakeClass)
            .def("TakeObject", &luaTest::TakeObject)
        ];
    globals(L)["test"] = this;
}

//#######################################################################
// Entiy Class
//#######################################################################
class cEntity
{
public:
    string name;
    int id;
    cEntity();
    ~cEntity();
    static void luabindClass(lua_State* L);
};
//#######################################################################
cEntity::cEntity()
{
    name = "NotSet";
    id = 0;
}

cEntity::~cEntity()
{
}
void cEntity::luabindClass(lua_State* L)
{
    module(L)
        [
            class_<cEntity>("cEntity") 
            .def(constructor<>()) 
            .def_readwrite("name", &cEntity::name)
            .def_readwrite("id", &cEntity::id)
        ];
}

//#######################################################################
// Lua File
//#######################################################################
entity = cEntity();
entity.name = "Entity1";
entity.id = 1;
test:TakeClass(entity);
test:TakeObject(entity);
//#######################################################################
//#######################################################################
// main
//#######################################################################
....
/* run the script */
if (luaL_dofile(L, "avg.lua")) {
    std::cout << lua_tostring(L, -1) << std::endl; // Print out the error message
}
....
//#######################################################################

https://gist.github.com/take-cheeze/7264dbf1ea6e08a2d24a

#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; // Print out the error message
  }
  lua_close(L);
}
印刷:

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

所以在脚本加载可能有问题。是否打印了任何错误信息?

对不起,我不能在IRC中回答这个问题。创建测试环境需要一些时间