我已经从 C++ 导出到 lua 这样的基类:
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 类型的对象并将其推送到 StateManager 中:
-- 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)
当然,这是行不通的。如果 IState 类型的对象,我不知道该怎么说 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
示例后:
class 'MainState' (Scene.IState)
function MainState:__init()
Scene.IState.__init(self, 'MainState')
end
...
state = StateManager
state:Push(MainState())
错误:“IState”类中没有静态“__init”
并且应该state = StateManager
有括号?对于他们来说,没有这样的运营商是错误的。