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

[Question] Turtle's going in circles

Started by Gorak, 29 June 2012 - 08:21 PM
Gorak #1
Posted 29 June 2012 - 10:21 PM
I tried to make a sinple program for a turtle to move five blocks, turn left, place a torch, then turn right again:


turtle.forward(5)
turtle.turnLeft()
while turtle.detect() do
turtle.turnRight()
end
turtle.forward(5)
turtle.turnLeft
while not turtle.detect() do
turtle place(1)
turtle.turnRight()
end

Instead, it just makes left turns. What am I doing wrong?
OmegaVest #2
Posted 29 June 2012 - 10:26 PM
Place does not accept an integer, it just places down whatever is in the selected slot. Use turtle.select(num) and then turtle.place().

Further, neither does turtle.forward(). If you want it to go multiple blocks forward, use a for loop.
MysticT #3
Posted 29 June 2012 - 10:28 PM
Always try to understand what you'r writing. Commentig the code helps to see the errors in program logic:

turtle.forward(5) -- forward has no parameters, this just goes forward once
turtle.turnLeft() -- turn left
while turtle.detect() do -- while there's a block in front of the turtle
  turtle.turnRight() -- turn right
end
turtle.forward(5) -- same as above, move forward once
turtle.turnLeft -- missing brackets. turn left
while not turtle.detect() do -- while there isn't a block in front of the turtle
  turtle place(1) -- place has no parameters, place a block from the selected slot (using turtle.select(<slot>), 1 by default)
  turtle.turnRight() -- turn right
end
Gorak #4
Posted 29 June 2012 - 10:47 PM
Thank you both. MysticT, I implemented the changes you listed. When the turtle encounters no object to its left, it now spins in a circle and places torches around it before coming to a stop. It behaves appropriately when there are objects, but it then stops after moving two meters. I was under the impression that it would loop through the program until it encountered an obstacle.
reptar #5
Posted 01 July 2012 - 02:27 AM
Here try this:

while true do -- infinite loop to go down a hallway placing torches
for a=1,5 do -- loop to do function five times
turtle.forward() -- function - move forward
end -- ends the loop
turtle.turnLeft() -- turns left
turtle.select(1) -- selects slot 1, do this instead of using an integer in place command
turtle.place() -- places a block from the previously selected slot
turtle.turnRight() --turtle turn right again
end -- ends the overall loop

i added an infinite loop so it can go down hallways placing a torch every 5 blocks, to get rid of this so it only does it once, remove the first and last lines

btw I'm pretty new to this so i would appreciate criticism on my code