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

If Statement not "triggering" in while loop.

Started by ultimate88t, 10 November 2016 - 03:35 AM
ultimate88t #1
Posted 10 November 2016 - 04:35 AM
Here is the code: http://pastebin.com/BmfPnDia

The program name is "miner" so I enter "miner 5" and I would expect it to go five blocks and stop, yet it never stops because the if count == length statement never gets triggered even though those two values do equal each other at some point, hence when it travels 5 blocks. I can get this to work by modifying the code to a hard value but cannot get it to work with this method.
Dog #2
Posted 10 November 2016 - 05:57 AM
I *think* the easy fix is to convert your argument to a number. I believe the values in an args table are all strings, so your number value will never be equal to your string.

local length = tonumber(args[1])

However, the better approach would probably be to eliminate the count variable and do something like this…

local length = tonumber(args[1])

function dig()
  for i = 1, length do
    turtle.dig()
    turtle.digUp()
    while not turtle.forward() do
      turtle.dig()
    end
  end
end
Edited on 10 November 2016 - 04:59 AM
ultimate88t #3
Posted 10 November 2016 - 03:30 PM
I *think* the easy fix is to convert your argument to a number. I believe the values in an args table are all strings, so your number value will never be equal to your string.

local length = tonumber(args[1])

However, the better approach would probably be to eliminate the count variable and do something like this…

local length = tonumber(args[1])

function dig()
  for i = 1, length do
	turtle.dig()
	turtle.digUp()
	while not turtle.forward() do
	  turtle.dig()
	end
  end
end

I figured it had to do with the args table. I have used your method of the dig() function, yet if the turtle encounters a large stack of gravel it messes up the counting and adds to the for loop despite it not actually moving a block because of the gravel. I found a solution to this in another thread using a different method after searching some more, here is the link for anyone who has a similar issue. http://www.computercraft.info/forums2/index.php?/topic/1263-how-do-you-gravel-proof-a-mining-program/
Dog #4
Posted 10 November 2016 - 03:59 PM
I figured it had to do with the args table. I have used your method of the dig() function, yet if the turtle encounters a large stack of gravel it messes up the counting and adds to the for loop despite it not actually moving a block because of the gravel. I found a solution to this in another thread using a different method after searching some more, here is the link for anyone who has a similar issue. http://www.computerc...mining-program/
I'm pretty sure you haven't used the dig() function I posted. The code I posted won't get messed up by gravel or sand - that's what the while not turtle.forward() do loop does.
ultimate88t #5
Posted 10 November 2016 - 07:50 PM
I figured it had to do with the args table. I have used your method of the dig() function, yet if the turtle encounters a large stack of gravel it messes up the counting and adds to the for loop despite it not actually moving a block because of the gravel. I found a solution to this in another thread using a different method after searching some more, here is the link for anyone who has a similar issue. http://www.computerc...mining-program/
I'm pretty sure you haven't used the dig() function I posted. The code I posted won't get messed up by gravel or sand - that's what the while not turtle.forward() do loop does.

Terribly sorry, I had skimmed your code and assumed I had already tried it, I was using and IF not forward instead of WHILE not forward, and that made all the difference, thank you for you help, this is the easiest solution I have found to my problem. Thank you!
Dog #6
Posted 10 November 2016 - 08:32 PM
No apology necessary :)/> You're very welcome - I'm happy to help.