Posted 04 December 2013 - 03:07 AM
Ok i am creating a menu interface for my program and the first menu looks ok until i enter into the 2nd option ("Lights Control")
Error is attempt to call nil on line 63.
–[[ Extra Info ]]–
line 63 – for i = 1, #menu do
Why does this not apply to my second option "Lights Control"?
Error is attempt to call nil on line 63.
--[[ This is my program ]]--
--[[ Local Variable ]]--
local termWidth, termHeight = term.getSize()
local selectedItem = 1
local inMainMenu = true
local inLightsMenu = false
--[[Menu Methods ]]--
function Choice1()
end
function Choice2()
inLightsMenu = true
selectedItem = 1
while inLightsMenu do
clear()
printMenu(lightsMenu)
event, key = os.pullEvent("key")
OnKeyPressed(key,lightsMenu)
end
end
function Exit()
inMainMenu = false
end
function ON()
rednet.open("back")
while true do
rednet.send(0, on)
end
end
function OFF()
while true do
rednet.send(0, off)
end
end
function Reboot()
os.reboot()
end
--[[ Menu Definition ]]--
local mainMenu = {
[1] = {text = "Doors", handler = Choice1},
[2] = {text = "Lights Control", handler = Choice2},
[3] = {text = "Exit", handler = Exit}
}
local lightsMenu = {
[1] = {text = "Lights on", handler = ON},
[2] = {text = "Lights off", handler = OFF},
[3] = {text = "Back to Main Menu", handler = Reboot}
}
--[[ 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
--[[ Handler Methods ]]--
function onKeyPressed(key, menu )
if key == keys.enter then
onItemSelected( menu )
elseif key == keys.up then
if selectedItem > 1 then
selectedItem = selectedItem - 1
end
elseif key == keys.down then
if selectedItem < #menu then
selectedItem = selectedItem + 1
end
end
end
function onItemSelected( menu )
menu[selectedItem].handler()
end
function clear()
term.clear()
term.setCursorPos(1,1)
end
--[[Main Method ]]--
function main()
while inMainMenu do
clear()
printMenu(mainMenu)
event, key = os.pullEvent("key")
onKeyPressed(key,mainMenu)
end
end
main()
–[[ Extra Info ]]–
line 63 – for i = 1, #menu do
Why does this not apply to my second option "Lights Control"?
Edited by