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

Code for detecting blocks

Started by jamco2014, 27 January 2013 - 03:54 PM
jamco2014 #1
Posted 27 January 2013 - 04:54 PM
Title: Code for detecting blocks

Hello, I am new to computer craft and was wondering if there was any way I could make a program that would allow my turtle to turn right when it hit a wall but keep running afterward?

turtle.digDown() --- when there is no block in front of it
turtle.placeDown()
turtle.forward()
turtle.turnRight() --when it hits a wall
Engineer #2
Posted 28 January 2013 - 09:06 AM
Dont know in what perspective it is used in, but here is what I came up with:

while true do -- infinite loop
  if turtle.detect() == false then
    turtle.digDown()
  end
  turtle.placeDown()
  turtle.forward()
  if turtle.detect() then
     turtle.turnRight()
  end
sleep(0) -- otherwise the loop will yield
end

I hope I helped