luacomputercraft

(Lua) a while statements stops when an if statement inside stops


I am creating an automated farm in minecraft with computercraft turtles, and I ran into a snag. I have a place where the turtles drop the wheat they collected into a hopper, but as soon as CropData.name == "minecraft:wheat" is false inside the if statement, the whole while loop stops without the value controlling the while loop being false. When the turtle runs into an inventory slot that doesn't have wheat in it, it just stops, when I want it to keep checking all the other slots. Is there a way to fix this?

Here is the snippet of code i am having trouble with:

while i < 16 do
    turtle.select(i)
    CropData = turtle.getItemDetail()
    if CropData.name == "minecraft:wheat" then
        turtle.dropDown()
    end
    i = i + 1
end

Here is the full code of the program:

local x = 1
local i = 1
local itemSlot = 1
local y = 1
local success, data = turtle.inspectDown()
local CropData = turtle.getItemDetail()
while true do
y = 1
while y < 9 do
    x = 1
    while x < 14 do
        turtle.forward()
        success, data = turtle.inspectDown()
        print(data.metadata)
        if data.metadata == 7 then
            turtle.digDown()
            turtle.select(12)
            turtle.suckDown()
            turtle.select(itemSlot)
            turtle.placeDown()
        elseif data.metadata == nil then
            turtle.down()
            success, data = turtle.inspectDown()
            if data.name == "minecraft:dirt" then
            turtle.up()
            turtle.digDown()
            turtle.placeDown()
            elseif data.name == "minecraft:farmland" then
            turtle.up()
            turtle.placeDown()
            end
        end
        if turtle.getItemCount()==0 then
            itemSlot = itemSlot + 1
        end
        turtle.select(itemSlot)
        x = x + 1
    end
    turtle.turnLeft()
    turtle.forward()
    turtle.turnRight()
    turtle.back()
    turtle.back()
    turtle.back()
    turtle.back()
    turtle.back()
    turtle.back()
    turtle.back()
    turtle.back()
    turtle.back()
    turtle.back()
    turtle.back()
    turtle.back()
    turtle.back()
    y = y + 1
end
while i < 16 do
    turtle.select(i)
    CropData = turtle.getItemDetail()
    if CropData.name == "minecraft:wheat" then
        turtle.dropDown()
    end
    i = i + 1
end
turtle.turnRight()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.turnLeft()
end

P.S. Sorry if some of my code is messy and inefficient, I wrote most of this on a small screen in a small amount of time, so I was lazy. Sorry again!


Solution

  • The (or one of the) problem(s) is that turtle.getItemDetail() returns nil when the ith slot is empty. You can't access a field of a nil value, so the script crashes if you ever come across an empty slot.

    Try changing it to

      if CropData ~= nil and CropData.name == "minecraft:wheat" then
        turtle.dropDown()
      end
    

    instead.

    Also: The condition in your while loop should be <=, not <, as slots go up to and including 16.