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

Combine turtle.inspect() with while?

Started by Avarion, 29 June 2015 - 05:22 PM
Avarion #1
Posted 29 June 2015 - 07:22 PM
Hi,

I try to make a turtle digging while there is gravel in front of it.

My first test was


  if turtle.forward() == false then
   if turtle.inspect() == true, "minecraft:gravel" then
    while turtle.inspect() == true, "minecraft:gravel" do
	 turtle.dig()
	 sleep(1)
    end
   else
    break
   end
  end

Unfortunately it seems the while command is not able to handle both values at once. Is there a way to make it work or do I have to fill the inspect information to variables first and compare then?
The_Cat #2
Posted 29 June 2015 - 07:32 PM
There is a simpler way of doing this:

function moveForward() --# Put in function so you can call it
while not turtle.forward() do --# Turtle forward is false
	if turtle.detect() then --# if there is a block in front of it, it will dig it
		turtle.dig()
		os.sleep(.5)
	else
		turtle.attack() --# If it didn't detect then there a player or a mob in the way so it will attack
	end
end
end
moveForward() --# Move turtle forward one block
Edited on 29 June 2015 - 05:33 PM
Bomb Bloke #3
Posted 30 June 2015 - 12:47 AM
As Cat says, you're better of detecting if anything is blocking the turtle's path. Gravel isn't the only block-type affected by gravity, and there's really no need to know exactly what's there.

But yeah, to make your original code block work, you'd need to 1) deal with turtle.inspect()'s multiple return values, and 2) extract the block name from the table that is the second return value. This is best done by saving the values to variables, checking that the first is true, and then performing the table index into the second only if so.
Avarion #4
Posted 30 June 2015 - 09:11 AM
Thank you both. I've made it work now with variables.

As soon I'm finished with work I'll change the code from inspect to detect.