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

Menu troubles

Started by nikojoi, 12 February 2013 - 07:15 AM
nikojoi #1
Posted 12 February 2013 - 08:15 AM
I made a program on my friend's tekkit server to order items and tried to make a spiffy little menu system to clean things up. So far it all works like it should, except when I go into the second layer of menus and try to hit the 'Back' line, it just terminates the program. I'm not sure what coding I need to do to make it just go back to the previous menu. Also any advice on cleaning the code up is very welcome.

Here's the program:


--[[Local Variables]]--

local inMenu = true
local inAmmo = false
local inDarts = false
local selectedItem = 1

--[[Moving methods]]--

function pulse(side, color, time)
rs.setBundledOutput(side, color, true)
sleep(time/2)
rs.setBundledOutput(side, 0)
sleep(time/2)
end

function musket()
  term.clear()
  term.setCursorPos(1,1)
  write("How many would you like ? ")
  t = read()
  for i=1,tonumber(t) do
	pulse("bottom", colors.orange, 0.8)
  end
end

function musball(t)
  inMenu = false
  inAmmo = true
end

function blowgun(t)
  term.clear()
  term.setCursorPos(1,1)
  write("How many would you like ? ")
  t = read()
  for i=1,tonumber(t) do
	pulse("bottom", colors.magenta, 0.8)
  end
end

function darts(t)
inMenu = false
inDarts = true
end

function ammo8()
  pulse('bottom', colors.white, 0.8)
end

function ammo16()
for i=1,2 do
  pulse('bottom', colors.white, 0.8)
end
end

function ammo32()
for i=1,4 do
  pulse('bottom', colors.white, 0.8)
end
end

function ammo64()
for i=1,8 do
  pulse('bottom', colors.white, 0.8)
end
end

function dart4()
for i=1,4 do
  pulse('bottom', colors.lightBlue, 0.8)
end
end

function dart8()
for i=1,4 do
  pulse('bottom', colors.lightBlue, 0.8)
end
end

function dart16()
for i=1,4 do
  pulse('bottom', colors.lightBlue, 0.8)
end
end

function dart32()
for i=1,4 do
  pulse('bottom', colors.lightBlue, 0.8)
end
end

function exit()
term.clear()
term.setCursorPos(1,1)
write("Password: ")
input = read('*')
if input == "gizmo" then
  inMenu = false
end
end

function back()
inMenu = true
inAmmo = false
end

function back2()
inMenu = true
inDarts = false
end

--[[Menu Definitions]]--
mainMenu = {
[1] = { text = "Musket", handler = musket },
[2] = { text = "Musket Balls", handler = musball },
[3] = { text = "Blowgun", handler = blowgun },
[4] = { text = "Darts", handler = darts },
[5] = { text = "Admin", handler = exit }
}

ammoMenu = {
[1] = { text = "8", handler = ammo8 },
[2] = { text = "16", handler = ammo16 },
[3] = { text = "32", handler = ammo32 },
[4] = { text = "64", handler = ammo64 },
[5] = { text = "Back", handler = back }
}

dartMenu = {
[1] = { text = "4", handler = dart4 },
[2] = { text = "8", handler = dart8 },
[3] = { text = "16", handler = dart16 },
[4] = { text = "32", handler = dart32 },
[5] = { text = "Back", handler = back2 }
}

--[[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 inMenu do
  term.clear()
  term.setCursorPos(1,1)
  printMenu(mainMenu)
  event, key = os.pullEvent("key")
  onKeyPressed(key,mainMenu)
end

while inAmmo do
  term.clear()
  term.setCursorPos(1,1)
  printMenu(ammoMenu)
  event, key = os.pullEvent("key")
  onKeyPressed(key,ammoMenu)
end
while inDarts do
  term.clear()
  term.setCursorPos(1,1)
  printMenu(dartMenu)
  event, key = os.pullEvent("key")
  onKeyPressed(key,dartMenu)
end
end

main()
Lyqyd #2
Posted 12 February 2013 - 08:58 AM
Split into new topic.
remiX #3
Posted 12 February 2013 - 04:53 PM
Encase the 'main' function into an infinite loop. Right now it only gets called once so it's exiting when you click back.

function main()
	while true do
		while inMenu do
			term.clear()
			term.setCursorPos(1,1)
			printMenu(mainMenu)
			event, key = os.pullEvent("key")
			onKeyPressed(key,mainMenu)
		end

		while inAmmo do
			term.clear()
			term.setCursorPos(1,1)
			printMenu(ammoMenu)
			event, key = os.pullEvent("key")
			onKeyPressed(key,ammoMenu)
		end
		while inDarts do
			term.clear()
			term.setCursorPos(1,1)
			printMenu(dartMenu)
			event, key = os.pullEvent("key")
			onKeyPressed(key,dartMenu)
		end
	end
end