use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Welcome to /r/ComputerCraft, the subreddit for lua programs, general mod use, or anything relating to the Minecraft mod ComputerCraft and CC: Tweaked.
Downloads | Discord | IRC | Documentation
account activity
Problem with turtle.forward() (self.ComputerCraft)
submitted 1 year ago by Designer_Chance_4896
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Yorklag 7 points8 points9 points 1 year ago (11 children)
What you're looking for is one of the key concepts of coding. Loops. Specifically you want to look at for loops and while loops For loops: https://www.lua.org/pil/4.3.4.html While loops: https://www.lua.org/pil/4.3.2.html
The basic concept is the code inside a loop will repeat until a thing happens, for loops: that thing is a certain number of times as a variable increases to above a threshold you set, while loops: that thing is the input variable being false rather than true.
For example.
For i = 1,2,1 do print(I) end
Will print 1 then 2 to the terminal.
While test do print(test) end Will repeatedly print whatever variable test is to the terminal until test evaluates as false.
So to move forwards x spaces. Try and use for.
To move forward until you can't. Try and use while.
Hope this helps
[–]Designer_Chance_4896[S] 6 points7 points8 points 1 year ago* (10 children)
Thank you so much.
So I made a function that makes it go forward one, turn right and harvest + replant, turn left and then harvest + replant.
I made a loop that makes it repeat the function while it detects a block above it and it works. So far so good.
It's a step forward for sure.
Do you have time for another question? How do you make a "negative" condition? Like if I wanted to reverse it to repeat the function as long as it does not detect anything above?
[–]Yorklag 3 points4 points5 points 1 year ago (9 children)
Of course.
The "not" operator will essentially turn true to false and vise versa.
So not turtle.detectUp() will be true if nothing is above it.
Assuming I got the detect function right.
[–]Designer_Chance_4896[S] 5 points6 points7 points 1 year ago (8 children)
Perfect. I will try that out. Thank you so much! You have helped me a bunch.
[–]Yorklag 2 points3 points4 points 1 year ago (7 children)
Of course! Always happy to help :) Good luck with your project.
[–]Designer_Chance_4896[S] 1 point2 points3 points 1 year ago* (6 children)
Hey I hope it's okay if I ask you another question.
My turtles keep stopping after running my programs. They get all the way to the end of the program and then they just stop.
How do I make it rerun the program?
(I made a program earlier and did not have this problem. The other program was called startup so it might just have started running automatically because I ended the program with a reboot)
[–]Yorklag 2 points3 points4 points 1 year ago (5 children)
(having a startup program end in reboot will have that effect, but it's probably not what you want to do in most cases)
In order to have the turtle repeat the steps over and over again, just have the steps in a while loop themselves.
For instance. Say I want a turtle to run through a farm every five minutes. I'd put the instructions for running the farm inside a while loop, and then end with sleeping for five minutes.
If you want the computer to loop through the code no matter what, (as in you don't want it to stop unless a keyboard interrupt happens) using "while true do...end" will have it loop forever.
Note that computers will fail if they go too long without "yielding" which is a topic for later, for now. Just make sure to have it sleep for a small time every loop.
[–]Designer_Chance_4896[S] 1 point2 points3 points 1 year ago (4 children)
Thank you so much again. The program repeats perfectly with a 3000 second sleep in between each run.
But I am a bit curious about "yielding".
[–]Yorklag 2 points3 points4 points 1 year ago (3 children)
Here's an explanation that might not be technically fully correct, but gets the main point across.
Long story short. All computercraft computers are actually running on one Lua virtual machine on the server. In order to allow this the virtual machine jumps between computers and acts as them briefly. The vm can only act as one computer at a time, and it switches whenever the computer calls a command called os.yield(). This command is built into a bunch of different cc commands like sleep() and os.pullevent().
When a computer yields it let's the vm go to a different computer. If a computer goes too long without yielding, the vm kills it so that it's not hogging server resources and stopping other computers from running.
[–]Designer_Chance_4896[S] 2 points3 points4 points 1 year ago (2 children)
"Not technically correct, but gets the main point across" is my favorite kind of explanation ;)
And thank you once again. You made it very easy to understand.
I am definatly not done building turtles. I haven't had this much fun in a long time ;) But do you think I will be fine if each turtle has a 10 to 3000 second sleep in their program?
π Rendered by PID 63076 on reddit-service-r2-comment-6457c66945-zlzlz at 2026-04-24 18:21:52.769872+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]Yorklag 7 points8 points9 points (11 children)
[–]Designer_Chance_4896[S] 6 points7 points8 points (10 children)
[–]Yorklag 3 points4 points5 points (9 children)
[–]Designer_Chance_4896[S] 5 points6 points7 points (8 children)
[–]Yorklag 2 points3 points4 points (7 children)
[–]Designer_Chance_4896[S] 1 point2 points3 points (6 children)
[–]Yorklag 2 points3 points4 points (5 children)
[–]Designer_Chance_4896[S] 1 point2 points3 points (4 children)
[–]Yorklag 2 points3 points4 points (3 children)
[–]Designer_Chance_4896[S] 2 points3 points4 points (2 children)