Posted 29 July 2013 - 10:21 AM
Hi there, i have a problem with this menu i wrote. I want to start in every menu on the first option. But for the turtleMenu it insists on starting at the second option. I can seem to figure out where to place the "selectedItem = 1" line. Whatever i try, it won't start at the first option!
Spoiler
local termWidth, termHeight = term.getSize()
selectedItem = 1
--[[menu methods]]--
function addSlave()
end
function checkSlave()
end
function removeSlave()
end
function addTurtle()
end
function checkTurtle()
end
function removeTurtle()
end
function slaveBack()
inSlaveMenu = false
selectedItem = 1
end
function turtleBack()
inTurtleMenu = false
selectedItem = 1
end
function Exit()
inMainMenu = false
inSlaveMenu = false
inTurtleMenu = false
term.clear()
term.setCursorPos(1, 1)
end
--[[menu definitions]]--
mainMenu = {
[1] = {text = 'Slave Operation', handler = slaveOperation},
[2] = {text = 'Turtle Operation', handler = turtleOperation},
[3] = {text = 'Exit', handler = Exit}
}
slaveMenu = {
[1] = {text = 'add slave', handler = addSlave},
[2] = {text = 'check slave', handler = checkSlave},
[3] = {text = 'remove slave', handler = removeSlave},
[4] = {text = 'back', handler = slaveBack}
}
turtleMenu = {
[1] = {text = 'add turtle', handler = addTurtle},
[2] = {text = 'check turtle', handler = checkTurtle},
[3] = {text = 'remove turtle', handler = removeTurtle},
[4] = {text = 'back', handler = turtleBack}
}
--[[printing methods]]--
function printMenu( menu )
print("--------------------------------------------------")
print("------[[Generator Master Control Interface]]------")
print("--------------------------------------------------\n")
for i=1,#menu do
if i == selectedItem then
print(">> "..menu[i].text)
else
print(" "..menu[i].text)
end
end
end
--[[handler method]]--
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
--[[main method]]--
function main()
inMainMenu = true
while inMainMenu do
term.clear()
term.setCursorPos(1, 1)
printMenu( mainMenu )
event, key = os.pullEvent("key")
onKeyPressed(key,mainMenu)
end
end
function slaveOperation()
inSlaveMenu = true
while inSlaveMenu do
term.clear()
term.setCursorPos(1,1)
printMenu( slaveMenu )
event, key = os.pullEvent("key")
onKeyPressed(key,slaveMenu)
end
end
function turtleOperation()
inTurtleMenu = true
while inTurtleMenu do
term.clear()
term.setCursorPos(1,1)
printMenu( turtleMenu )
event, key = os.pullEvent("key")
onKeyPressed(key,turtleMenu)
end
end
main()