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

Selecteditem Trouble

Started by cartmen180, 29 July 2013 - 08:21 AM
cartmen180 #1
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()
Lyqyd #2
Posted 29 July 2013 - 01:03 PM
Split into new topic.
cartmen180 #3
Posted 29 July 2013 - 01:51 PM
EDIT: it didn't work, what it did was every choice i made was the first option. even when i select exit it would go to the slave menu >.>

i am putting blank lines between functions, i don't know why it is not showing up here :(/>
now that i think about it you make alot of sense with tip 2.
cartmen180 #4
Posted 29 July 2013 - 02:45 PM
i was thinking the same thing! I figured i should do
function turtleOperation()
    inTurtleMenu = true
    
    while inTurtleMenu do
        term.clear()
        term.setCursorPos(1,1)
        printMenu( turtleMenu )
   	 selectedItem = 1

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

but that won't do it either..

i have cleaned up my file and posted it on pastebin: http://pastebin.com/7vRmd4m9
the past problem is still there, but now the exit function no longer works..
cartmen180 #5
Posted 29 July 2013 - 03:18 PM
i use them as booleans. As long as inTurtleMenu = true it will stay in the turtlemenu and not go anywhere else. Now as soon as i press back it sets the boolean to false, thus exiting the turtlemenu and returning to the mainmenu.

i fixed the exit function by moving the menu tables just above the main function.

everything works as it should now, except for the selectedItem bug :(/>
immibis #6
Posted 29 July 2013 - 05:12 PM
When you choose "turtle operation", selectedItem is 2, because "turtle operation" is the second item of the main menu.
Then turtleOperation doesn't reset selectedItem before it runs its menu, so it starts off with selectedItem=2.