This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
nikojoi's profile picture

Menu problems

Started by nikojoi, 09 October 2012 - 03:24 AM
nikojoi #1
Posted 09 October 2012 - 05:24 AM
EDIT: I fixed up the code (edited my main comment with the new code) and the main menu works now. I can ever switch into the lights menu and turn the lights on and off, perfect. When I try to hit the "To Do" option though I get this message:

menuBoard.txt:106: attempt to call nil
Line 106 should just be calling a specific handler. Why does it work for the lights menu but not to print my To Do list to the attached monitor ?


--[[Local variables]]--

local termWidth, termHeight = term.getSize()
local selectedItem = 1
local inMainMenu = true
local inLightsMenu = false
local inToDo = false
local monitor = peripheral.wrap("back")

--[[Menu Methods]]--

function lights()
inLightsMenu = true
selectedItem = 1
while inLightsMenu do
  term.clear()
  term.setCursorPos(1,1)
  printMenu(lightsMenu)

  event, key = os.pullEvent("key")
  onKeyPressed(key, lightsMenu)
end
end

function lightsOn()
rs.setOutput("bottom", true)
inLightsMenu = false
selectedItem = 1
end

function lightsOff()
rs.setOutput("bottom", false)
inLightsMenu = false
selectedItem = 1
end

function toDO()
inToDo = true
while inToDo do
  monitor.clear()
  monitor.setCursorPos(1,1)
  for i=1,4 do
   monitor.print(toDoList[i])
  end
end
end

function topSecret()

end

function exit()
  inMainMenu = false
end

--[[Menu Definitions]]--

mainMenu = {
[1] = { text = "Lights", handler = lights},
[2] = { text = "To Do", handler = toDo},
[3] = { text = "Top Secret", handler = topSecret},
[4] = { text  = "Exit", handler = exit}
}

lightsMenu = {
[1] = { text = "Lights On", handler = lightsOn },
[2] = { text = "Lights Off", handler = lightsOff }
}

toDoList = {
"[1] RailCraft Building",
"[2] Bother Jared",
"[3] Make Cookies",
"[4] Brainstorm Frame Ideas"
}

--[[Printing Methods]]--

function printMenu( menu )
for i=1,#menu do
  if i == selectedItem then
   print(">> "..menu[i].text)
  else
   print("   "..menu[i].text)
  end
end
end

--[[Handling Methods]]--

function onKeyPressed( key, menu )
if key == 28 then
  onItemSelected(menu)
elseif key == 200 then
  if selectedItem > 1 then
   selectedItem = selectedItem - 1
  end
elseif key == 208 then
  if selectedItem < #menu then
   selectedItem = selectedItem + 1
  end
end
end

function onItemSelected( menu )
menu[selectedItem].handler()
end

--[[Main Method]]--

function main()
while inMainMenu do
  term.clear()
  term.setCursorPos(1,1)
  printMenu(mainMenu)

  event, key = os.pullEvent("key")
  onKeyPressed(key,mainMenu)
end
end

main()
faubiguy #2
Posted 09 October 2012 - 05:34 AM
You misspelled function as "funtion".
Doyle3694 #3
Posted 09 October 2012 - 06:21 AM
I see you watched FunshineX…
nikojoi #4
Posted 09 October 2012 - 06:55 PM
Oh wow, I feel dumb :P/>/>
Sometimes all it takes is a fresh set of eyes to see the easiest mistakes.

I fixed up the code (edited my main comment with the new code) and the main menu works now. I can ever switch into the lights menu and turn the lights on and off, perfect. When I try to hit the "To Do" option though I get this message:

menuBoard.txt:106: attempt to call nil
Line 106 should just be calling a specific handler. Why does it work for the lights menu but not to print my To DO list to the attached monitor ?


I see you watched FunshineX…

Yea, I watch a lot of his videos. I like the way he did his menu in the tutorial video he did on them so I decided to use the code. That's pretty much how I've been learning lua and computercraft.
Fatal_Exception #5
Posted 10 October 2012 - 12:54 AM
You call ToDo, but you declared your function as ToDO