有没有办法在 If 语句内添加一个等待类的时间?

我几天前开始学习 LUA,在 Tabletop Simulator 游戏中启动我的项目,但我卡在了这个问题上。 我无法为 Wait 类添加时间。

这是我尝试的例子:

function state_check()
    --此函数检查 obj_1 和 obj_2 的状态是否为 0 或 1
end

function press_button()
    --此函数基于 obj_1 和 obj_2 的状态激活其他函数

    I = 1 --函数开始等待 1 秒钟

    如果 obj_1_state == 1,则 Wait.time(func_1, i) -- 如果状态为 1,则触发函数并将 1 秒钟添加到 I
        i = i + 1
    end

    如果 obj_2_state == 1 则 Wait.time(func_2, i)
    end
end

我需要函数检查第一个部分,如果为真,则在 1 秒钟后执行第二个部分;如果为假,则正常执行第二个部分并跳过“i=i + 1”。 我的问题是函数同时做所有事情。 我知道我做错了什么,但我想不出什么。 有没有办法创建某种类型的门来依次完成所有操作或任何类似操作的方式?

点赞
用户1847592
用户1847592

你的代码看起来是正确的。

我不知道问题出在哪里。

但我知道一种可能的解决方案是遵循“回调地狱”式的编程方式:

local function second_part(obj_2_state)
    if obj_2_state == 1 then
        Wait.time(func_2, 1)
    end
end

local function first_part(obj_1_state, obj_2_state)
    if obj_1_state == 1 then
        Wait.time(
            function()
                func_1()
                second_part(obj_2_state)
            end, 1)
    else
        second_part(obj_2_state)
    end
end

function press_button()
    local obj_1_state, obj_2_state = --calculate states of obj_1 and obj_2 here
    first_part(obj_1_state, obj_2_state)
end
2020-07-10 21:38:56