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

Need help with the turtle.inspect() command

Started by tvc, 25 November 2014 - 12:11 AM
tvc #1
Posted 25 November 2014 - 01:11 AM
So I'm building a large mining program…( Yes I Know there are like 900 i could use but you know pride)

But im running into issues with a if then loop. The turtle is doing both the if and the else. Ill Post a copy of my code here

The area of the code is between line 138 and 165. Ill also include those lines here
Spoiler

local function right()
turtle.turnRight()
  for i = 1,10 do
  tCheck = true
  while tCheck == true do
   p1,p2 = turtle.inspect()
   if p1 == true then
	for k,v in pairs(p2) do
	 if v == "ComputerCraft:CC-Turtle" then
	  sleep(.1)
	 else
	  turtle.dig()
	  turtle.forward()
	  tCheck = false
	 end
	end
   else
	turtle.dig()
	turtle.forward()
	tCheck = false
   end
  end
end
turtle.turnLeft()
end

right()
Bomb Bloke #2
Posted 25 November 2014 - 01:17 AM
This chunk here:

        for k,v in pairs(p2) do
         if v == "ComputerCraft:CC-Turtle" then
          sleep(.1)
         else
          turtle.dig()
          turtle.forward()
          tCheck = false
         end
        end

Assuming the p2 table contains multiple entries (and IIRC it always will), so long as at least one of them is "ComputerCraft:CC-Turtle" it'll sleep for a tenth of a second… and so long as at least one of them is anything else, it'll dig and go forward.

Presumably what you want to do is make sure NONE of the entries are "ComputerCraft:CC-Turtle" before you attempt to dig:

        local test = false

        for k,v in pairs(p2) do
         if v == "ComputerCraft:CC-Turtle" then
          test = true
          break
         end
        end

        if test then
         sleep(.1)
        else
         turtle.dig()
         turtle.forward()
         tCheck = false
        end
tvc #3
Posted 25 November 2014 - 01:22 AM
This chunk here:

		for k,v in pairs(p2) do
		 if v == "ComputerCraft:CC-Turtle" then
		  sleep(.1)
		 else
		  turtle.dig()
		  turtle.forward()
		  tCheck = false
		 end
		end

Assuming the p2 table contains multiple entries (and IIRC it always will), so long as at least one of them is "ComputerCraft:CC-Turtle" it'll sleep for a tenth of a second… and so long as at least one of them is anything else, it'll dig and go forward.

Presumably what you want to do is make sure NONE of the entries are "ComputerCraft:CC-Turtle" before you attempt to dig:

		local test = false

		for k,v in pairs(p2) do
		 if v == "ComputerCraft:CC-Turtle" then
		  test = true
		  break
		 end
		end

		if test then
		 sleep(.1)
		else
		 turtle.dig()
		 turtle.forward()
		 tCheck = false
		end

perfect it work thank you very much
Bomb Bloke #4
Posted 25 November 2014 - 04:00 AM
Actually, come to think of it, looking at the docs for turtle.inspect() we can see the name of the key that'll have the data we're interested in. Thus we can shrink the code further:

        if p2.name == "ComputerCraft:CC-Turtle" then
         sleep(.1)
        else
         turtle.dig()
         turtle.forward()
         tCheck = false
        end

No need for a "for" loop.