7 posts
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?)
1604 posts
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
7 posts
Posted 04 August 2012 - 08:41 PM
sorry i dont really understand. ( i am very new to this )
1604 posts
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.
7 posts
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
7 posts
Posted 04 August 2012 - 09:10 PM
no?
1604 posts
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.
7 posts
Posted 04 August 2012 - 09:50 PM
i dont understand what ive done wrong
1604 posts
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
7 posts
Posted 04 August 2012 - 10:17 PM
Thank you!
7 posts
Posted 05 August 2012 - 05:29 PM
What would i do if i want to add a third one and make it work?
318 posts
Location
Somewhere on the planet called earth
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