Turtle API

From ComputerCraft
Jump to: navigation, search
FunctionFunctionturtle.attack
Attacks the entity in direction and range
Syntax turtle.attack()
Returns bool didAttack
Part of CC:Tweaked (source)
API BIOS globals
ExampleExampleCheck for hit
See if it was able to hit the entity it targeted
Code
local didHit = turtle.attack()
print(didHit)
Output
false
1

Turtle.attackDown Turtle.attackUp

FunctionFunctionturtle.back
When called, tries to move the turtle backwards 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. Note Note: This function call consumes fuel, if it is enabled in the mod configuration. It will not work if the turtle has no fuel.
Syntax turtle.back()
Returns boolean success [, string error ]
Part of CC:Tweaked (source)
API turtle
ExampleExampleMoving a turtle
Tries to move the turtle 3 blocks backward, and tells the player if this succeeded.
Code
-- 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.back()
  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 backward by", blocksToMove, "blocks!")
else
  print("Did not move the target of", blocksToMove, ", but moved", blocksSucceeded, "blocks instead!")
end

Turtle.compare Turtle.compareDown Turtle.compareTo Turtle.compareUp Turtle.craft

FunctionFunctionturtle.detect
When called, will return true if the block in front of the turtle is anything that is not air or liquid.
Syntax turtle.detect()
Returns boolean result
Part of CC:Tweaked (source)
API turtle
ExampleExampleDetecting blocks
Detect if there is a block in front of the turtle.
Code
local result = turtle.detect()
if result then
  print("There is a block in front of the turtle!")
else
  print("There is no block in front of the turtle, only air or a liquid.")
end
Output
There is a block in front of the turtle!


FunctionFunctionturtle.detectDown
When called, will return true if the block below the turtle is anything that is not air or liquid.
Syntax turtle.detectDown()
Returns boolean result
Part of CC:Tweaked (source)
API turtle
ExampleExampleDetecting blocks
Detect if there is a block below the turtle.
Code
local result = turtle.detectDown()
if result then
  print("There is a block below the turtle!")
else
  print("There is no block below the turtle, only air or a liquid.")
end
Output
There is a block below the turtle!


FunctionFunctionturtle.detectUp
When called, will return true if the block above the turtle is anything that is not air or liquid.
Syntax turtle.detectUp()
Returns boolean result
Part of CC:Tweaked (source)
API turtle
ExampleExampleDetecting blocks
Detect if there is a block above the turtle.
Code
local result = turtle.detectUp()
if result then
  print("There is a block above the turtle!")
else
  print("There is no block above the turtle, only air or a liquid.")
end
Output
There is a block above the turtle!

Turtle.dig Turtle.digDown Turtle.digUp

FunctionFunctionturtle.down
When called, tries to move the turtle down 1 block. The function will return false if the destination is obstructed or protected, or if the turtle is out of fuel. Note Note: This function call consumes fuel, if it is enabled in the mod configuration. It will not work if the turtle has no fuel.
Syntax turtle.down()
Returns boolean success [, string error ]
Part of CC:Tweaked (source)
API turtle
ExampleExampleMoving a turtle
Tries to move the turtle 3 blocks down, and tells the player if this succeeded.
Code
-- 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.down()
  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 down by", blocksToMove, "blocks!")
else
  print("Did not move the target of", blocksToMove, ", but moved", blocksSucceeded, "blocks instead!")
end

Turtle.drop Turtle.dropDown Turtle.dropUp Turtle.equipLeft Turtle.equipRight

FunctionFunctionturtle.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. Note Note: This function call consumes fuel, if it is enabled in the mod configuration. It will not work if the turtle has no fuel.
Syntax turtle.forward()
Returns boolean success [, string error ]
Part of CC:Tweaked (source)
API turtle
ExampleExampleMoving a turtle
Tries to move the turtle 3 blocks forward, and tells the player if this succeeded.
Code
-- 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


FunctionFunctionturtle.getFuelLevel
Returns a number representing the current amount of fuel (block moves) the turtle currently holds.
Syntax turtle.getFuelLevel()
Returns number fuel
Part of CC:Tweaked
API turtle
ExampleExampleGet fuel level
Get the amount of blocks a turtle can still move.
Code
print("We can still move", turtle.getFuelLevel(), "blocks before we run out of fuel!")
Output
We can still move 9802 blocks before we run out of fuel!
ExampleExampleGet fuel level in percentage
Get the fuel level of a turtle in percentage of the max fuel holding capability, using turtle.getFuelLimit().
Code
print("Fuel level is: ", ( turtle.getFuelLevel() / turtle.getFuelLimit() ) * 100, "%")
Output
Fuel level is: 9.802 %


FunctionFunctionturtle.getFuelLimit
Returns a number representing the maximum amount of fuel (block moves) the turtle can possibly hold.
Syntax turtle.getFuelLimit()
Returns number fuel
Part of CC:Tweaked
API turtle
ExampleExampleGet fuel limit
Get the maximum amount of fuel the current turtle can have
Code
print("I can hold ", turtle.getFuelLimit(), "fuel!")
Output
I can hold 100000 fuel!

Turtle.getItemCount Turtle.getItemDetail Turtle.getItemSpace Turtle.getSelectedSlot Turtle.inspect Turtle.inspectDown Turtle.inspectUp Turtle.place Turtle.placeDown Turtle.placeUp Turtle.refuel Turtle.select Turtle.suck Turtle.suckDown Turtle.suckUp Turtle.transferTo

FunctionFunctionturtle.turnLeft
When called, tries to rotate the turtle 90 degrees to the left. Note that performing this action does not require fuel, and the turtle can not be obstructed or denied from rotating.
Syntax turtle.turnLeft()
Returns boolean success
Part of CC:Tweaked (source)
API turtle
ExampleExampleRotating a turtle
Rotates a turtle 360 degrees counter-clockwise, which equals to 4 steps of 90 degrees to the left, to end facing in the same direction as it started.
Code
for step = 1, 4 do
  turtle.turnLeft()
end


FunctionFunctionturtle.turnRight
When called, tries to rotate the turtle 90 degrees to the right. Note that performing this action does not require fuel, and the turtle can not be obstructed or denied from rotating.
Syntax turtle.turnRight()
Returns boolean success
Part of CC:Tweaked (source)
API turtle
ExampleExampleRotating a turtle
Rotates a turtle 360 degrees clockwise, which equals to 4 steps of 90 degrees to the right, to end facing in the same direction as it started.
Code
for step = 1, 4 do
  turtle.turnRight()
end


FunctionFunctionturtle.up
When called, tries to move the turtle up 1 block. The function will return false if the destination is obstructed or protected, or if the turtle is out of fuel. Note Note: This function call consumes fuel, if it is enabled in the mod configuration. It will not work if the turtle has no fuel.
Syntax turtle.up()
Returns boolean success [, string error ]
Part of CC:Tweaked (source)
API turtle
ExampleExampleMoving a turtle
Tries to move the turtle 3 blocks up, and tells the player if this succeeded.
Code
-- 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.up()
  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 up by", blocksToMove, "blocks!")
else
  print("Did not move the target of", blocksToMove, ", but moved", blocksSucceeded, "blocks instead!")
end