我已經從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
有括號?與他們有沒有這樣的操作員的錯誤。
「應該state = StateManager有括號?」我不知道;你想要它做什麼?你想創建一個StateManager對象嗎?或者你想使用一個已經存在的對象嗎?我無法讀懂你的想法;你需要更清楚你想要達到的目標。 – 2012-07-14 18:58:59
@NicolBolas'globals(state)[「StateManager」] = Root :: Get()。GetState();'我在這裏給'StateManager'設置了一些指針,這就是爲什麼我認爲我沒有括號寫它 – Ockonal 2012-07-14 19:38:17