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

A if -else problem

Started by konik98, 06 August 2012 - 04:37 PM
konik98 #1
Posted 06 August 2012 - 06:37 PM
Can someone tell me how can i write this in a way lua doesnt complain?

term.clear()
term.setCursorPos(1,1)
while true do
function Destroy()
if turtle.detect() then
turtle.dig()
turtle.forward()
turtle.digUp()
turtle.digDown()
  else
  turtle.turnLeft()
   else
   turtle.up()
end
end
print("Start Destroying?")
input = io.read()
if input == "yes" then
  while true do
  Destroy()
  end
end
end

I think the two elses make the problem ?
Edited on 06 August 2012 - 04:38 PM
Creator13 #2
Posted 06 August 2012 - 07:07 PM
Sure, you never can put two else's in one if statement. If you want multiple if's in a statement you can use the elseif.
In fact, I don't really see what the program should do (I'm not familiar with the turtle API, but I can see a little bit what it means). what you say now, is that if turtle.detect is true, it digs, it goes forward, it digs up, and digs down. If the turtle doesn't detect a block, it turns left and if it doesn't detect a block it goes up. I don't know what has to happen when it doesn't detect a block, but it doesn't seem very logical to me.

Please correct me if I'm wrong, by the way.
konik98 #3
Posted 06 August 2012 - 07:15 PM
Sure, you never can put two else's in one if statement. If you want multiple if's in a statement you can use the elseif.
In fact, I don't really see what the program should do (I'm not familiar with the turtle API, but I can see a little bit what it means). what you say now, is that if turtle.detect is true, it digs, it goes forward, it digs up, and digs down. If the turtle doesn't detect a block, it turns left and if it doesn't detect a block it goes up. I don't know what has to happen when it doesn't detect a block, but it doesn't seem very logical to me.

Please correct me if I'm wrong, by the way.
Well it's supposed to be able to bring down a house - dig it all out and when it goes to a corner it turns so it can continue to dig the house.
(Were planning a little war in my server so i wanna have tools :P/>/>)
but then i think about it i can copy some of the things in the excavate program ;)/>/>
Tgwizman #4
Posted 07 August 2012 - 12:03 PM
Here's a little something I just wrote that should fix your problem.

function Destroy()
if turtle.detect() then
turtle.dig()
turtle.forward()
turtle.digUp()
turtle.digDown()
  elseif turtle.turnLeft() then
   elseif turtle.up() then
    else
    return "ERROORRRRRZZZZ!!!!!1"
end
end
Putting the function call inside an if causes the return of the action to decide the turn out for you.
Luanub #5
Posted 07 August 2012 - 12:33 PM
turtle.turnLeft() will only rotate the turtle. So using it alone to check for a corner is not going to be very useful since it will always return true. You're basically going to have to turn the turtle after each move forward and check to see if a block is present or not. If so assume its a corner.


function Destroy()
if turtle.detect() then
turtle.dig()
turtle.forward()
turtle.digUp()
turtle.digDown()
turtle.turnLeft()
if not turtle.detect() then -- check for block to try and tell if we are at the corner yet
  turtle.turnRight() -- if its not turn back to the original direction and continue destroying the wall, otherwise will stayed turned
end
end
end

You also will want to remove the function Destroy from the while loop and just call the function instead.

Corrected Code

function Destroy()
if turtle.detect() then
  turtle.dig()
  turtle.forward()
  turtle.digUp()
  turtle.digDown()
  turtle.turnLeft()
  if not turtle.detect() then
    turtle.turnRight()
  end
else
  return -- to break the while loop
end
end

term.clear()
term.setCursorPos(1,1)
while true do
print("Start Destroying?")
input = io.read()
if input == "yes" then
  while true do
  Destroy()
  end
end
end