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

Can't select next slot when current is empty

Started by Bergy, 28 December 2013 - 03:43 PM
Bergy #1
Posted 28 December 2013 - 04:43 PM
Hello all programmers,
I have a little problem with my program, it's basically a wheat field farm kinda deal and the part where it change slot when the current one is empty is broken.
I know this program isn't pro at al but it's for my solo map and making clean programs isn't in my priority list.
When my little buddy runs he won't change slot when the first one is empty.


rednet.open("right")
function open()
  rednet.send(13,"open")
end
function close()
  rednet.send(13,"close")
end
function rowRight()
  for i=1,6 do
    turtle.placeDown()
    turtle.forward()
    turtle.placeDown()
  end
  turtle.turnRight()
  turtle.forward()
  turtle.turnRight()
end
function rowLeft()
  for i=1,6 do
    turtle.placeDown()
    turtle.forward()
    turtle.placeDown()
  end
  turtle.turnLeft()
  turtle.forward()
  turtle.turnLeft()
end
function rowEnd()
  for i=1,6 do
    turtle.placeDown()
    turtle.forward()
    turtle.placeDown()
  end
  turtle.turnLeft()
  for i=1,4 do
    turtle.forward()
  end
  turtle.turnRight()
  turtle.up()
  turtle.forward()
end
function floor()
  rowRight()
  rowLeft()
  rowRight()
  rowLeft()
  rowEnd()
end
function floorDown()
  turtle.forward()
  turtle.down()
  turtle.forward()
  turtle.forward()
  turtle.forward()
  turtle.forward()
  turtle.forward()
  turtle.forward()
end
function floorLast()
  rowRight()
  rowLeft()
  rowRight()
  rowLeft()
  for i=1,6 do
    turtle.placeDown()
    turtle.forward()
    turtle.placeDown()
  end
end
 
function cycle()
  floor()
  floor()
  floor()
  floor()
  floorLast()
  floorDown()
  floorDown()
  floorDown()
  floorDown()
end

floor()

itemCount = turtle.getItemCount(slotNum)
if itemCount == 0 then
    slotNum = slotNum + 1
    turtle.select(slotNum)
end

Thanks to all who helps people out in this forum, very appreciated !:)/>
Farrk #2
Posted 28 December 2013 - 06:22 PM
If its complete program you did not specify anywhere that slotNum should start with 1, just add


local slotNum = 1
somewhere in the program.
Bergy #3
Posted 28 December 2013 - 07:01 PM
I tried and i'ts still not working.
But thanks for your time and you make awesome programs dude ;)/>
OReezy #4
Posted 28 December 2013 - 09:49 PM
The problem is your select slot number function is in your if statement so out will only select the slot if slotnum == 0.

Edit: Edited with what the code should be
local slotnNum = 1
itemCount = turtle.getItemCount(slotNum)
if itemCount == 0 then
  slotNum = slotNum +1
end
turtle.select(slotNum)
Edited on 28 December 2013 - 09:05 PM
Bergy #5
Posted 28 December 2013 - 11:38 PM
Thanks OReezy I made the changes and now it works but only if the selected slot is empty when I run the program.
If in the middle of the row he runs out of items he just keep on going without switching.
OReezy #6
Posted 29 December 2013 - 12:06 AM
You will want to make your program loop and have it check if the slot is empty whenever necessary.
while true do
--do something
end
Also, you can have it cycle through all the slots until it finds one that isn't empty like this
for i = 1,16 do
  local itemCount = turtle.getItemCount(i)
  if itemCount ~= 0 then
    break
  end
end
Ideally you would want to save the itemCount to a variable outside the loop and have your program subtract 1 each time it places an item down. When it hits 0 you have it run the item check again to find the next slot with items in it.
aaa #7
Posted 30 December 2013 - 07:00 PM
What your programs does is :
1.it opens rednet
2.it declares a bunch of functions
3.it calls floor() –alright, it's what you want for now
4.finally, AFTER executing floor(), it check wether the current slot is empty and go to the first non empty.

Suggestions :
You either got to modify your functions to check before each placement of crop wether the current slot is empty, and then, if it is, select the first non empty slot (you can transform your 4 last lines of code in a function, or use the code of OReezy).

When you state
local slotNum = 1
have a
turtle.select(1)
just for sure ;-)

another solution : if you know how many crops you need per floor and manage to have enough in your slot before calling floor()
one way of doing it (detailed in english, not lua, you seem good enough to translate if you want) :
SpoilerLet's say there are 35 crops per floor, you can check wether the first slot contains more than 35 seed, and while it do not, goto the next non empty slot and transfer it's content to the first slot. Then, you know that your turtle either do not have enough seeds, or that the first slot contains >35, and you can run floor.()
Edited on 30 December 2013 - 06:07 PM
Bergy #8
Posted 31 December 2013 - 12:56 AM
Hey thanks man that solved my problem, it works now thanks x1000 man !
Farrk, OReezy and aaa you are a bunch of helpful people !