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

Conditional statement being triggered when it shouldn't?

Started by Chamimnya, 16 April 2015 - 12:00 AM
Chamimnya #1
Posted 16 April 2015 - 02:00 AM
Hi all,

For some reason my turtle program is triggering a conditional statement every time it runs, when it shouldn't be.

My program is supposed to create stairs in my existing quarry. I have it set to detect if turtle.placeDown() returns false, it will print an error and break out of the loop. However, every time I run the loop, it detects turtle.placeDown() as false (when it did place down a block) and breaks the loop.

How can I fix this?

Code snippet:

if not turtle.placeDown() then
  print("Error - could not place block")
  print("Check fuel level or existing blocks")
  break
end

I've even tried using

if turtle.placeDown() == false then
but that doesn't work either.

Full program code: http://pastebin.com/YCfjbYwc

On a side note, is it possible to convert strings to boolean variables for use with returnToTop? I've heard of tostring and tonumber, but does toboolean exist?
Edited on 16 April 2015 - 12:19 AM
Bomb Bloke #2
Posted 16 April 2015 - 02:27 AM
The line directly above your "snippet" reads:

turtle.placeDown()

So when you go to run:

if not turtle.placeDown() then

… turtle.placeDown() will obviously fail (as if the line above failed, then this second attempt will fail for the same reason; and if the line above succeeded, then there's a block under the turtle and so another one can't be placed).

On a side note, is it possible to convert strings to boolean variables for use with returnToTop? I've heard of tostring and tonumber, but does toboolean exist?

There's no "toboolean()" function, though you could always write one to suit your tastes. I like to just use this sort of construct:

if read():sub(1, 1):lower() == "y" then  -- If the first letter of whatever is typed, when converted to lowercase, is a "y", then...
Chamimnya #3
Posted 16 April 2015 - 03:29 AM
-snip-

Thanks, I got rid of the first turtle.placeDown() and now it works perfectly. I didn't realize the code in the if statement would actually run.

As for converting to boolean, I'll just use a simple one for now:

input = read()
if input == "yes" then
  returnToTop = true
elseif input == "no" then
  returnToTop = false
else
  print("Invalid input. Use 'yes' or 'no'.")
end
Edited on 16 April 2015 - 01:30 AM