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

Turtle moving forward then back

Started by goblim, 05 June 2015 - 11:41 PM
goblim #1
Posted 06 June 2015 - 01:41 AM

while true do
p1,p2 = turtle.inspectDown()
if p1 == true then
for k,v in pairs(p2) do
if v == "minecraft:stone" then
turtle.forward()
end
if v ~= "minecraft:stone" then
turtle.back()
turtle.left()
end
end
end
end




this code is a path follower that uses turtle.inspect() to find the path. The problem I have is that the turtle moves forward but immediately moves back and it does this continually. The turtle moves on the stone and never gets off the stone. I cannot figure out the reason for this. Please help.
Edited on 05 June 2015 - 11:50 PM
Dragon53535 #2
Posted 06 June 2015 - 01:52 AM
Usually the turtle.inspect table returned is formatted like this, mind you I don't know the exact one.

{
  name = "minecraft:stone"
  id = 2
  meta = "fd"
}
If you notice, there's the name and id at least, so what's happening is this:

Loop one of for k,v in pairs(p2) do
k = 'name'
v = 'minecraft:stone'
so the turtle moves forward
loop two of the for loop
k = 'id'
v = 2
so the turtle then moves backwards.
Fix you want.

if p1 then
  if p2.name == "minecraft:stone" then
    turtle.forward()
  else
    turtle.back()
    turtle.turnLeft()
  end
end
goblim #3
Posted 06 June 2015 - 01:56 AM
thanks I try it