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

Menu not working

Started by Draedo, 23 May 2014 - 03:54 PM
Draedo #1
Posted 23 May 2014 - 05:54 PM
Hello I'm very new to CC and Coding in general. I've searched for topics on multiple menus but I don't understand the help that is being given in them.

I'm writing code to control and automate a nuclear reactor and it's fueling.
I'm trying to use a menu with sub menu's.

As it stands what my code does is, when the enter key is pressed on "reactor" it clears the screen and prints the second table but omits option 1 starting at "Output Level" instead of "Temperature". On top of that on any arrow key press it returns back to the main menu. The Caveat is that on the second menu I can navigate it by pressing the down key, then pressing enter on reactor, and it will show me as being on the next item down for the second menu.

I've been staring at this code for about 12 hours now trying to do different things and I'm not sure what's going on. I know it's very messy. It used to be very different.

I've added Completely separate Variables to the second menu function. But it performs the same.

I think the issue is that it's trying to run both the MainMenu and RCmainMenu loops. however, when I put a break into the MainMenu it just tells me that there is supposed to be an "end".

Spoiler

local termWidth, termHeight = term.getSize()
local selectedItem = 1
local selectedItemRC = 1
local onMainMenu = true
local onRContMenu = false
local onPowerMenu =false
reactor = peripheral.wrap("back")


function Choice1()
term.clear()
term.setCursorPos(1,1)
RCmenuMain()
end

function Choice2()
term.clear()
term.setCursorPos(1,1)
print("Environment")
sleep(1)
end

function Exit()

end

mainMenu = {
[1] = { text = "Reactor", handler = RCmenuMain },
[2] = { text = "Environment", handler = Choice2 },
[3] = { text = "Exit", handler = Exit }
}

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

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


main()

-- Reactor Menu --

function rCont1()
term.clear()
term.setCursorPos(1,1)
print("Tempetature")
sleep(1)
end

function rCont2()
term.clear()
term.setCursorPos(1,1)
print("Output Level")
sleep(1)
end

function rCont3()
term.clear()
term.setCursorPos(1,1)
print("Fuel")
sleep(1)
end

function rCont4()
term.clear()
term.setCursorPos(1,1)
print("Shutting System Down")
rs.setOutput("left", false)
sleep(1)
end

function rCont5()
term.clear()
term.setCursorPos(1,1)
print("Power Control")
onRContMenu = false
onMainMenu = false
onPowerMenu = true
sleep(1)
end

function rCont6()
term.clear()
term.setCursorPos(1,1)
print("-Back-")
sleep(1)
onPowerMenu = false
onRContMenu = false
onMainMenu = true
end


-- Heat functions --

function heatLevel()
term.setCursorPos(1,1)
reactor.isActive()
		if reactor.isActive() == true then
		print("Reactor Is Currently: Online")
		else
		print("Reactor Is Currently: Offline")
		end
reactor.getHeat()
reactor.getMaxHeat()
danger = reactor.getMaxHeat
		if reactor.getHeat >= (.75 * danger) then
		print("!~~!WARNING! HEAT LEVEL CRITICAL!~~!")
		else
		print("Within Normal Operating Temp")
		end
print("Current Temp")
print("  ")
print(reactor.getHeat)
print("  ")
print("Max Temp")
print("  ")
print(reactor.getMaxHeat)
end


RCmenu = {
[1] = { text = "Tempetature", handler = rCont1 },
[2] = { text = "Output Level", handler = rCont2 },
[3] = { text = "Fuel", handler = rCont3 },
[4] = { text = "Emergency Shutdown", handler = rCont4 },
[5] = { text = "Power Control", handler = rCont5 },
[6] = { text = "-Back-", handler = rCont6 },
[7] = { text = "Exit", handler = Exit }
}

function printMenuRC( RCmenu )
for b=1,#RCmenu do
  if b == selectedItemRC then
   print(">> "..RCmenu[b].text)
  else
   print("   "..RCmenu[b].text)
  end
end
end

function onKeyPressedRC( keyRC, RCmenu )
if keyRC == keysRC.enter then
  onItemSelectedRC(RCmenu)
elseif keyRC == keysRC.up then
  if selectedItemRC > 1 then
   selectedItemRC = selectedItemRC - 1
  end
elseif keyRC == keysRC.down then
  if selectedItemRC < #RCmenu then
   selectedItemRC = selectedItemRC + 1
  end
end
end

function onItemSelectedRC( RCmenu )
RCmenu[selectedItemRC].handler()
end

function RCmenuMain()
b=1
i=2
onMainMenu = false
onRContMenu = true
while onRContMenu == true do
  term.clear()
  term.setCursorPos(1,1)
  printMenuRC( RCmenu )
  eventRC, keyRC = os1.pullEvent("keyRC")
  onKeyPressedRC(keyRC,RCmenu)
end
end

RCmenuMain()
wieselkatze #2
Posted 23 May 2014 - 07:05 PM
I've not looked at all of the code, but the first thing that stands out is the "os1.pullEvent("keyRC")".

Firstly there isn't os1.pullEvent(), I think you meant os.pullEvent, because you haven't defined os1 before.

Secondly the name of the event is "key", not "keyRC".

So, change the "os1.pullEvent("keyRC")" to "os.pullEvent("key")" and reply if there are still problems.
Draedo #3
Posted 23 May 2014 - 07:11 PM
I did what you suggested and it returns a nil at line 58.
http://pastebin.com/PpPBGFvJ


function onItemSelected( menu )
 menu[selectedItem].handler()
end
wieselkatze #4
Posted 23 May 2014 - 07:28 PM
Ok, I think I got it fixed now.
You declared RCMenuMain after you called it.
So you have you main function properly working, but if you select the first item, it calls RCMenuMain, which is currently not even defined yet.
Also in your onKeyPressesRC function you have to replace keysRC.enter, keysRC.up and keysRC.down with keys.enter and so on.
If you've done that, move the whole part from line 73 to line 203 up (above) choice1().
Also just delete line 205.

Fixed code could look like this:
http://pastebin.com/pZDKJp6c

~wieselkatze
Draedo #5
Posted 23 May 2014 - 08:07 PM
Thanks dude! That works,. after staring at it for hours on end with no results this is a huge sigh of relief.

though I just keep thinking to myself, there has to be a simpler way but this will work for now.

I'll keep in mind defining something BEFORE you call it.

If you have any other advice i'm all ears. Thanks again!