【问题标题】:Converting Lua function to C#将 Lua 函数转换为 C#
【发布时间】:2020-04-02 18:57:06
【问题描述】:

我一直在努力思考如何创建这个函数调用,我在 Lua 中编写没有问题,但我正在将内容转换为 C# 以离开 Lua 并转向新的东西。非常感谢我能得到的任何帮助,我附上了工作 Lua 代码,就我在 C# 中得到的而言,我还在我无法弄清楚的部分代码中添加了注释。

Lua 代码:

Progress({
    name = "unique_action_name",
    duration = 1000,
    label = 'Doing Something',
    useWhileDead = true,
    canCancel = true,
    controlDisables = {
        disableMovement = true,
        disableCarMovement = true,
        disableMouse = false,
        disableCombat = true,
    },
    animation = {
        animDict = "missheistdockssetup1clipboard@base",
        anim = "base",
        flags = 49,
    },
    prop = {
        model = "p_amb_clipboard_01",
        bone = 18905,
        coords = { x = 0.10, y = 0.02, z = 0.08 },
        rotation = { x = -80.0, y = 0.0, z = 0.0 },
    },
    propTwo = {
        model = "prop_pencil_01",
        bone = 58866,
        coords = { x = 0.12, y = 0.0, z = 0.001 },
        rotation = { x = -150.0, y = 0.0, z = 0.0 },
    },
}, function(cancelled)
    if not cancelled then
        -- Do Something If Action Wasn't Cancelled
    else
        -- Do Something If Action Was Cancelled
    end
end)

到目前为止,我已经获得了 C# 代码

Progress(new {
    name = "unique_action_name",
    duration = 1000,
    label = "Doing Something",
    useWhileDead = true,
    canCancel = true,
    controlDisables = new {
        disableMovement = true,
        disableCarMovement = true,
        disableMouse = false,
        disableCombat = true,
    },
    animation = new {
        animDict = "missheistdockssetup1clipboard@base",
        anim = "base",
        flags = 49,
    },
    prop = new {
        model = "p_amb_clipboard_01",
        bone = 18905,
        coords = new { x = 0.10, y = 0.02, z = 0.08 },
        rotation = new { x = -80.0, y = 0.0, z = 0.0 },
    },
    propTwo = new {
        model = "prop_pencil_01",
        bone = 58866,
        coords = new { x = 0.12, y = 0.0, z = 0.001 },
        rotation = new { x = -150.0, y = 0.0, z = 0.0 },
    },
}, function(cancelled) { // < This part here is the question that rises for me, no clue how to call this in C#

    if (!cancelled)
        //Do Something If Action Wasn't Cancelled
    else
        // Do Something If Action Was Cancelled
});

【问题讨论】:

  • Progress 是什么?你不能只使用/编写自己的类吗?
  • Progress 属于第 3 方脚本,它最后执行的函数调用在游戏中的多核函数中非常常见,这是我正在尝试学习如何调用的部分。不完全确定如何从第一次调用中声明回调函数
  • 试试(cancelled) =&gt; { /* your code goes there */ } 而不是function(canceled { })。这称为lambda expression
  • 你想要使用的可能是一个函数/动作委托。查看函数代表here 和动作代表here的文档
  • 您在编辑中编写它的第二种方式应该可以工作。但是,不要编辑您的问题并添加您的解决方案,您可以对其进行测试,然后,如果它按预期工作,请将其作为答案发布

标签: c# lua


【解决方案1】:

在上面的 cmets 的帮助下解决了这个问题,非常感谢您的所有帮助!

Progress(new
{
    name = "unique_action_name",
    duration = 1000,
    label = "Doing Something",
    useWhileDead = true,
    canCancel = true,
    controlDisables = new
    {
        disableMovement = true,
        disableCarMovement = true,
        disableMouse = false,
        disableCombat = true,
    },
    animation = new
    {
        animDict = "missheistdockssetup1clipboard@base",
        anim = "base",
        flags = 49,
    },
    prop = new
    {
        model = "p_amb_clipboard_01",
        bone = 18905,
        coords = new { x = 0.10, y = 0.02, z = 0.08 },
        rotation = new { x = -80.0, y = 0.0, z = 0.0 },
    },
    propTwo = new
    {
        model = "prop_pencil_01",
        bone = 58866,
        coords = new { x = 0.12, y = 0.0, z = 0.001 },
        rotation = new { x = -150.0, y = 0.0, z = 0.0 },
    },
}, new Action<bool>((isCanceled) =>
{
    if (!isCanceled)
        Debug.WriteLine("DONE");
    else
        Debug.WriteLine("CANCELED");
}));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-24
    • 2013-06-22
    • 2011-12-24
    • 2013-04-21
    相关资源
    最近更新 更多