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

Return to top

Started by lempo123, 04 August 2012 - 06:34 PM
lempo123 #1
Posted 04 August 2012 - 08:34 PM
Hi i have been working on a simple code to turn a powerplant on and off, i got it to work a bit but after its done turning on i want it to return to the top without restarting the program so i can then turn it off again.
(is there a command for it to return to top?)
MysticT #2
Posted 04 August 2012 - 08:39 PM
You can use a loop. The best (and easier) would be to use a while loop:

while true do -- infinite loop
  -- your code
end
lempo123 #3
Posted 04 August 2012 - 08:41 PM
sorry i dont really understand. ( i am very new to this )
MysticT #4
Posted 04 August 2012 - 08:46 PM
The while loop repeats everything inside untile the condition is false. Using "while true do", it makes an infinite loop.
Just add your code inside the while loop (where it says – your code) and when it reaches the end it goes back to the start.
lempo123 #5
Posted 04 August 2012 - 08:47 PM
i tired but i just got an error
i would show you the code but i dont know how to copy it from the ingame computer
lempo123 #6
Posted 04 August 2012 - 09:10 PM
no?
MysticT #7
Posted 04 August 2012 - 09:40 PM
What's the error?
You only need to add the line

while true do
at the start, and

end
at the bottom. That should work for simple programs. If you still have problems, post the code. If you'r on single player, go to .minecraft/saves/<YourWorld>/computer/<Computer Id>, and open the file with a text editor and copy the code.
lempo123 #8
Posted 04 August 2012 - 09:50 PM

i dont understand what ive done wrong
MysticT #9
Posted 04 August 2012 - 10:00 PM
Well, the error is in the code inside the loop. You need to change the second if to an elseif.
This should work:

while true do
  term.clear() -- clear the screen
  term.setCursorPos(1, 1) -- and move the cursor to the start
  print("Reactor 1 Control")
  write("Set status: ")
  local input = read()
  if input == "on" then
    rs.setOutput("top", false)
    print("Reactor 1 ON")
    sleep(1)
  elseif input == "off" then
    rs.setOutput("top", true)
    print("Reactor 1 OFF")
    sleep(1)
  end
end
lempo123 #10
Posted 04 August 2012 - 10:17 PM
Thank you!
lempo123 #11
Posted 05 August 2012 - 05:29 PM
What would i do if i want to add a third one and make it work?
sjele #12
Posted 05 August 2012 - 05:35 PM

while true do
  term.clear() -- clear the screen
  term.setCursorPos(1, 1) -- and move the cursor to the start
  print("Reactor 1 Control")
  write("Set status: ")
  local input = read()
  if input == "on" then
    rs.setOutput("top", false)
    print("Reactor 1 ON")
    sleep(1)
  elseif input == "off" then
    rs.setOutput("top", true)
    print("Reactor 1 OFF")
    sleep(1)
  elseif input == "nextinput" then
    --rest of code                                       
end
end