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

Turtle Farm

Started by grand_mind1, 21 December 2012 - 06:06 PM
grand_mind1 #1
Posted 21 December 2012 - 07:06 PM
I am new to Lua and I'm trying to explore different options in what I can do with computercraft. Right now I am trying to make a small wheat farm.

num = turtle.getItemCount(3)
while num ~= 32 do
  turtle.place()
  turtle.select(2)
  turtle.place()
  turtle.select(1)
  turtle.dig()
end
What I thought this would do was place the seeds in slot 1, select slot 2 with the bonemeal, use the bonemeal, select the seeds again and break it. This part works but the while loop goes on until the bonemeal runs out when I thought it would only go until the item count in slot 3 (where the wheat goes) was 32.
Again, I am new to Lua and this may be completely wrong. Help is appreciated!
Thanks! :D/>
grand_mind1 #2
Posted 21 December 2012 - 07:10 PM
Crap! Just realized I put this in the wrong place! How do I move it?
Falco #3
Posted 21 December 2012 - 07:20 PM
I'm also new to LUA, but this might work.Let me know. :)/>


num = turtle.getItemCount(3)
while num ~= 32 do
  turtle.place()
  turtle.select(2)
  turtle.place()
  turtle.select(1)
  turtle.dig()
else
print("Wheat has been collected!") -- or whatever you want it to say when its full.
end

or…


num = turtle.getItemCount(3)
while num ~= 32 do
  turtle.place()
  turtle.select(2)
  turtle.place()
  turtle.select(1)
  turtle.dig()
else
if num ~= 32 then
print("Wheat has been collected!") -- or whatever you want it to say when its full.
end
end

Maybe someone could vouch this…?
Orwell #4
Posted 21 December 2012 - 09:51 PM
Crap! Just realized I put this in the wrong place! How do I move it?
Click the Report button under the opening post and put your request to move it to Ask A Pro in the message.
Ulthean #5
Posted 21 December 2012 - 09:52 PM
The problem is that 'num' (the itemcount in slot 3) is never updated since it isn't part of the loop.
The correct code would be:

num = turtle.getItemCount(3)
while num ~= 32 do
  turtle.place()
  turtle.select(2)
  turtle.place()
  turtle.select(1)
  turtle.dig()
  num = turtle.getItemCount(3)
end
Orwell #6
Posted 21 December 2012 - 09:55 PM
Or, to make people comfortable with other kinds of loops:

repeat
  turtle.place()
  turtle.select(2)
  turtle.place()
  turtle.select(1)
  turtle.dig()
  num = turtle.getItemCount(3)
until num == 32
Ulthean #7
Posted 21 December 2012 - 11:13 PM
Or, to make people comfortable with other kinds of loops:

#repeat-loop#

Depends on how strict he is about the condition. (in your case it will get executed at least once)
Anyway: for both our solutions the condition should be changed to:

while num < 32 do
...
end

Or:

repeat
...
until num >= 32