Difference between revisions of "Turtle.forward"

From ComputerCraft Wiki
Jump to: navigation, search
m (forward chuck)
(Fixed some typoes)
Line 7: Line 7:
 
|examples=
 
|examples=
 
{{Example
 
{{Example
|desc=Trys to move the turtle forward and outputs whether it worked or not.
+
|desc=Tries to move the turtle forward and outputs whether it worked or not.
 
|code=if turtle.forward() then
 
|code=if turtle.forward() then
 
   [[print]] ("Turtle moved forward.")
 
   [[print]] ("Turtle moved forward.")
Line 16: Line 16:
 
}}
 
}}
 
{{Example
 
{{Example
|desc=Trys to move the turtle forward x number of times. X is spezified by the user and if the turtle can't move forward it will break the block infront of it.
+
|desc=Tries to move the turtle forward x number of times. X is specified by the user and if the turtle can't move forward it will break the block infront of it.
|code=Arg = (...) -- gets the param from the user
+
|code=Arg = (...) -- gets the parameters from the user
 
  if Arg ~= nil then
 
  if Arg ~= nil then
 
   Arg = [[tonumber]] (Arg) -- make it a number
 
   Arg = [[tonumber]] (Arg) -- make it a number
Line 31: Line 31:
 
   [[print]] ("Missing parameter.")
 
   [[print]] ("Missing parameter.")
 
  end
 
  end
|output= Either the trutle moving the specified amount of times. or Missing parameter.
+
|output= Either the turtle moving the specified amount of times or missing parameter.
 
}}
 
}}
 
}}
 
}}

Revision as of 02:23, 15 July 2012

Grid Redstone.png  Function turtle.forward
Attempts to move the turtle forward.
Syntax turtle.forward()
Returns boolean whether the turtle succeeded in moving forward
Part of ComputerCraft
API turtle

Examples

Grid paper.png  Example
Tries to move the turtle forward and outputs whether it worked or not.
Code
if turtle.forward() then
 print ("Turtle moved forward.")
else
 print ("Turtle didn't move forward.")
end
Output Turtle moved forward. or Turtle didn't move forward.



Grid paper.png  Example
Tries to move the turtle forward x number of times. X is specified by the user and if the turtle can't move forward it will break the block infront of it.
Code
Arg = (...) -- gets the parameters from the user
if Arg ~= nil then
 Arg = tonumber (Arg) -- make it a number
 a = 1
 while a <= Arg do
  if not turtle.forward() then
   turtle.dig()
   turtle.forward()
  end
 a = a + 1
 end
else
 print ("Missing parameter.")
end
Output Either the turtle moving the specified amount of times or missing parameter.