Help with turtle code please

  • The FTB Forum is now read-only, and is here as an archive. To participate in our community discussions, please join our Discord! https://ftb.team/discord

unholywar23

New Member
Jul 29, 2019
134
0
0
all I want is a turtle that will fill in blocks in a straight line for a certain distance. how would I right that code
 

Mikey_R

New Member
Jul 29, 2019
382
0
0
I don't know how to read a user input, but the bulk of your program will be.

do i=1, X
turtle.MoveForward()
turtle.PlaceDown()
end

Where X is the user input. You could essentially use that and replace X, but that's no fun. I'll leave the last bit for someone else (and they might correct some parts of mine, I haven't done too much except a few basic things).
 

Newflesh

New Member
Jul 29, 2019
10
0
0
I'm a noob in programming, but I think that user input would be:
read()

ex:

local x = read()

for i = 1 , x do
*code*
i = i - 1 (not sure if that is necessary)
end
 

Belone

New Member
Jul 29, 2019
417
0
0
I'm a noob in programming, but I think that user input would be:

i = i - 1 (not sure if that is necessary)

no it's not necessary, this would actually break it, as I presume you meant i = i + 1. The For loop will auto increment based on the optional third argument for the for loop, default being +1.
 

slay_mithos

New Member
Jul 29, 2019
1,288
0
0
or you could use arguments to do it instead of read:
Code:
local args = {...}
local length
 
if (args[1] == nil) then
  length = 10
else
  length = args[1]
end
 
for i=0, length do
  while (not turtle.forward()) do
    if (turtle.detect()) then
      turtle.dig()
    else
      turtle.attack()
    end
  end
  turtle.placeDown()
end

So, by default, the length will be 10, unless you provide it with a length as the first argument.
Then, it will dig or attack if it is prevented to move, depending on what is in front, and place a block down.

the "while" loop is basically used in every single one of my programs to ensure that the turtle will really move the number of blocks I want, and not just stop if a block or a zombie came in front of it, but you can remove it if you don't want the turtle to dig/attack and replace it with a "turtle.forward()".
 

DutchStealth

New Member
Jul 29, 2019
8
0
0
I'm still a LUA beginner, but this might do the trick:

Code:
term.write("How many blocks to fill: ")
blocks = read()
 
turtle.select(1)
 
for i = 1, blocks do
  turtle.placeDown()
  turtle.forward()
end

You have to place the Turtle directly above the first hole, then specify how many blocks to fill. The Turtle will grab materials from slot 1 (upper left corner).
 

slay_mithos

New Member
Jul 29, 2019
1,288
0
0
Except it is tricky to place a turtle above a hole in most situations, which is why we did the "forward" before the "placeDown", but you do have a point, we did forget to mention placing blocks in the turtle.

To be fair, if you want it to use the slot 1, you should add a "turtle.select(1)" at the start.
 

DutchStealth

New Member
Jul 29, 2019
8
0
0
True. I also assumed the hole is 1 block deep, which might be incorrect. But I will let you handle this problem, since you have more experience then me :p
 

slay_mithos

New Member
Jul 29, 2019
1,288
0
0
Just to be clear, having more experience also usually means having bad habits, and you should not take what I said as a bad critic, it was actually hints to help you progressing.

But anyway, the person having posted first didn't come back yet, so we have no idea if what we are writing will be used or not.
 

QuantumPugilist

New Member
Jul 29, 2019
52
0
0
One thing that needs to be added is having the turtle change slots, otherwise it can only place a line of 64. Something like:

Code:
-- Checks if slot is empty, and moves to next slot
i = 1
while turtle.getItemCount() <=0 do
  i = i + 1
  turtle.select(i)
end
 
turtle.placeDown()

Edit: Fixed to fit Lua

Edit 2: Edited to fix what Democretes noted. Coding before coffee, terrible plan :D.
 

Belone

New Member
Jul 29, 2019
417
0
0
One thing that needs to be added is having the turtle change slots, otherwise it can only place a line of 64. Something like:

Code:
i = 0
while turtle.place() == false do
  i = i + 1
  turtle.select(i)
end
 
turtle.placeDown()
I know I'm nitpicking, so I apologise for this, but can't you have i start at 1 since 1 is the first slot? Or am I wrong and it goes from 0 to 8?
 

QuantumPugilist

New Member
Jul 29, 2019
52
0
0
I know I'm nitpicking, so I apologise for this, but can't you have i start at 1 since 1 is the first slot? Or am I wrong and it goes from 0 to 8?

You're completely correct, I keep reverting back to standard coding habits. I think everything Lua starts at 1, rather than 0.
 

Democretes

New Member
Jul 29, 2019
1,134
0
1
One thing that needs to be added is having the turtle change slots, otherwise it can only place a line of 64. Something like:

Code:
i = 1
while turtle.place() == false do
  i = i + 1
  turtle.select(i)
end
 
turtle.placeDown()

Edit: Fixed to fit Lua
In that case, it would keep changing slots if there was a block under it. You can use turtle.getSlotCount() (I think that's what it's called) to check the count and use an if then statement to say when the slot is less than 0 to change the slot.
 
  • Like
Reactions: QuantumPugilist

lukasni

New Member
Jul 29, 2019
19
0
0
Those are lua programs that are run from a file. To do that, first create a new file using something like edit bridge, where bridge can be any name you want for the program. This will bring up an editor. You can enter the code there. There is no copy/paste, so you'll have to do it manually. After you've entered your code, hit ctrl to bring up the menu, then S to save. Bring up the menu again and hit E to exit. You can now run the program by typing its name, e.g. "> bridge" and hitting enter.

For additional information i suggest consulting the CC wiki at computercraft.info/wiki/ , it's a great source of information for everything ComputerCraft.
 

MrZwij

New Member
Jul 29, 2019
452
0
0
Easy solution, fill the turtle with material and type "tunnel 100" (or whatever distance you want.) This will go along and fill in the space under the turtle BUT it will also mine out a 2x3 along the way, and will be fairly slow.