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

Piston Door Help

Started by 072608, 14 May 2012 - 12:28 AM
072608 #1
Posted 14 May 2012 - 02:28 AM
I've made this program it Opens and closes and Piston Door (3*3)
All this works, i want make stay in the program unless you use the 3 command
I've try but it either Mess up or won't work


shell.run("clear")
print (" Panic Room Door control 0.2v")
print (" ----------------------------")
print (" [1] Open Door ")
print (" [2] Close Door ")
print (" [3] Exit ")
print (" [4] Shutdown Computer ")
input = read.io()
yes = "1"
no  = "2"
yo  = "3"
la  = "4"
if input == yes then
redstone.setOutput ("back", false)
shell.run("startup")
end
if input == no then
redstone.setOutput ("back", true)
shell.run("startup")
end
if input == yo then
shell.run("clear")
function mainmenu()
end
end
if input == la then
sleep(1)
os.shutdown()
end

Luanub #2
Posted 14 May 2012 - 02:33 AM
From what I can tell if option 3 is selected you want it to reprint the menu?

If so just place it in a while loop. the function that you have declared in the if state does basically nothing so remove it. Try this.


while true do -- start infinite loop
shell.run("clear")
print (" Panic Room Door control 0.2v")
print (" ----------------------------")
print (" [1] Open Door ")
print (" [2] Close Door ")
print (" [3] Exit ")
print (" [4] Shutdown Computer ")
input = read.io()
yes = "1"
no  = "2"
yo  = "3"
la  = "4"
if input == yes then
redstone.setOutput ("back", false)
shell.run("startup")
break -- break loop before continuing
end
if input == no then
redstone.setOutput ("back", true)
shell.run("startup")
break -- break the loop before proceeding
end
if input == yo then
sleep(0) -- to avoid an error
if input == la then
sleep(1)
os.shutdown()

end
end -- end the loop

If you want use functions then it should look something like

function mainmenu()
--insert code for mainmenu here
end

mainmenu() -- to call the function
Zalerinian #3
Posted 14 May 2012 - 04:06 AM
If you're trying to exit the program and go to the CraftOS screen when you select the third option, the code would not be the function for the main menu, but rather simply return.


shell.run("clear")
print (" Panic Room Door control 0.2v")
print (" ----------------------------")
print (" [1] Open Door ")
print (" [2] Close Door ")
print (" [3] Exit ")
print (" [4] Shutdown Computer ")
input = read.io()
yes = "1"
no  = "2"
yo  = "3"
la  = "4"
if input == yes then
redstone.setOutput ("back", false)
shell.run("startup")
end
if input == no then
redstone.setOutput ("back", true)
shell.run("startup")
end
if input == yo then
shell.run("clear")
return
end
end
if input == la then
sleep(1)
os.shutdown()
end

this will simply shut down the PROGRAM, NOT the COMPUTER
Grim Reaper #4
Posted 15 May 2012 - 04:58 AM
If you're trying to get the program to exit without shutting down the computer, you could try the previous poster's code ^^ or this little snippet:

shell.run("clear")
function printMainMenu()
print (" Panic Room Door control 0.2v")
print (" ----------------------------")
print (" [1] Open Door ")
print (" [2] Close Door ")
print (" [3] Exit ")
print (" [4] Shutdown Computer ")
end
while true do
shell.run("clear") -- Moving this line up here will simply clear the screen every time until the loop is broken
printMainMenu()

input = read.io()
yes = "1"
no  = "2"
yo  = "3"
la  = "4"

if input == yes then
redstone.setOutput ("back", false)
shell.run("startup")
elseif input == no then
redstone.setOutput ("back", true)
shell.run("startup")
end
elseif input == yo then break
-- the break keyword will 'break' or end/exit the current loop. Our while true loop will stop,
-- the program will end, and the user will be prompted with the familiar CraftOS screen.
elseif input == la then
sleep(1)
os.shutdown()
end

Similarly, if you wanted to get the computer to just reboot so the user would be prompted with the boot up screen ("CraftOS 1.3 >_") then you could try this:

shell.run("clear")
function printMainMenu()
print (" Panic Room Door control 0.2v")
print (" ----------------------------")
print (" [1] Open Door ")
print (" [2] Close Door ")
print (" [3] Exit ")
print (" [4] Shutdown Computer ")
end
while true do
shell.run("clear") -- Moving this line up here will simply clear the screen every time until the loop is broken
printMainMenu()

input = read.io()
yes = "1"
no  = "2"
yo  = "3"
la  = "4"

if input == yes then
redstone.setOutput ("back", false)
shell.run("startup")
elseif input == no then
redstone.setOutput ("back", true)
shell.run("startup")
end
elseif input == yo then os.reboot()
elseif input == la then
sleep(1)
os.shutdown()
end
MathManiac #5
Posted 17 May 2012 - 04:17 AM
Here are some things that can be of mind:

os.shutdown() -- shuts the computer down
os.reboot() -- restarts the computer
return -- if used by itself in lua code OUTSIDE A FUNCTION, then it will end the program.
no -- when it is in the code bar, it looks suspicious... maybe use a different variable such as "novar"

Also, I hate a wall of text. Use indentions, for it not only makes code easier to see, it is good practice.
072608 #6
Posted 23 May 2012 - 08:50 PM
what I meant was to make not exit out of program when you enter something random
MysticT #7
Posted 23 May 2012 - 08:57 PM
Use a loop:

local function clear() -- way better that using shell.run("clear")
  term.clear()
  term.setCursorPos(1, 1)
end

while true do
  clear()
  print (" Panic Room Door control 0.2v")
  print (" ----------------------------")
  print (" [1] Open Door ")
  print (" [2] Close Door ")
  print (" [3] Exit ")
  print (" [4] Shutdown Computer ")
  local input = read()
  if input == "1" then
	redstone.setOutput ("back", false)
  elseif input == "2" then
	redstone.setOutput ("back", true)
  elseif input == "3" then
	clear()
	break
  elseif input == "4" then
	print("Goodbye")
	sleep(1)
	os.shutdown()
  end
end
I changed some other things also, like using elseif and the clear function.
072608 #8
Posted 24 May 2012 - 03:46 AM
thanks MysticT
it does what wanted too do :)/>/>