为什么我的 Lua 脚本为全局布尔变量抛出异常?
2021-9-23 2:49:15
收藏:0
阅读:151
评论:0
我正在尝试为我的 Lua 脚本中的全局变量赋予布尔默认值,然后输出或利用该值。但是,在我的主程序中,这会抛出异常...在以下的小程序中,我没有抛出异常...但是一旦我尝试输出布尔值,它就不会输出其他任何东西(即使不是布尔值)...有人知道发生了什么吗?我已经花了数小时在这上面了...
这是我在我的主程序上得到的异常,但不是在我的示例程序上。当我将变量变为布尔值时,才会抛出此异常(如果是整数,则不会发生)。
[sol3] An error occurred and panic has been invoked: stack index 1, expected string, received no value: bad get from protected_function_result (is not an error)
Main.cpp
#pragma warning(push, 0)
#define SOL_ALL_SAFETIES_ON 1
#include "sol.hpp"
#pragma warning(pop)
int ExceptionHandler(
lua_State* L,
sol::optional<const std::exception&> maybe_exception,
sol::string_view description)
{
if (maybe_exception) {
const std::exception& ex = *maybe_exception;
std::cout << "Lua 错误1: " << std::string(ex.what());
}
else {
std::cout << "Lua 错误2: " << std::string(description.data(), description.size());
}
return sol::stack::push(L, description);
}
int main()
{
sol::state lua_state;
lua_state.open_libraries(sol::lib::base);
lua_state.set_exception_handler(&ExceptionHandler);
sol::protected_function_result result = lua_state.safe_script_file("File1.lua", &sol::script_pass_on_error);
if (!result.valid())
{
sol::error err = result;
std::cout << err.what();
}
else
{
lua_state["PrintInfo"]("CPP 字符串");
}
system("pause");
}
File1.lua
SampleString = "示例";
IsSampleBool1 = false;
IsSampleBool2 = false;
IsSampleBool3 = false;
IsSampleBool4 = false;
SampleInt1 = 0;
SampleInt2 = 1;
dofile("File2.lua");
File2.lua
function PrintInfo(cpp_string)
print("-- CPPString: " .. cpp_string);
print("-- SampleString: " .. SampleString);
print("-- SampleBool1: " .. IsSampleBool1);
print("-- SampleBool2: " .. IsSampleBool2);
print("-- SampleBool3: " .. IsSampleBool3);
print("-- SampleBool4: " .. IsSampleBool4);
print("-- SampleInt1: " .. SampleInt1);
print("-- SampleInt2: " .. SampleInt2);
print("-- 结束");
end
原文链接 https://stackoverflow.com/questions/69293430
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- 如何在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中获取用户配置主目录的跨平台方法
- 如何编写 Lua 模式将字符串(嵌套数组)转换为真正的数组?