2012-07-14 49 views
0

我已經從C++導出到LUA這樣的基類:創作LUA對象的從C++基類

class IState 
{ 
    public: 
     virtual ~IState() { } 

     virtual void Init() = 0; 
     virtual void Update(float dSeconds) = 0; 
     virtual void Shutdown() = 0; 
     virtual string Type() const = 0; 
}; 

// Wraper of state base class for lua 
struct IStateWrapper : IState, luabind::wrap_base 
{ 
    virtual void Init() { call<void>("Init"); } 
    virtual void Update(float dSeconds) { call<void>("Update", dSeconds); } 
    virtual void Shutdown() { call<void>("Shutdown"); } 
    virtual string Type() const { return call<string>("Type"); } 
}; 

導出代碼:

 class_<IState, IStateWrapper>("IState") 
      .def("Init", &IState::Init) 
      .def("Update", &IState::Update) 
      .def("Shutdown", &IState::Shutdown) 

接下來的部分:我有StateManager與功能:void StateManager::Push(IState*)它的出口:

 class_<StateManager>("StateManager") 
      .def("Push", &StateManager::Push) 

現在,我想要在Lua中創建IState類型的對象,並將其推入St ateManager:

-- Create a table for storing object of IState cpp class 
MainState = {} 

-- Implementatio of IState::Init pure virtual function 
function MainState:Init() 
    print 'This is Init function' 
end 

function MainState:Update() 
    print 'this is update' 
end 

function MainState:Shutdown() 
    print 'This is shutdown' 
end 

state = StateManager 
state.Push(MainState) 

當然,這是行不通的。我不知道怎麼說,如果MainState型即將狀態置的對象:

class 'MainState' (Scene.IState) 
function MainState:__init() 
    Scene.IState.__init(self, 'MainState') 
end 
... 
state = StateManager 
state:Push(MainState()) 

錯誤:

error: No matching overload found, candidates: void Push(StateManager&,IState*) 

UPD

module(state, "Scene") [ 
     class_<StateManager>("StateManager") 
      .def("Push", &StateManager::Push), 

     class_<IState, IStateWrapper>("IState") 
      .def("Init", &IState::Init) 
      .def("Update", &IState::Update) 
      .def("Shutdown", &IState::Shutdown) 
    ]; 

    globals(state)["StateManager"] = Root::Get().GetState(); // GetState() returns pointer to obj 

例如無後類'IState'中的靜態'__init'

而且應該state = StateManager有括號?與他們有沒有這樣的操作員的錯誤。

+0

「應該state = StateManager有括號?」我不知道;你想要它做什麼?你想創建一個StateManager對象嗎?或者你想使用一個已經存在的對象嗎?我無法讀懂你的想法;你需要更清楚你想要達到的目標。 – 2012-07-14 18:58:59

+0

@NicolBolas'globals(state)[「StateManager」] = Root :: Get()。GetState();'我在這裏給'StateManager'設置了一些指針,這就是爲什麼我認爲我沒有括號寫它 – Ockonal 2012-07-14 19:38:17

回答

2

你不能只在Luabind扔桌子。如果你打算從Luabind定義的類中派生出來,你必須遵循Luabind的規則。您必須create a Lua class with Luabind's tools,從您的IState類派生。這將是這樣的:

class 'MainState' (IState) --Assuming that you registered IState in the global table and not a Luabind module. 

function MainState:__init() 
    IState.__init(self, 'MainState') 
end 

function MainState:Init() 
    print 'This is Init function' 
end 

function MainState:Update() 
    print 'this is update' 
end 

function MainState:Shutdown() 
    print 'This is shutdown' 
end 

state = StateManager() 
state:Push(MainState()) 

此外,注意到的變化到最後兩行。具體來說,StateManager如何被調用,而不是簡單地設置爲state)。另外,您如何使用state:而不是state.。我不知道該代碼如何在你的例子中起作用。

+0

Thanks for答覆。你能否看看更新? – Ockonal 2012-07-14 18:39:44