c# - 逐行编写Lua脚本

标签 c# dynamic lua scripting-language embedded-script

我使用 DynamicLua 将 Lua 脚本添加到我的 C# 应用程序中库,它工作得很好。 我想实现您获取正在执行的当前行(就像在 Visual Studio 中一样)并突出显示它。

目前我正在这样做:

   public static void RunLua(string LuaToExecute)
    {
        dynamic lua = new DynamicLua.DynamicLua();

        string[] lua_s_split = LuaToExecute.Split('\n');
        int counter = 0;
        foreach (string line in lua_s_split)
        {
            // highlight current line in editor
            HighlightLine(counter + 1);

            //execute current line 
            lua(line);
            counter++;
        }
    }

这对我的 Lua 代码非常有效,例如

move(20, 19)
sleep(1000)
move(5, 19)

但我不能只执行一行语句。就像我的绑定(bind)函数 move()。但我也想使用多行语句,如函数和循环。如果文本编辑器包含:

function test()
    return  "Hallo Welt"
end

lua(line) 会抛出异常,因为只有第一行 function test() 被传递,解释器缺少结束语句。

我能做什么?我是否应该检查该行是否以函数开头,而...命令,而不是扫描结束 block 并将其添加到字符串中,以便我可以一次执行并突出显示此多行语句?这可能吗?我该怎么做?

请帮助。

最佳答案

I would like to implement that you get the current line which is being executed (Like in Visual Studio) and highlight it.

不要通过一次给 Lua 脚本一行来做到这一点。运行整个脚本并让 Lua 在执行切换到新行时通过调试 Hook 通知您:

Programming in Lua: Hooks
Lua manual: debug.sethook

关于c# - 逐行编写Lua脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24334574/

相关文章:

javascript - 在 JavaScript 中传递多个可选参数

c# - "Doesn' 尝试连接到域时没有访问域所需的权限

c - 通过引用将动态二维数组传递给另一个函数来分配动态二维数组

javascript - 通过 javascript 的 IE/Firefox 风格更新不起作用

c# - 在 C# 中删除动态创建的控件

c - 如何在尾调用中保留 Lua 函数闭包?

lua - Lua中如何忽略字符串操作的特殊字符?

c# - 两个物体在什么增量时间发生碰撞?

c# - 在依赖注入(inject)中注册多个 .Net Core IHostedService - .net core 中的后台服务

C# 和 Docker - 无法从容器化的 .NET Core 3.1 Web API 连接到容器化的 MySQL 服务器