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

Why doesn't this if statement run as I want it to?

Started by Tea, 04 January 2013 - 05:21 PM
Tea #1
Posted 04 January 2013 - 06:21 PM
Hi, I'm building a basic floor filler to learn LUA. Here's the code:


--Startup
turtle.forward()
--Variables
itemcount=1
--Functions
function mainline()
repeat
  turtle.forward()
  turtle.placeDown()
until turtle.detect() == false
end
function returnto()
turtle.turnRight()
turtle.turnRight()
repeat
  turtle.forward()
until turtle.detect() == false
turtle.turnLeft()
turtle.forward()
turtle.turnLeft()
end
--Run
while turtle.getFuelLevel() > 100 do
if turtle.detect() then
  returnto()
else
  turtle.select(itemcount)
  mainline()
end
  if turtle.getItemCount(itemcount) == 1 then
   itemcount = itemcount + 1
   turtle.select(itemcount)
  end
end

There is no error, but when I set the program to run the turtle moves forwards and places blocks underneath itself as it should, but when it detects a block it does nothing. Can anybody figure out why this is?
Thanks very much.
Neraphi #2
Posted 04 January 2013 - 07:45 PM
Your repeat loops end when the turtle doesn't detect a block in front of it:
Try setting them to:

repeat
[indent=1]command[/indent]
until turtle.detect() == true
PrinzJuliano #3
Posted 05 January 2013 - 06:18 AM
if turtle.getItemCount(itemcount) == 1 then

You need to pre define the itemcount :

local itemcount = 0
–startup
Lyqyd #4
Posted 05 January 2013 - 06:45 AM
if turtle.getItemCount(itemcount) == 1 then

You need to pre define the itemcount :

local itemcount = 0
–startup

He already did. Please read the code before replying.

OP, here is a slightly edited version. Try this out, see if it does what you're looking for. It does need walls on both ends of the area it's filling, of course.


--Startup
turtle.forward()
--Variables
itemcount=1
--Functions
function mainline()
repeat
  turtle.forward()
  turtle.placeDown()
until turtle.detect() --go until we find a wall
end
function returnto()
turtle.turnRight()
turtle.turnRight()
repeat
  turtle.forward()
until turtle.detect() --go until we find a wall
turtle.turnLeft()
turtle.forward()
turtle.turnLeft()
end
--Run
while turtle.getFuelLevel() > 100 do
if turtle.detect() then
  returnto()
else
  turtle.select(itemcount)
  mainline()
end
  if turtle.getItemCount(itemcount) == 1 then
   itemcount = itemcount + 1
   turtle.select(itemcount)
  end
end
ChunLing #5
Posted 05 January 2013 - 09:57 AM
Hmmm…how does it go?

[[Sob]lbl[Sob]rbrSob] Or something like that.