0

I started learning LUA a few days ago, started my own project inside Tabletop Simulator game, but I've hit a brick wall. I can't add time to a Wait class.

This is an example of what I tried:

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

I need for the function to check the first part and if true, do the second part 1 second later. If not, do the second part normally and skip the "i = i + 1". My problem is that the function does everything at the same time. I know I'm doing something wrong, but I can't figure out what. Is there a way to create some for of gate to do everything in order or anything similar?

ikegami
  • 367,544
  • 15
  • 269
  • 518

1 Answers1

0

Your code seems correct.
I don't know what is the problem.

But I know that one of possible solutions is to follow the "callback hell" style of programming:

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
Egor Skriptunoff
  • 23,359
  • 2
  • 34
  • 64