pinix
pinix

Reputation: 35

Requiring a Lua file that uses a function declared in the other Lua file

I have this issue in Lua where I have 2 files. FileA may look like this:

require "FileB"
local function foo(bar)
    -- random stuff
end

And FileB looks like this

foo(bar)

But an error pops up, saying that foo is an invalid function. Is there any fix for this?

Upvotes: 0

Views: 184

Answers (1)

Jane T
Jane T

Reputation: 2091

Normal practice would be to put the functions in the required file, not the calling code.

In any case as you have your code above, you are calling foo before you are defining it. So move the require below the definition of foo, and don't use local.

function foo(bar)
    -- random stuff
end

require "FileB"

Upvotes: 1

Related Questions