2014-06-26 24 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感謝您的編輯。 –