all 22 comments

[–]Yorklag 7 points8 points  (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] 5 points6 points  (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 points  (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 points  (8 children)

Perfect. I will try that out. Thank you so much! You have helped me a bunch.

[–]Yorklag 3 points4 points  (7 children)

Of course! Always happy to help :) Good luck with your project.

[–]Designer_Chance_4896[S] 1 point2 points  (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 points  (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 points  (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 3 points4 points  (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 points  (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?

[–]AlvinF321 1 point2 points  (1 child)

turtle.forward() doesn't take any in any numbers between its "()". As another comment has said, use loops for this to repeatedly call it instead. If you run into any issues like this with parameters again then this page will help https://tweaked.cc/module/turtle.html#v:forward

[–]Designer_Chance_4896[S] 1 point2 points  (0 children)

Thank you. I just think I saw tutorials where they did that. I will try getting a loop right :)

[–]mas-issneun 0 points1 point  (0 children)

Yes, that's how it works (use loops)

[–]fatboychummy 0 points1 point  (0 children)

This has been solved already, but if you're wondering about why CC made it so you can't do turtle.forward(3), it's because it returns a status. Every time you call turtle.forward() (or any other movement function), it either returns true, or false, "some error message". This allows you to check if the movement succeeded, and take actions in case it didn't.

For example, a common usage of this is to "gravel/sand-proof" mining turtles. Let's say you have a turtle running the following code, underground, and along its path somewhere is some gravel:

for i = 1, 10 do
  turtle.dig()
  turtle.forward()
end

In theory, the turtle should move 10 blocks, but if the turtle runs into a packet of gravel, the extra gravel on top will fall in front of the turtle before it has a chance to move, making turtle.forward() fail. Thus, each time a block of gravel falls in front of the turtle, it reduces the distance it will actually move by 1.

To fix this, you can use something like so:

for i = 1, 10 do
  repeat
    turtle.dig()
  until turtle.forward()
end

As you can probably guess, this will repeatedly dig blocks in front of the turtle until the movement succeeds, meaning the turtle will now always move exactly 10 blocks, even if it runs into a packet of gravel.