Difference between revisions of "Turtle Tutorial"

From ComputerCraft Wiki
Jump to: navigation, search
m (The Program)
 
(25 intermediate revisions by 12 users not shown)
Line 1: Line 1:
 
[[Category:Tutorials]]
 
[[Category:Tutorials]]
 
This tutorial will teach you the functions of the Turtle API, and also will teach you how to make your first turtle program.
 
This tutorial will teach you the functions of the Turtle API, and also will teach you how to make your first turtle program.
by: rockymc
 
  
 
== Turtle ==
 
== Turtle ==
  
The Turtle API is used to work with your Turtles. Here is a list of the complete Turtle API.
+
The Turtle API is used to work with your Turtles.  
{| border="1" cellpadding="2" cellspacing="0"
+
[[Turtle (API)|Here is a list of the complete Turtle API]]
!style="background:#EEE" width="200px"|Method name
+
!style="background:#EEE" width="*"|Description
+
|-
+
|turtle.forward()
+
|Let the Turtle move forward
+
|-
+
|turtle.back()
+
|Let the Turtle move back
+
|-
+
|turtle.up()
+
|Let the Turtle move up
+
|-
+
|turtle.down()
+
|Let the Turtle move down
+
|-
+
|turtle.turnLeft()
+
|The Turtle turns left
+
|-
+
|turtle.turnRight()
+
|The Turtle turns right
+
|-
+
|turtle.select( slotNum )
+
|The Turtle selects the given Slot (1 is top left, 9 is bottom right)
+
|-
+
|turtle.getItemCount( slotNum )
+
|Counts how many items are in the given Slot
+
|-
+
|turtle.getItemSpace( slotNum )
+
|Counts how many items you need to fill the stack in the given Slot
+
|-
+
|turtle.dig()
+
|Breaks the Block in front
+
|-
+
|turtle.digUp()
+
|Breaks the Block above
+
|-
+
|turtle.digDown()
+
|Breaks the Block below
+
|-
+
|turtle.place()
+
|Places a Block of the selected Slot in front
+
|-
+
|turtle.placeUp()
+
|Places a Block of the selected Slot above
+
|-
+
|turtle.placeDown()
+
|Places a Block of the selected Slot below
+
|-
+
|turtle.detect()
+
|Detects if there is a Block in front
+
|-
+
|turtle.detectUp()
+
|Detects if there is a Block above
+
|-
+
|turtle.detectDown()
+
|Detects if there is a Block below
+
|-
+
|turtle.drop()
+
|Drops everything of the selected Slot
+
|}
+
  
For our program, we will use turtle.detectDown(), turtle.digDown(), turtle.down() and turtle.forward(). It is recommended that you learn what method does what before messing up with Turtles.
+
For our program, we will use turtle.refuel(),turtle.detectDown(), turtle.digDown(), turtle.down(), turtle.forward(), and turtle.placeDown(). It is recommended that you learn what these method do before messing with Turtles.
  
 
== The Program ==
 
== The Program ==
 
So, for you to make the Turtle program, first craft a Turtle.
 
So, for you to make the Turtle program, first craft a Turtle.
  
[[File:Turtle-craft.png]]
+
{{Crafting
 +
|A1=iron ingot |B1=iron ingot |C1=iron ingot
 +
|A2=iron ingot |B2=Computer  |C2=iron ingot
 +
|A3=iron ingot |B3=chest      |C3=iron ingot
 +
|Output=turtle
 +
}}
  
 
Then make a Mining Turtle.
 
Then make a Mining Turtle.
  
[[File:Turtle-craft2.png]]
+
{{Crafting
 +
|A2=turtle |B2=diamond pickaxe
 +
|Output=mining turtle
 +
|Output-link=Turtle
 +
}}
  
  
Then, place it anywhere you want. Right click it and type 'edit MyFirstTurtleProgram', you don't have to call it 'MyFirstTurtleProgram' it can be whatever you want.
+
Then, place it anywhere you want. Right click it and type 'edit MyFirstTurtleProgram'. You don't have to call it 'MyFirstTurtleProgram'; It can be whatever you want.
So, inside the file, type:
+
So, inside the file, type
 +
