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

Small issue about turtle API returns

Started by Hexus_One, 15 October 2012 - 12:06 PM
Hexus_One #1
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?
GopherAtl #2
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.
remiX #3
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
Cloudy #4
Posted 15 October 2012 - 08:07 PM
To handle gravel my code is usually:

while not turtle.forward() do turtle.dig() end
Ditto8353 #5
Posted 15 October 2012 - 08:09 PM
Would this work as well?

while turtle.detect() do turtle.dig() end
Doyle3694 #6
Posted 15 October 2012 - 08:11 PM
yeah but won't move forward :D/>/>
Ditto8353 #7
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.
GopherAtl #8
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.
Doyle3694 #9
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
Hexus_One #10
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.
Fatal_Exception #11
Posted 16 October 2012 - 03:22 AM
It means it returns a bool that indicates the success of the operation.
Hexus_One #12
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?
Doyle3694 #13
Posted 16 October 2012 - 07:39 AM
5.1 I think