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".
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()