9 posts
Posted 30 December 2012 - 07:17 AM
I have been trying to write code for a quarrying. It was working well, until I wrote a function to check the turtle's inventory. Now, the turtle will go to the assigned space (my coordinate command is fine) then instead of mining move a block forward, turn left, and go. The quarry is in sand, so it gets stuck on the wall and I do not know where it is heading. I believe it is heading back to base. This is my code:
function ivFull()
for i = 1,16 do
turtle.getItemCount(i)
if turtle.getItemCount(i) == 0 then
return false
else
return true
end
end
end
I want it to return false until there are no slots with nothing in them. Then it should return true.
1688 posts
Location
'MURICA
Posted 30 December 2012 - 07:58 AM
And in comes turtle.getItemSpace, which returns how much is needed to fill a certain slot.
function ivFull()
for i = 1,16 do
if turtle.getItemSpace(i) > 0 then
return false
end
end
return true
end
9 posts
Posted 30 December 2012 - 08:05 AM
And in comes turtle.getItemSpace, which returns how much is needed to fill a certain slot.
That did work kind sir, but why didn't my version work? I did notice I had that extra getItemCount before the for loop. Did that affect it?
18 posts
Posted 30 December 2012 - 08:10 AM
And in comes turtle.getItemSpace, which returns how much is needed to fill a certain slot.
That did work kind sir, but why didn't my version work? I did notice I had that extra getItemCount before the for loop. Did that affect it?
Take another look at your if statement.
if turtle.getItemCount(i) == 0 then
return false
else
return true
end
That statement will always return either true or false. The loop never gets past its first iteration.
1688 posts
Location
'MURICA
Posted 30 December 2012 - 08:11 AM
That did work kind sir, but why didn't my version work? I did notice I had that extra getItemCount before the for loop. Did that affect it?
That didn't really do anything. You could just have that there and it wouldn't affect anything. The problem here was this:
else
return true
end
The code is in the first loopthrough and i is 1, so it checks the number of items in that slot, and if it's full or not. If it's not full, the code stops and tells you that there's a slot that could use more items in it. However if it is full, the code should continue to the other slots and check if those aren't full either, but here, it instantly returns true on the first loopthrough if only the first slot is full.
7 posts
Posted 30 December 2012 - 09:05 AM
Try this
function ivFull()
local full = true
for i = 1,16 do
if turtle.getItemCount(i) == 0 then
full = false
end
end
return full
end
9 posts
Posted 30 December 2012 - 05:33 PM
Thank you for the input, I see the problem now.