C#中嵌入Lua交互提示窗口
我尝试在我的 C# 应用程序中提供一些脚本功能。 经过一番搜索,Lua 和 LuaInterface 看起来是一个合理的方法。 通过在我的 C# 代码中嵌入 Lua VM,我可以加载 Lua 脚本并处理方法 在 C# 类中定义没有任何问题。
但是,我想知道有没有办法不仅在中执行lua脚本文件 我的c#程序,还要在我的程序中有一个lua交互提示吗? 因此我可以输入命令并立即看到返回结果。
任何关于我应该从哪里开始工作的提示都会非常有帮助!
此致
I tried to provide some script features in my C# App.
After some searches, The Lua and LuaInterface looks like a reasonable way to do it.
By embedding a Lua VM in my C# code, I can load a Lua script and process methods
defined in the C# class without any problems.
However, I wonder is there a way to not only execute a lua script file in
my c# program, but also to have a lua interactive prompt in my program?
Thus I can type commands and see the returns immediately.
Any hint about where I should start to work on it will be very helpful!
Best Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Lua的
debug
模块包含一个非常基本的交互式控制台:debug.debug()
(Lua手册建议此模块只能用于调试)。如果这不能满足您的需求,您可以很容易地自行实现。如果你用 Lua 本身编写它,绝对的骨架控制台将是:
这会因语法或运行时错误而崩溃。稍微不太简单的版本,它捕获(并忽略)错误:
这可以通过显示语法错误(失败时从
loadstring
中返回的第二个值)、运行时错误(从pcall 中返回第二个值)来增强。
失败),从评估的代码中返回值([2..N] 从pcall
成功时返回值)等。如果您想要真正奇特的是,您可以允许人们输入多行语句。查看源发行版中的 src/lua.c 来了解这是如何完成的。
Lua's
debug
module contains a very basic interactive console:debug.debug()
(the Lua manual suggests this module should be used only for debugging).If that doesn't meet your needs, it's easy enough to implement one yourself. If you were to write it in Lua itself, the absolute bare bones console would be:
That'll crash on either a syntax or runtime error. A slightly less minimal version, which captures (and ignores) errors:
This could be enhanced by displaying syntax errors (second return value from
loadstring
on failure), runtime errors (second return value frompcall
on failure), return values from evaluated code ([2..N] return values frompcall
on success), etc.If you want to get really fancy, you can allow people to enter multi-line statements. Look at
src/lua.c
in the source distro to see how this is done.