How can I resolve function-dependency loops in Lua?

circular-dependency, coronasdk, function, lua

Solution

Remember that

local function FuncName() end

is syntactically equivalent to:

local FuncName = function() end

So it's a simple matter of declaring the local variables, but setting their function values after the declaration:

local foo = nil
local bar = nil

foo = function()
    bar()
end

bar = function()
    foo()
end

Problem

In my Lua code (using the Corona SDK), my issue essentially boils down to this: ``` local function foo() bar() end local function bar() foo() end ``` However, because Lua is done line-by-line, this has no chance of working. Futhermore, I don't see a way to avoid this dependency loop; foo() creates DisplayObjects that call bar() for touch events, which itself has the capability of calling foo(). To put it another way, I need to be able to make buttons that, when clicked, make more buttons that can do the same thing. Futhermore, I know Lua doesn't have function prototyping like in C/C++. Any suggestions for how to fix this?

Original source