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

How do I keep turtles from messing up forward movement?

Started by xzero121, 17 December 2012 - 05:39 AM
xzero121 #1
Posted 17 December 2012 - 06:39 AM
I am trying to figure out a code so that turtle.forward() within a loop is not interrupted by an entity such as a mob being in the way. I have programmed a turtle for branch mining and usually the first 3 or 4 branches are fine, but then after that it does not go deep enough into the branches.

I can include all of my code if I need too, but right now all I am going to post is this:

function go()
repeat
turtle.forward()
until turtle.forward() == success
end
This was my attempt at making sure the turtle did not lose count, it failed.

Sorry if this has been asked before but I looked and did not come up with anything when I searched.
dissy #2
Posted 17 December 2012 - 07:08 AM
turtle.forward() will return true or false depending if it moved forward.

For a mining example:

while not turtle.forward() do
  turtle.dig()
end

A counting movement example:

while turtle.forward() do
  count = count + 1
end

Hopefully that helps give some ideas how to structure your code!
xzero121 #3
Posted 17 December 2012 - 08:38 AM
turtle.forward() will return true or false depending if it moved forward.

For a mining example:

while not turtle.forward() do
  turtle.dig()
end

A counting movement example:

while turtle.forward() do
  count = count + 1
end

Hopefully that helps give some ideas how to structure your code!

Thanks, I think that might help a lot actually.
KaoS #4
Posted 17 December 2012 - 10:03 AM
also just while not turtle.forward() do end to keep trying until it works
theoriginalbit #5
Posted 17 December 2012 - 11:07 AM
if you are using CC1.4 you can also command your turtle to attack if it cant move, digs and still cant move, using turtle.attack()
Henness #6
Posted 17 December 2012 - 02:33 PM
My favorite way of doing it is like this

	local moved=turtle.forward()
	if not moved and turtle.detect() then
	 repeat dig() until turtle.forward()
	 local moved=true
	elseif not moved and not turtle.detect() then
	 repeat turtle.attack() until turtle.forward()
	end
if it doesn't move and it detects a block then it digs
if it doesn't move and it doesn't detect a block then it attacks
theoriginalbit #7
Posted 17 December 2012 - 02:36 PM
My favorite way of doing it is like this

	moved=forward()
	if not moved and turtle.detect() then
	 repeat dig() until forward()
	elseif not moved and not turtle.detect() then
	 repeat turtle.attack() until forward()
	end
if it doesn't move and it detects a block then it digs
if it doesn't move and it doesn't detect a block then it attacks

obviously forward is a function you have made? or else you would need to call turtle.forward()
Henness #8
Posted 17 December 2012 - 02:37 PM
My favorite way of doing it is like this

	moved=forward()
	if not moved and turtle.detect() then
	 repeat dig() until forward()
	elseif not moved and not turtle.detect() then
	 repeat turtle.attack() until forward()
	end
if it doesn't move and it detects a block then it digs
if it doesn't move and it doesn't detect a block then it attacks

obviously forward is a function you have made? or else you would need to call turtle.forward()

ya its a function I made I'll change that
theoriginalbit #9
Posted 17 December 2012 - 02:42 PM
My favorite way of doing it is like this

	moved=forward()
	if not moved and turtle.detect() then
	 repeat dig() until forward()
	elseif not moved and not turtle.detect() then
	 repeat turtle.attack() until forward()
	end
if it doesn't move and it detects a block then it digs
if it doesn't move and it doesn't detect a block then it attacks

obviously forward is a function you have made? or else you would need to call turtle.forward()

ya its a function I made I'll change that

cool, just checking :)/> and just incase someone who reads it doesn't realise ;)/> :P/>