Difference between revisions of "Turtle.forward"

From ComputerCraft Wiki
Jump to: navigation, search
m (Corrected mapping from "Boolean" to "Boolean (type)")
(That code example hurt my eyes.... its now fixed, also made sure better coding habits promoted as well as removing code that was taking attention away from the function at hand)
Line 16: Line 16:
 
}}
 
}}
 
{{Example
 
{{Example
|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.
+
|desc=Tries to move the turtle forward a number of times. The times to move forward is specified by the user.
|code=Arg = (...) -- gets the parameters from the user
+
|code=local distance = ... -- gets the first argument from the user
  if Arg ~= nil then
+
  if distance ~= nil then
   Arg = [[tonumber]] (Arg) -- make it a number
+
   distance = [[tonumber]] (distance) -- make it a number
   a = 1
+
   for i = 1, distance do
  while a <= Arg do
+
  if not turtle.forward() then
+
    [[turtle.dig]]()
+
 
     turtle.forward()
 
     turtle.forward()
  end
 
  a = a + 1
 
 
   end
 
   end
 
  else
 
  else
   [[print]] ("Missing parameter.")
+
   [[print]] ("Did not specify distance.")
 
  end
 
  end
 
|output= Either the turtle 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 06:38, 4 November 2013

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 a number of times. The times to move forward is specified by the user.
Code
local distance = ... -- gets the first argument from the user
if distance ~= nil then
 distance = tonumber (distance) -- make it a number
 for i = 1, distance do
   turtle.forward()
 end
else
 print ("Did not specify distance.")
end
Output Either the turtle moving the specified amount of times or missing parameter.



Grid paper.png  Example
Digs until the turtle can move forward.
Code
while not turtle.forward() do
 turtle.dig()
end
Output The turtle moves forward.