turtle.forward

When called, tries to move the turtle forward 1 block in the direction it is facing. The function will return false if the destination is obstructed or protected, or if the turtle is out of fuel.

turtle.forward
Function
Syntax
turtle.forward()
Returns boolean success [, string error ]
API turtle
Source CC:Tweaked (source)

 Note: This function call consumes fuel, if it is enabled in the mod configuration. It will not work if the turtle has no fuel.

ExampleMoving a turtle
Tries to move the turtle 3 blocks forward, and tells the player if this succeeded.
Code
<nowiki>
-- first we set these variables
local blocksSucceeded = 0
local blocksToMove = 3

-- then we initiate a 'for loop'. The for loop will execute the code in it's body multiple times, from 1 to 'blocksToMove' in this case, which we set to 3.
for attempt = 1, blocksToMove do
  local success, error = turtle.forward()
  if not success then -- what to do if the move failed
    break -- break will stop the 'for loop' from continuing
  else -- what to do if the move succeeded
    blocksSucceeded = blocksSucceeded + 1
  end
end

-- now we're going to print the results to screen.
if blocksToMove == blocksSucceeded then
  print("Sucessfully moved forward by", blocksToMove, "blocks!")
else
  print("Did not move the target of", blocksToMove, ", but moved", blocksSucceeded, "blocks instead!")
end
    </nowiki>
This article is issued from Computercraft. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.