67 posts
Location
Brazil
Posted 12 December 2013 - 08:25 PM
Heyo, I'm trying to make a automatic sugar cane farm, and, at one point, for the turtle to know that it reached the end of the farm's row, it compares the block in front of it to the slot 16 of it's internal inventory, which contains a stone brick. The image should make it more clear to you guys.
Concept of the farm:
http://imgur.com/F6gH8PjAnd the code:
pastebin.com/yByReHVDThe code is still in it's dev stages, so don't worry about the rest of the code, the main problem is, the turtle doesn't seems to really get a positive result (the boolean doesn't return true) from the "turtle.compareTo(16)", so it doesn't go to the next row, it just mines up the stone brick in front of it and continues on, forever.
Any ideas why that is happening?
7508 posts
Location
Australia
Posted 12 December 2013 - 08:52 PM
turtle.compareTo is designed to compare the currently selected slot with another slot in the turtle
turtle.compare/
turtle.compareUp/
turtle.compareDown is designed to compare the currently selected slot with the block in front/above/below the turtle.
Edited on 12 December 2013 - 08:00 PM
67 posts
Location
Brazil
Posted 12 December 2013 - 08:55 PM
Got it, thanks.
7508 posts
Location
Australia
Posted 12 December 2013 - 09:00 PM
Got it, thanks.
Also after taking a look at your code, I noticed that line 6 you've got a flaw in your logic. true (a boolean) will never be equal to "true" (a string)
Also there is no such thing as
turtle.right or
turtle.left, it is
turtle.turnLeft and
turtle.turnRightI feel as though this may be of massive use to you:
http://computercraft...iki/Turtle_(API)
Take a look at some improved logic for your program
local right = true
--# we need to select slot 16 for comparison later
turtle.select(16)
local function turn()
--# this is in charge of turning the turtle whichever way is needed
if right then
turtle.turnRight()
else
turtle.turnLeft()
end
end
--# there was no need to have the loop in a function
while true do
if turtle.compare() then --# notice there is no need for == true, no point comparing a boolean against a boolean
turn() --# call the function we defined above
turtle.forward()
turtle.forward()
turn()
right = not right --# this flips the boolean, not true is false, and not false is true
else --# we do not need to check again, if it comes to this else, it clearly doesn't match
turtle.dig()
turtle.forward()
end
end
Edited on 12 December 2013 - 08:02 PM
67 posts
Location
Brazil
Posted 12 December 2013 - 10:06 PM
Thanks a lot, theoriginalbit! It really helped!