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

Changing turtle slot when out of blocks.

Started by teg0r, 11 November 2014 - 09:49 AM
teg0r #1
Posted 11 November 2014 - 10:49 AM
Hello! Im trying to make program that first builds tower, then later when wanted will add layers of alvearys+pipes. My problem is when im building tower, i run out of blocks and i want to change inventory slot and when empty i want it to take more blocks from ender chest.
Tower im making:

so far my code looks like this
function place()
turtle.placeDown()
end

function place2()
turtle.placeDown()
turtle.forward()
turtle.placeDown()
end

function place3()
for i=1,2 do
turtle.placeDown()
turtle.forward()
end
turtle.placeDown()
end

function place4()
for i=1,3 do
turtle.placeDown()
turtle.forward()
end
turtle.placeDown()
end

function place5()
for i=1,4 do
turtle.placeDown()
turtle.forward()
end
turtle.placeDown()
end


function left()
turtle.forward()
turtle.turnLeft()
turtle.forward()
turtle.turnRight()
end

function right()
turtle.forward()
turtle.turnRight()
turtle.forward()
turtle.turnLeft()
end

i = 1
while turtle.getItemCount(i) <1 do
  i = i + 1
  turtle.select(i)
end

local x = 0
term.write("How high tower?")
x = read()

turtle.up()
for i=1, x do
for i=1, 4 do
place5()
left()
place3()
left()
place2()
left()
place()
left()
place2()
left()
place()
turtle.forward()
turtle.turnLeft()
turtle.forward()
place2()
right()
place()
right()
place2()
right()
place3()
right()
place4()
turtle.forward()
end
turtle.up()
end
turtle.turnLeft()
turtle.forward()
for i=1,x do
turtle.Down()
end

So im using
i = 1
while turtle.getItemCount(i) <1 do
  i = i + 1
  turtle.select(i)
end 

to change slot, but i dont know where i should put that code, as of now it only checks slot before it starts building. Do i have to add that to every function or is there simple way to do it?

this is what iv been thinking for taking items from enderchest in slot 16

while turtle.getItemCount(i) <1 do
  i = i + 1
turtle.select(i)
if i=16 do
turtle.placeUp()
for i=1,15 do
turtle.suckUp()
turtle.digUp()
end
end

So long story short: Where do i place code that checks slots and selects new slot if no blocks on current slot?
Bomb Bloke #2
Posted 11 November 2014 - 12:24 PM
I'd stick the slot-checking code into its own function, then call that function from within any function that wants to place blocks.

Eg (assuming the enderchest is in slot 16):

local function checkSlots()
  local curSlot = 1
  while turtle.getItemCount(curSlot) == 0 do curSlot = curSlot + 1 end

  turtle.select(curSlot)

  if curSlot == 16 then
    turtle.placeUp()
    turtle.select(1)
    for i=1,15 do turtle.suckUp() end
    turtle.select(16)
    turtle.digUp()
    turtle.select(1)
  end
end

local function place()
  checkSlots()
  turtle.placeDown()
end

etc
teg0r #3
Posted 11 November 2014 - 01:02 PM
Thanks! Helps me out alot :D/> Have a good day sir.