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

[Question]How do I get a turtle to check if current slot is empty?

Started by Taji34, 18 September 2012 - 10:23 PM
Taji34 #1
Posted 19 September 2012 - 12:23 AM
Okay, so I'm writing a program to build a house, however I'm running into the problem of running out of the material it is building. I want to program it to check if the current slot is empty before it continues placing blocks, and if it is to switch to the next slot. Here's the code I have to try to check the contents of the slot(Probably horribly wrong):

turtle.getItemCount(1)
if ItemCount==0 then
  turtle.select(2)
else
  write("slot still filled")
end
This doesn't work however, when nothing is in slot 1 it doesn't switch slots, also is there a way to say getItemCount of the currently selecte slot? Thanks for the help. If you want to see the program I am trying to insert it into, I can provide it.
Cranium #2
Posted 19 September 2012 - 12:41 AM
You never defined Itemcount. You should do do ItemCount = turtle.getItemCount(1)
Taji34 #3
Posted 19 September 2012 - 12:56 AM
You never defined Itemcount. You should do do ItemCount = turtle.getItemCount(1)
. . . I should have caught that, I'll try it, thanks. Do you know of a way to tell it to just check the selected slot rather than a specificly defined slot?
Cranium #4
Posted 19 September 2012 - 01:00 AM
No, with the Turtle API, you have to specify which slot. Take a look at the Turtle API wiki here.
Taji34 #5
Posted 19 September 2012 - 02:13 AM
No, with the Turtle API, you have to specify which slot. Take a look at the Turtle API wiki here.
Okay, I was just wondering.
zoldtri #6
Posted 21 February 2013 - 02:20 AM
you could do it so

z = 1 – slot number

turtle.getItemCount(z)
if turtle.getItemCount(z) == 0 then
z = z + 1
turtle.selcect(z) – z = 2 now so it will change it to the second slot

else
write("slot has blocks in")
end
remiX #7
Posted 21 February 2013 - 02:43 AM

for i = 1, 16 do
    if turtle.getItemCount(i) == 0 then
        print( "Slot " .. i .. " is empty")
    else
        print( "Slot " .. i .. " has items")
    end
end
LordIkol #8
Posted 21 February 2013 - 03:22 AM
I would do something like this (not tested)


currentslot = 1

local function selectit(s)
turtle.select(s)
currentslot = s
end

local function checkslot(c)
if turtle.getItemCount(c) == 0 then
selectit(currentslot +1)
else
print( "Current slot still has items")
end
end

Instead of turtle.select() u use selectit in your program so you always know which slot is currently selected
to test this just use checkslot(currentslot)

Greets Loki