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

Select next slot every other loop / Switch to next slot when slot is empty?

Started by Klausar, 30 September 2012 - 09:04 AM
Klausar #1
Posted 30 September 2012 - 11:04 AM
I use a repeat until slope for my turtle and want it to select the next inventory slot after 2 loops. It uses 32 Blocks per loop and after 2 the slot is empty, so it should select the second and so on. How to do it?
robhol #2
Posted 30 September 2012 - 12:56 PM
If you have an incrementing variable (eg. for i=n,m), you can do "if i%2 == 0 then", which will then be true every other loop.
Klausar #3
Posted 30 September 2012 - 01:08 PM
What is a incrementing variable? Can you explain it?
Klausar #4
Posted 30 September 2012 - 01:30 PM
Okay I make an example, lets say this is the code:

local i=1
x=tonumber(io.read())
turtle.select(1)
repeat
for f=1,32 do
turtle.placeDown()
turtle.up
end
i=i+1
until i == x

Whats the code I would have to add?

Edit: It would also be better if it automatically detects when the slot is empty and switches to the next, how to do it?
Doyle3694 #5
Posted 30 September 2012 - 03:02 PM

local i=1
local slotNum
x=tonumber(io.read())
turtle.select(1)
repeat
for f=1,32 do
if f%2 == 0 then
   slotNum = slotnum + 1
   turtle.select(slotNum)
end
turtle.placeDown()
turtle.up()
end
i=i+1
until i == x
Doyle3694 #6
Posted 30 September 2012 - 03:04 PM

local i=1
local slotNum
x=tonumber(io.read())
turtle.select(1)
repeat
for f=1,32 do
while turtle.getItemCount() == 0 do
   slotNum = slotnum + 1
   turtle.select(slotNum)
end
turtle.placeDown()
turtle.up()
end
i=i+1
until i == x
for detecting empty slots.
Klausar #7
Posted 30 September 2012 - 03:05 PM
Thank you very much! But sorry for me asking so much, this is the real code from me:
It would have to check after every blockplacement, should I just add this code after every turtle.place() ?

turtle.up()
turtle.select(1)
   local i = 1
write("How high should the dice be: ")
   x = tonumber(io.read())
turtle.forward()
--walls
for f=1,x do
for a=1,8 do
turtle.forward()
turtle.placeDown()
end

turtle.turnRight()

for b=1,8 do
turtle.forward()
turtle.placeDown()
end

turtle.turnRight()

for c=1,8 do
turtle.forward()
turtle.placeDown()
end

turtle.turnRight()

for d=1,8 do
turtle.forward()
turtle.placeDown()
end

turtle.turnRight()
turtle.up()
end

--Turtle going down when finished
turtle.back()
while not turtle.detectDown() do
turtle.down()
end
Doyle3694 #8
Posted 30 September 2012 - 03:16 PM
put it before blockplace, that way you know it will always select before it tries to place down. makes it more idiot proof
Klausar #9
Posted 30 September 2012 - 03:22 PM
Okay thanks, but now it is expecting a number at this line:
(I believe it wants the slotnumber in the getItemCount() )

while turtle.getItemCount() == 0 do
What could I do ?
Doyle3694 #10
Posted 30 September 2012 - 04:02 PM
oh, just put slotNum in there.
and when you define slotNum, define it to 1, so you won't get a slot 0 doesn't exist bug.
Klausar #11
Posted 30 September 2012 - 04:05 PM
Thank you! I already tried but made a mistake. Now it works without any problems.