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

[programming help] select new slot when slot is empty in turtles inventory?

Started by mikesaa309, 22 December 2012 - 10:09 PM
mikesaa309 #1
Posted 22 December 2012 - 11:09 PM
Hi another dumb question,

how do I make a bit of code to make the turtle check the current slot for items then if its empty move on to the next one? I have used the turtle.getItemCount to check the number of items and i've managed to get it to check the items then if empty go to slot two but how would i do it so if slot 2 is empty move on to the next slot etc without righting a load of code?

I want to do this because i'm trying to get the turtle to build up a simple house but made out of one material. I know that i can copy codes from the forum but i want to try and do it myself so hopefully i learn stuff for future references etc. I've got it to do what i want at last which is to build four wall four blocks high but i tested the program using a 5 x 5 size square where as the actual building i want to make (a cinema) is 16 x 16 so it runs out of blocks after a while so i need tell it to move on to the next slot if the current slot is empty. How do i do this?
ChunLing #2
Posted 22 December 2012 - 11:20 PM
First, use a variable to track your current slot, and generally select using that variable rather than directly, so that the content of that variable is the actual slot selected. Then use that variable to check things like itemcount.
local sslot = 1
turtle.select(sslot)
sslot = (sslot%16)+1
turtle.select(sslot)
turtle.getItemCount(sslot)
Oh, and to get it to move to the next slot when empty you just do something like:
while turtle.getItemCount(sslot) < 1 do
  sslot = (sslot%16)+1
end
turtle.select(sslot)
You might want to revise that a bit since if your turtle is completely empty it will run that until you get a failed to yield.
Edited on 22 December 2012 - 10:23 PM
mikesaa309 #3
Posted 23 December 2012 - 12:56 AM
thanks for your reply would you mind explaining each line of code and what it does etc as i'm new to programming and i find things easier if explained somethings i can pick up my self like the common sense things such as the while loop is obviously going to say while a event is happening do the following. So would you mind explaining the lines of code?
ChunLing #4
Posted 23 December 2012 - 02:16 AM
Err, okay:
local sslot = 1 --First, use a variable to track your current slot,
turtle.select(sslot) --and generally select using that variable rather than directly
sslot = (sslot%16)+1  --to change slots, change the variable first,
turtle.select(sslot)  --so that the content of that variable is the actual slot selected.
turtle.getItemCount(sslot) --Then use that variable to check things like itemcount.
The modulus operator "%" is useful for incrementing a value and having it loop within a given range, in this case from 1-16 (sslot%16 returns a number from 0-15, we then add 1 so it's 1-16).
while turtle.getItemCount(sslot) < 1 do -- while there is less than one item in the sslot
  sslot = (sslot%16)+1 -- change sslot to the next slot
end
turtle.select(sslot) -- then select sslot, which has at least one item
KaoS #5
Posted 23 December 2012 - 03:58 AM
I would use a for loop…


for slot=1,16 do
  if turtle.getItemCount(slot)>0 then
    turtle.select(slot)
    break
  end
end

I know it's longer. just makes more sense to me
Doyle3694 #6
Posted 23 December 2012 - 04:33 AM
oh that use of % ChunLing, havn't thought of that one before. That is so clever!
ChunLing #7
Posted 23 December 2012 - 04:02 PM
Hey, the OP asked for explaining every line.