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

Turtle wont compare

Started by lramos15, 21 January 2013 - 05:51 AM
lramos15 #1
Posted 21 January 2013 - 06:51 AM
When I make my program that clears out some land it works until the very end I try to get
it to stop by comparing a block in slot 16 with a block I placed at the end it just keeps going please
help here is a pastebin link http://pastebin.com/LUrzWiZA

Thanks, lramos15
SuicidalSTDz #2
Posted 21 January 2013 - 06:55 AM
print("Starting test program")
function compare()
if turtle.compare() == false then
dig()
else
print("We have reached the end")
end
end
function check()
while turtle.forward() == true do
forward()
end
end
function forward()
turtle.digDown()
end
function dig()
  check()
  turtle.turnRight()
  turtle.forward()
  turtle.digDown()
  turtle.turnRight()
  check()
  turtle.turnLeft()
  turtle.forward()
  turtle.digDown()
  turtle.turnLeft()
end
turtle.select(16)
while true do
compare()
end

You must do turtle.compare(), not compare()

EDIT: Just saw your function, you are telling your turtle to do compare() while true, but it is always true.

EDIT: Indenting your code also helps understand it better ;)/> Like so:

print("Starting test program")
function compare()
   if turtle.compare() == false then
	  dig()
   else
	  print("We have reached the end")
   end
end
function check()
   while turtle.forward() == true do
	  forward()
   end
end
function forward()
turtle.digDown()
end
function dig()
  check()
  turtle.turnRight()
  turtle.forward()
  turtle.digDown()
  turtle.turnRight()
  check()
  turtle.turnLeft()
  turtle.forward()
  turtle.digDown()
  turtle.turnLeft()
end
turtle.select(16)
while true do
compare()
end
lramos15 #3
Posted 21 January 2013 - 07:30 AM
I'm still confused my If statement says if turtle.compare() == false then
dig() – another function i made
else
print("The job is all done")
I want it so if the block that is in slot 16 is the same as the one in front of it the program stops
remiX #4
Posted 21 January 2013 - 08:29 AM
Can I ask you why you're making forward() function digDown? I guess if it does the job, it's good :)/>

Try this,

print("Starting test program")

function check()
	while turtle.forward() do
		forward()
	end
end

function forward()
	turtle.digDown()
end

function dig()
  check()
  turtle.turnRight()
  turtle.forward()
  turtle.digDown()
  turtle.turnRight()
  check()
  turtle.turnLeft()
  turtle.forward()
  turtle.digDown()
  turtle.turnLeft()
end

turtle.select(16)
while true do -- infinite loop
	if turtle.compare() then
		print("We have reached the end!")
		break -- stop the loop so the program stops
	else
		dig()
	end
end
lramos15 #5
Posted 21 January 2013 - 08:59 AM
It still wont stop when it encounters yellow wool i dont know whats up
remiX #6
Posted 21 January 2013 - 09:07 AM
Try using another block? When it's at the block, open the turtle and check which slot is selected. Also, turtle.compare() compares forward to the turtle btw, not everywhere if you didn't know.
lramos15 #7
Posted 21 January 2013 - 10:19 AM
I know that when it sees the block in front of it it just turns because of another line of code i dont know how to fix it
ChunLing #8
Posted 21 January 2013 - 02:13 PM
Post your current code.
lramos15 #9
Posted 21 January 2013 - 02:16 PM
Here http://pastebin.com/srJ57Ddn i have no idea whats wrong
ChunLing #10
Posted 21 January 2013 - 03:12 PM
Okay, where exactly are you placing this stop block? It will only work if the stop block is in front of the turtle at the end of a dig(), which goes back and forth and ends up pointing the same direction it started. Um…let me try to represent this:
********ST
----------
---------T
The T represents the starting and stopping positions of the turtle after a single dig(), the -'s represent area traversed/dugDown by the turtle, the S represents the stopper block. The turtle starts off on the bottom line going left, then turns right, goes forward, right again, back over to the end of the line, then turns left, goes forward to the final position and turns left to directly face the stop block. Is this where you have your stopper block set?
lramos15 #11
Posted 21 January 2013 - 03:42 PM
Just in case I give you wrong info I uploaded a picture http://imgur.com/siK8fAv here you go i really dont know why this wont work
ChunLing #12
Posted 21 January 2013 - 10:00 PM
Look at my little diagram again, and look at your setup. You've got something like:
********S*
---------*
---------*
---------*
---------*
T--------*
********S*
What you need would look more like:

**********
---------*
---------*
---------*
---------S*
T---------*
***********
The T represents the starting position of the turtle, the *'s represent those stone blocks, the -'s represent the area where the turtle will be moving about and digging, and the S's represent the stopper blocks.

The turtle must end up pointing in the same direction it started in order for it to see the stopper block. That's how you've got dig() written.
lramos15 #13
Posted 22 January 2013 - 03:28 AM
I try to do what you say but the turtle just turns because turtle.forward() is false please help
ChunLing #14
Posted 22 January 2013 - 08:08 AM
Look carefully at that bit at the bottom right corner, under the stopping block. I left that open, so that the turtle could go forward into it, then turn left and be facing the stop block when the time came for the compare.

Of course, this arrangement is probably not ideal for your needs. What you probably want to do is have the turtle in a slightly different starting location/facing and thus to be able to put the stop block in a less awkward place. Or to rewrite check() so that the turtle always checks for the stop block when it runs into something.
Engineer #15
Posted 22 January 2013 - 11:49 AM
You have to select slot 16


function compare()
   turtle.select(16)
   If turtle.compare() then
        --stop code
   else
      turtle.select(1)
   end
end