Jump to content




Combine turtle.inspect() with while?


  • You cannot reply to this topic
3 replies to this topic

#1 Avarion

  • Members
  • 29 posts

Posted 29 June 2015 - 06: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?

#2 The_Cat

  • Members
  • 119 posts

Posted 29 June 2015 - 06: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 by The_Cat, 29 June 2015 - 06:33 PM.


#3 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 29 June 2015 - 11:47 PM

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.

#4 Avarion

  • Members
  • 29 posts

Posted 30 June 2015 - 08: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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users