【问题标题】:Is there a way to add time to a Wait class inside an If statement?有没有办法在 If 语句中为 Wait 类添加时间?
【发布时间】:2020-07-25 12:59:30
【问题描述】:

我几天前开始学习 LUA,在 Tabletop Simulator 游戏中开始了我自己的项目,但我碰壁了。我无法为等待课程增加时间。

这是我尝试过的一个例子:

function state_check()
    --This function checks if the state of obj_1 and obj_2 is 0 or 1
end

function press_button()
    --This function activates other functions based on the state of obj_1 and obj_2

    i = 1 --Function starts with a wait of 1 second

    if obj_1_state == 1 then --If state is 1, then the function is triggered and 1 second is added to i
        Wait.time(func_1, i)
        i = i + 1
    end

    if obj_2_state == 1 then
        Wait.time(func_2, i)
    end
end

我需要该函数检查第一部分,如果为真,请在 1 秒后执行第二部分。如果没有,请正常执行第二部分并跳过“i = i + 1”。 我的问题是该函数同时执行所有操作。我知道我做错了什么,但我不知道是什么。有没有办法创建一些 for of gate 来按顺序执行所有操作或类似的操作?

【问题讨论】:

    标签: lua moonsharp tabletop-simulator


    【解决方案1】:

    您的代码似乎正确。
    不知道是什么问题。

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

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-30
      相关资源
      最近更新 更多