在“运行时错误”之后,lua+luabind栈顶无错误信息。
2012-5-26 21:43:37
收藏:0
阅读:173
评论:1
我正在将 Lua 嵌入我的游戏引擎中,一切都很好,直到现在。我开始注册函数来与游戏引擎进行接口交互(例如,move_object(id,x,y)),并且注册成功。接下来,我尝试像这样使用它:
handlers={}
handlers["update"] = function(objId)
-- print(objId)
move_object(objId, 0, 0)
end
这使我收到了一个 luabind 的 "runtime error" 异常。我到处搜索,根据互联网(和 luabind 源代码)的说法,错误消息应该仍然在 lua 栈的顶部。我尝试捕获 luabind::error,并像这样打印栈的顶部:
std::cout << "error: lua: " << lua_tostring(exc.state(), -1) << std::endl;
和像这样打印:
std::cout << "error: lua: " << luabind::object(luabind::from_stack(exc.state(), -1)) << std::endl
两者都打印:
error: lua: update
我假设这只是调用处理程序表中 update 函数时推送的最后一个值。当遇到错误时,这个值应该被详细的错误字符串覆盖,正确吗?根据 luabind 源文件 "error.hpp" 在 class error 的定义中:
// this exception usually means that the lua function you called
// from C++ failed with an error code. You will have to
// read the error code from the top of the lua stack
// the reason why this exception class doesn't contain
// the message itself is that std::string's copy constructor
// may throw, if the copy constructor of an exception that is
// being thrown throws another exception, terminate will be called
// and the entire application is killed.
我认为我的 Lua 代码中的问题实际上是我的 move_object 函数的注册(update 函数正在运行。我知道这一点,因为我已经取消了注释,并在之前打印了该调用!),但没有更好的错误信息,我不能确定!哈哈
我已解决错误(如果有帮助的话)。我定义 move_object 函数是这样的:
luabind::module(m_L)[
luabind::def("move_object", &ScriptEntityManager::move_object)
];
但显然,您不能将静态成员函数定义为 luabind 的常规函数。我将其更改为(在 C++ 中使 move_object 成为常规全局函数):
luabind::module(m_L)[
luabind::def("move_object", move_object)
];
我不知道这是否有助于原始问题,但我认为更多信息不会有害! :)
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的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使用的错误处理程序(set_pcall_callback)来解决这个问题,这样它会打印错误消息(和调用堆栈),在异常抛出之前发生。这种方法对我来说很有效。
但是如果有人真正知道为什么在捕捉异常时堆栈顶部不包含错误消息,则我也很感兴趣:-)
这是我使用的错误处理程序(其中"print"是一些自定义函数,可以记录一个std::string):
int luabindErrorHandler( lua_State* L ) { // log the error message luabind::object msg( luabind::from_stack( L, -1 ) ); std::ostringstream str; str << "lua> run-time error: " << msg; print( str.str() ); // log the callstack std::string traceback = luabind::call_function<std::string>( luabind::globals(L)["debug"]["traceback"] ); traceback = std::string( "lua> " ) + traceback; print( traceback.c_str() ); // return unmodified error object return 1; }