This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Bluethousandand2's profile picture

Shortening a piece of code.

Started by Bluethousandand2, 30 November 2013 - 11:43 AM
Bluethousandand2 #1
Posted 30 November 2013 - 12:43 PM
I'm a beginner at turtle coding (Programming?) and was wondering if there was a way to shorten this piece, for future purposes. It does work, but using it in several programs and typing it each time is insanely tedious. It's meant to check if the item count in slot 1 is empty. If not, it doesn't do anything. If so, it moves items in all slots to slot one.

Spoiler

function fill()
if turtle.getItemCount(1) == 0 then
turtle.select(2)
turtle.transferTo(1)
turtle.select(3)
turtle.transferTo(1)
turtle.select(4)
turtle.transferTo(1)
turtle.select(5)
turtle.transferTo(1)
turtle.select(6)
turtle.transferTo(1)
turtle.select(7)
turtle.transferTo(1)
turtle.select(8)
turtle.transferTo(1)
turtle.select(9)
turtle.transferTo(1)
turtle.select(10)
turtle.transferTo(1)
turtle.select(11)
turtle.transferTo(1)
turtle.select(12)
turtle.transferTo(1)
turtle.select(13)
turtle.transferTo(1)
turtle.select(14)
turtle.transferTo(1)
turtle.select(15)
turtle.transferTo(1)
turtle.select(16)
turtle.transferTo(1)
turtle.select(1)
end
end

Also, for context purposes, I'm using it in this program. It creates a bridge until it detects a solid piece of land underneath it.

Spoiler

local dis = 0
local pln = 0

function fill()
if turtle.getItemCount(1) == 0 then
turtle.select(2)
turtle.transferTo(1)
turtle.select(3)
turtle.transferTo(1)
turtle.select(4)
turtle.transferTo(1)
turtle.select(5)
turtle.transferTo(1)
turtle.select(6)
turtle.transferTo(1)
turtle.select(7)
turtle.transferTo(1)
turtle.select(8)
turtle.transferTo(1)
turtle.select(9)
turtle.transferTo(1)
turtle.select(10)
turtle.transferTo(1)
turtle.select(11)
turtle.transferTo(1)
turtle.select(12)
turtle.transferTo(1)
turtle.select(13)
turtle.transferTo(1)
turtle.select(14)
turtle.transferTo(1)
turtle.select(15)
turtle.transferTo(1)
turtle.select(16)
turtle.transferTo(1)
turtle.select(1)
end
end

turtle.forward()
while turtle.detectDown() == false do
turtle.down()
turtle.turnLeft()
turtle.place()
fill()
pln = pln + 1
turtle.turnRight()
turtle.turnRight()
turtle.place()
pln = pln + 1
fill()
turtle.up()
turtle.placeDown()
pln = pln + 1
fill()
turtle.turnLeft()
turtle.forward()
dis = dis + 1
print(pln.. "...")
end

print("Bridged ".. dis .." blocks, using "..pln.."blocks.")
Strite #2
Posted 30 November 2013 - 01:01 PM
Try something like this.


local x = 2

function fill()
repeat 
if turtle.getItemCount(1) == 0 then
turtle.select(x)
turtle.transferTo(1)
 x = x +1
until x > 16
end

See if that works, I didn't really tested it.
Kingdaro #3
Posted 30 November 2013 - 01:02 PM
Use a for loop.


function fill()
   if turtle.getItemCount(1) == 0 then
      for slot=2, 16, 1 do
         turtle.select(slot)
         turtle.transferTo(1)
      end
      turtle.select(1)
   end
end

The for loop declares a variable, "slot". It repeats the loop, starting the variable at 2, ending at 16, and going up by 1 each time.
Bluethousandand2 #4
Posted 30 November 2013 - 01:19 PM
Thanks for the help, both of you. I got it working without using that atrocity of a code.