3

Here is raw idea of what I'm trying to do.

function a(str)
    print(str)
end

function b(str)
    print(str)
end

function c(str)
    print(str)
end

function runfunctions(...)
    local lst = {...}
    lst.startup()
end

local n1 = a('1')
local n2 = b('2')
local n3 = c('3')

runfunctions(n3,n1,n2)

Few functions got to be pass as args to other functions and being executed in sequence. Once any of them been executed it can't be executed for msec so next will be executed, to avoid only being executed few of them and don't run till the last one.

2 Answers 2

3

You need closures.

In your code, the functions a, b and c all do the execution and returns nothing. Instead, return a closure that does the work (but not execute for now):

function a(str)
    return function() print(str) end
end

Then execute the function when needed:

function runfunctions(...)
    for _, v in ipairs{...} do
        v()
    end
end
Sign up to request clarification or add additional context in comments.

1 Comment

What about when those functions got some actions and they don't return any value, in that case how would it look like?
2
function runfunctions(...)
   for _, f_with_args in ipairs{...} do
      pcall((table.unpack or unpack)(f_with_args))
   end
end

runfunctions({c, '3'}, {a, '1'}, {b, '2'}, {print, "Hello", "world"})

2 Comments

input:16: attempt to call a nil value (global 'unpack')
@user1768615 - Updated.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.