从C++基类创建Lua对象。
2012-7-14 18:49:1
收藏:0
阅读:209
评论:1
我已经将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; }; //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中:
--创建一个表来存储IState cpp类的对象
MainState = {}
--IState :: Init纯虚拟函数的实现
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是IState类型的对象:
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())
error: no static '__init' in class 'IState'
是否应该将state = StateManager括起来?这样会有一个错误,表示没有这样的运算符。
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
你不能只是将一个表格扔给 Luabind。如果你想要从一个 Luabind 定义的类派生,你必须遵循 Luabind 的规则。你必须使用 Luabind 的工具来创建一个 Lua 类,并从你的
IState类中派生。代码如下所示:class 'MainState' (IState) -- 假设你将 IState 在全局表中注册,而非注册在 Luabind 模块中。 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.。我不知道你的示例代码是如何工作的。