Difference between revisions of "Turtle.forward"
From ComputerCraft Wiki
("Created" the page again) |
(Adding "see also".) |
||
| (16 intermediate revisions by 10 users not shown) | |||
| Line 2: | Line 2: | ||
|name=turtle.forward | |name=turtle.forward | ||
|api=turtle | |api=turtle | ||
| + | |returns=[[boolean_(type)|boolean]] whether the turtle succeeded in moving forward | ||
|addon=ComputerCraft | |addon=ComputerCraft | ||
|desc=Attempts to move the turtle forward. | |desc=Attempts to move the turtle forward. | ||
|examples= | |examples= | ||
{{Example | {{Example | ||
| − | |desc= | + | |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 15: | Line 16: | ||
}} | }} | ||
{{Example | {{Example | ||
| − | |desc= | + | |desc=Tries to move the turtle forward a number of times. The times to move forward is specified by the user. |
| − | |code= | + | |code=local distance = ... -- gets the first argument from the user |
| − | if | + | if distance ~= nil then |
| − | + | distance = [[tonumber]] (distance) -- make it a number | |
| − | + | for i = 1, distance do | |
| − | + | ||
| − | + | ||
| − | + | ||
turtle.forward() | turtle.forward() | ||
| − | |||
| − | |||
end | end | ||
else | else | ||
| − | [[print]] (" | + | [[print]] ("Did not specify distance.") |
end | end | ||
| − | |output= Either the | + | |output= Either the turtle moving the specified amount of times or missing parameter. |
| + | }} | ||
| + | {{Example | ||
| + | |desc=Digs until the turtle can move forward. | ||
| + | |code=while not turtle.forward() do | ||
| + | [[turtle.dig]]() | ||
| + | end | ||
| + | |output= The turtle moves forward. | ||
}} | }} | ||
}} | }} | ||
| + | |||
| + | ==See also== | ||
| + | *[[turtle.back]] | ||
| + | |||
| + | [[Category:Lua_Core_Functions]] | ||
Latest revision as of 23:09, 26 April 2015
| Attempts to move the turtle forward. | |
| Syntax | turtle.forward() |
| Returns | boolean whether the turtle succeeded in moving forward |
| Part of | ComputerCraft |
| API | turtle |
Examples
| 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. |
| 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. |
| Digs until the turtle can move forward. | |
| Code |
while not turtle.forward() do turtle.dig() end |
| Output | The turtle moves forward. |