4

My problem is that I have a function that needs to be called before it is referenced. Another words, the code looks like:

doStuff()

local function doStuff()  end

and whenever I try to run it, it cannot reference the function doStuff(). My question is how can I call this function without moving the function above where it is called? So I don't want:

local function doStuff() end

doStuff()

since it will cause errors in other parts of my program.

3
  • possible duplicate of lua - how to call function from above it in code (prior to it being defined)? Commented Jan 10, 2015 at 19:11
  • 1
    Why can't you move the definition to be before the call, can you demonstrate the problem? Commented Jan 10, 2015 at 20:14
  • The reason is that this function is dependent upon a function between doStuff() and its definition. Commented Jan 10, 2015 at 20:54

1 Answer 1

4

a function that needs to be called before it is referenced

You cannot. You need to solve this problem in a different way. The only situation you may need to do that is if you have two functions that recursively call each other. You can do this way:

local a
local function b()
  a()
end
a = function()
  b()
end
a()

This will go into infinite recursion, but you should get the idea. Another option is to use global variables, but you still won't be able to call a function before it's defined (by any means).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.