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

Stop a Program From Ending?

Started by Agentmass, 17 May 2012 - 09:55 PM
Agentmass #1
Posted 17 May 2012 - 11:55 PM
So I just installed ComputerCraft on my server and I have figured out how to do some very basic stuff. One thing that still has me puzzled is how to prevent a program from ending after the program has been executed.


For example, I have this program here, and once I type Hello or Bye, the program just ends. I would like to have it so that I can continue to type Hello and Bye without the program ending. Here is a screenshot of my program. Sorry I couldn't paste the text because I still have not figured out how to copy paste in ComputerCraft ^^. I would appreciate it if someone could give me an example to how I can fix this.

Thanks for any help.



Uploaded with ImageShack.us
MysticT #2
Posted 18 May 2012 - 12:06 AM
You need to use a loop:

while true do
  -- your code
end
That will make your program run until the computer shuts down or you terminate the program (Ctrl+T).
And for copying the code, you have to open the file in the .minecraft/saves/<YourWorld>/computer/<ID> folder, where ID is the id of the computer that has the file.
Agentmass #3
Posted 18 May 2012 - 12:18 AM
I did try doing that before. The problem with that is that it prints 2 until the program crashes. Is there a way to make the program loop once, and then wait for another user input so the program does not crash? I guess it's doing this because Input is still equal to Hello so it keeps spamming it. I'm not sure how to fix this though.


Input = read()
while true do
if Input == "Hello" then
I = 2
print (I)
else
if Input == "Bye" then
I = 3
print (I)
   end
end
end
MathManiac #4
Posted 18 May 2012 - 12:52 AM
First, please indent your code.

You need to use a loop:

while true do
  -- your code
end
That will make your program run until the computer shuts down or you terminate the program (Ctrl+T).
And for copying the code, you have to open the file in the .minecraft/saves/<YourWorld>/computer/<ID> folder, where ID is the id of the computer that has the file.

Second, MysticT means to place ALL you code there. :P/>/>
MysticT #5
Posted 18 May 2012 - 01:04 AM
Yes, you have to put the first line in the loop also, that way it will ask for the input then print what corresponds to that input, then ask for it again and print … etc.
And there's other types of loops you can use, depending on what you need you can use one or another. Just look at some tutorial on lua loops.
Agentmass #6
Posted 18 May 2012 - 01:27 AM
I see. So I have to set Input = read() inside the loop because this way it rechecks the code to see what the user wrote right? And in the next loop iteration, then the Input is equal to nothing so it prints nothing right? Also, this works very well. Thank you.

Edit: Just one last question. How could I create a function that pulses redstone? For example, I want to repeat this code a specific number of times, and I don't want to write it 30 times to get 30 pulses.

redstone.setOutput("back",true)
sleep(0.5)
redstone.setOutput("back",false)
sleep(0.5)

Edit2: Nevermind, I figured it out ^^.
MathManiac #7
Posted 19 May 2012 - 04:23 AM
-snip-

Just one last question. How could I create a function that pulses redstone? For example, I want to repeat this code a specific number of times, and I don't want to write it 30 times to get 30 pulses.

redstone.setOutput("back",true)
sleep(0.5)
redstone.setOutput("back",false)
sleep(0.5)

Edit2: Nevermind, I figured it out ^^.

Oh, those for do … end blocks? I love those. My fav is the ones that uses ipairs.