2014-06-26 53 views
-1

我有luabind问题,或者至少我希望它是一个问题luabind - C++到Lua到C++

我有一个香港专业教育学院与卢阿注册的实体类,

理想我想它的子类并覆盖其职能,从那里我希望把它送回来到C++,并将其存储

此外,我希望能够从C++从存储的对象调用它的新功能/指针然而

即时通讯目前正在努力甚至可以让C++获取类型cEn的对象tity * back?在LUA脚本 我可以加载的类,调用它的变量和函数,我试图把它 到takeClass或takebOject但它出来是什么也没有一个空白类就可以了

设置例如foo->名称是“”而不是“Entity1”和id是0而不是1

anyideas我做错了什么? ive在谷歌搜索至少一周现在 没有理解这个问题的运气,它完全停止我的项目进度 ?

//####################################################################### 
// 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 
} 
.... 
//####################################################################### 

回答

0

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回答这个问题。花了一段时间来创建测试ENV)

+0

当代码回答,请在代码应答器后在这里。为什么?因为如果X年Github处于关闭状态,阅读此文的人仍然可以得到答案; ) – DrakaSAN

+0

感谢您的快速回复,因为我不得不继续工作,因此我不得不继续工作(仍然是heh) um没有错误打印出来,我在takeClass和takeObject中添加了一个检查,以及do_file只是为了确保 有没有我应该使用的lua/luabind的特定版本?因为我运行了你的代码,并得到了和以前一样的东西?我知道我使用lua5.1,我从主站点抓住了luabind? – Saragan

+0

@DrakaSAN感谢您的编辑。 –