turtle.refuel()
 +
print("Refueled turtle!")
 
  while turtle.detectDown() do
 
  while turtle.detectDown() do
print("Digging down!")
+
    print("Digging down!")
turtle.digDown()
+
    turtle.digDown()
print("Going down!")
+
    print("Going down!")
turtle.down()
+
    turtle.down()
print("Digging forward!")
+
    print("Digging forward!")
turtle.dig()
+
    turtle.dig()
print("Going forward!")
+
    print("Going forward!")
turtle.forward()
+
    turtle.forward()
 
  end
 
  end
  
 
To save the program, press CTRL and select SAVE with the Arrow Keys. After that, type MyFirstTurtleProgram (or whatever you called your program).
 
To save the program, press CTRL and select SAVE with the Arrow Keys. After that, type MyFirstTurtleProgram (or whatever you called your program).
 +
 +
If you want a spiral drill formation that allows you to be able to retrieve the turtle after its done mining, do the same as above but when editing the program type:
 +
turtle.refuel()
 +
while turtle.detectDown() do
 +
    turtle.dig()
 +
    turtle.digDown()
 +
    turtle.down()
 +
    turtle.dig()
 +
    turtle.forward()
 +
    turtle.turnLeft()
 +
end
  
 
== Explanation ==
 
== Explanation ==
 +
 +
The line turtle.refuel() refuels the Turtle so that it can move.
  
 
The line while turtle.detectDown() do makes the program run until there is no block underneath the Turtle.
 
The line while turtle.detectDown() do makes the program run until there is no block underneath the Turtle.
Line 107: Line 70:
  
 
NOTE: this should be running on a mining turtle, or it wont work!
 
NOTE: this should be running on a mining turtle, or it wont work!
 
== Author ==
 
[[User:Rockymc|rockymc]]
 

Latest revision as of 06:19, 4 August 2020

This tutorial will teach you the functions of the Turtle API, and also will teach you how to make your first turtle program.

Turtle

The Turtle API is used to work with your Turtles. Here is a list of the complete Turtle API

For our program, we will use turtle.refuel(),turtle.detectDown(), turtle.digDown(), turtle.down(), turtle.forward(), and turtle.placeDown(). It is recommended that you learn what these method do before messing with Turtles.

The Program

So, for you to make the Turtle program, first craft a Turtle.

Empty-crafting-table.png
Grid iron ingot.png
Grid iron ingot.png
Grid iron ingot.png
Grid iron ingot.png
Grid Computer.png
Grid iron ingot.png
Grid turtle.png
Grid iron ingot.png
Grid chest.png
Grid iron ingot.png

Then make a Mining Turtle.

Empty-crafting-table.png
Grid turtle.png
Grid diamond pickaxe.png
Grid mining turtle.png


Then, place it anywhere you want. Right click it and type 'edit MyFirstTurtleProgram'. You don't have to call it 'MyFirstTurtleProgram'; It can be whatever you want. So, inside the file, type

turtle.refuel()
print("Refueled turtle!")
while turtle.detectDown() do
    print("Digging down!")
    turtle.digDown()
    print("Going down!")
    turtle.down()
    print("Digging forward!")
    turtle.dig()
    print("Going forward!")
    turtle.forward()
end

To save the program, press CTRL and select SAVE with the Arrow Keys. After that, type MyFirstTurtleProgram (or whatever you called your program).

If you want a spiral drill formation that allows you to be able to retrieve the turtle after its done mining, do the same as above but when editing the program type:

turtle.refuel()
while turtle.detectDown() do
    turtle.dig()
    turtle.digDown()
    turtle.down()
    turtle.dig()
    turtle.forward()
    turtle.turnLeft()
end

Explanation

The line turtle.refuel() refuels the Turtle so that it can move.

The line while turtle.detectDown() do makes the program run until there is no block underneath the Turtle.

The turtle.digDown() will make the Turtle dig down.

The part where it says turtle.forward() will make the Turtle go forward.

The line that says turtle.dig() will make the Turtle dig forward.


NOTE: this should be running on a mining turtle, or it wont work!