9 posts
Posted 15 October 2012 - 02:06 PM
So, how exactly would I check to see if a turtle API command (such as turtle.forward()) has successfully executed?
I'm currently trying to solve an issue with my mining program stuffing up when encountering gravel, and I'm trying to see if turtle.forward() fails, then the turtle should dig and try moving again.
Would it be something like
a = turtle.forward()
if a == (whatever the success message is) then
–etc.?
would «if turtle.forward() == "success"» work instead, like with most functions?
871 posts
Posted 15 October 2012 - 02:12 PM
As you could've learned on the wiki in less time than it took to post this, they return booleans - true/false. So you can just do "if turtle.forward() then…" or "if not turtle.forward() then …" for the on-failure case.
2088 posts
Location
South Africa
Posted 15 October 2012 - 02:17 PM
As GopherAtl said, it will be something like this.
while true do
if turtle.forward() then
print("Going forward!")
else
print("Not going forward!")
end
sleep(1)
end
2447 posts
Posted 15 October 2012 - 08:07 PM
To handle gravel my code is usually:
while not turtle.forward() do turtle.dig() end
136 posts
Posted 15 October 2012 - 08:09 PM
Would this work as well?
while turtle.detect() do turtle.dig() end
818 posts
Posted 15 October 2012 - 08:11 PM
yeah but won't move forward :D/>/>
136 posts
Posted 15 October 2012 - 08:15 PM
yeah but won't move forward
Which is perfect for my needs :3
I like that it keeps mob interference separate from block interference.
Also I just like it.
871 posts
Posted 15 October 2012 - 09:53 PM
actually no, the detect version would get stuck as it takes time for the gravel to fall after the dig, so detect can return false and then a subsequent forward() still fail.
818 posts
Posted 15 October 2012 - 09:55 PM
Oh yeah I meant more syntax wise, adding a sleep(0.1) after the turtle.dig would fix it
9 posts
Posted 15 October 2012 - 09:55 PM
As you could've learned on the wiki in less time than it took to post this, they return booleans - true/false. So you can just do "if turtle.forward() then…" or "if not turtle.forward() then …" for the on-failure case.
I was slightly confused because this page:
http://computercraft.info/wiki/index.php?title=Turtle_(API)Says that movement functions would return 'bool "success"'. I wasn't sure whether it would return a bool or a string. Thanks, anyway.
105 posts
Posted 16 October 2012 - 03:22 AM
It means it returns a bool that indicates the success of the operation.
9 posts
Posted 16 October 2012 - 07:35 AM
Ah. Thanks. My five years of knowledge of Lua has been improved.
Does ComputerCraft use Lua 5.1 or 5.2?
818 posts
Posted 16 October 2012 - 07:39 AM
5.1 I think