0

I've got a Lua script that presents an interactive text menu for configuring the script before actually doing the work. There is a main_menu() function, which has options the user can select, each of which call a different submenu() function. Each of those different submenu() functions do their thing and then they go back to (they call) the main_menu() function. Finally, each function has a parameter of settings passed to it, which is a table of settings.

Things look like this:

local function submenu(settings)
    -- Get user input & change a settings{} table key accordingly
    main_menu(settings)
end

local function main_menu(settings)
    -- Present choices & get user input
    submenu(settings)
end

local settings={}
settings["foo"] = "bar"

main_menu(settings)

The problem is I'm getting attempt to call nil errors whenever (as far as I can tell) a function calls another function that is defined later on in the script. So if, as in the example above, I define submenu() and then main_menu(), main_menu() has no problem calling submenu(), but submenu() can't call main_menu().

FWIW, this is being done in the ComputerCraft mod for Minecraft.

prapin
  • 6,395
  • 5
  • 26
  • 44
Sandwich
  • 475
  • 1
  • 8
  • 16
  • possible duplicate of [Lua function range problem](http://stackoverflow.com/questions/6394721/lua-function-range-problem) and [How to call function from above it in code (prior to it being defined)?](http://stackoverflow.com/questions/12291203/lua-how-to-call-function-from-above-it-in-code-prior-to-it-being-defined) – finnw Dec 26 '12 at 01:18
  • Thanks for those links; I searched on this issue in Google before posting but wasn't able to find anything relevant, nor did I see anything like those threads in the thread suggestions that were presented while I was posting this one. Musta been using the wrong keywords. – Sandwich Dec 26 '12 at 12:16

1 Answers1

2

You could either do a local function with forward declaration like this:

local main_menu

local function submenu(settings)
    -- Get user input & change a settings{} table key accordingly
    main_menu(settings)
end

main_menu = function(settings)
    -- Present choices & get user input
    submenu(settings)
end

or do a global function declaration by removing local keywords:

function submenu(settings)
    -- Get user input & change a settings{} table key accordingly
    main_menu(settings)
end

function main_menu(settings)
    -- Present choices & get user input
    submenu(settings)
end
cctan
  • 2,015
  • 3
  • 19
  • 29
  • The top line of the first answer is invalid syntax; you need to use `local main_menu` rather than `local function main_menu` for forward declaration. – furq Dec 26 '12 at 01:43
  • Hmm, ok. From what I read (only been dabbling in Lua for 2 days), it's best to stay away from globals if at all possible, so I'll try to implement the first solution you provided. The actual program has a whole mess of "submenu" functions; would there be anything intrinsically wrong with just declaring them all in one fell swoop up top (`local main_menu local submenu_1 local submenu_2`, etc) and only then defining them all? – Sandwich Dec 26 '12 at 12:14
  • @Sandwich In the context of computercraft, I see no problem with it ;) – cctan Dec 27 '12 at 01:26