LuaInterface随机异常
我正在使用 LuaInterface 在我正在制作的游戏中编写脚本。
脚本工作得很好,但不知何故我遇到了很多随机异常。
我已将问题范围缩小到从 C# 代码调用 lua 函数的位置,这些函数是从另一个线程调用的,处理与游戏服务器的网络连接。
我认为正在发生的事情是 lua 代码正在运行,并且 lua 函数是从另一个线程从 C# 调用的,这会导致随机错误。
我如何从 C# 调用函数:
function.Call(message);
函数的类型为 LuaFunction,消息是自定义对象。
我收到的错误:
- “LuaInterface.LuaScriptException”类型的第一次机会异常 发生在 LuaInterface.dll
- 运行 lua 时出错:函数
- 类型为“LuaInterface.LuaScriptException”的第一次机会异常 发生在 LuaInterface.dll
- 无法打开脚本:mainmenu/console
这是否与从创建 LuaVM 的线程以外的线程运行 lua 函数有关?
I'm using LuaInterface for scripting in a game I'm making.
The scripting works quite nice, but somehow I'm getting a lot of random exceptions.
I've narrowed the problem down to where I call lua functions from C# code, which are invoked from another thread, handling networking with the gameserver.
I think what is happening, is that the lua code is being run, and lua functions are called from C# from another thread, which causes the random errors.
How I'm calling the functions from C#:
function.Call(message);
function is of type LuaFunction and message is a custom object.
Errors I'm getting:
- A first chance exception of type 'LuaInterface.LuaScriptException'
occurred in LuaInterface.dll - Error running lua: function
- A first chance exception of type 'LuaInterface.LuaScriptException'
occurred in LuaInterface.dll - Unable to open script: mainmenu/console
Could it have to do with running lua functions from threads other than the thread the LuaVM was created on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Lua 不是线程安全的。您要么需要每个线程有一个 Lua 状态并调用线程特定状态,要么在任何 Lua 调用周围放置锁。
Lua is not thread safe. You either need to have a Lua state per thread and call the thread specific state, or place locks around any Lua calls.
问题确实出在线程上,感谢 BMitch 指出了这一点。
我通过让消息线程将带有参数的函数传递给主线程来修复它,然后主线程在更新方法中调用它们。
The problem was indeed threading, thanks to BMitch for pointing that out.
I've fixed it by letting the message thread hand functions with parameters to the main thread, which then calls them in the update method.