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

[Lua][Bug]If statement will not detect current loop iteration

Started by Artix3, 19 December 2012 - 03:47 AM
Artix3 #1
Posted 19 December 2012 - 04:47 AM
So I'm trying to make a program for a mining turtle that'll dig out a room x by y by z blocks. So far I've gotten it to dig out a 3x3x3 space, except at the end of its last loop, it repeats it's "dig up, go up" command. So I thought I'd make it detect if it was the last iteration of the loop with a simple if command, but it would not detect that. I even put some print() functions in that prove that the two variables are equal, but the if then does not run. Here's the code I have so far if that'll help. (x, y, and z are values you give the turtle.)


for i=1,z do
  for i=1,(x-1) do
	turtle.dig()
	turtle.forward()
  end
  for i=1,(y-1) do
	if (i % 2 == 0) then
	  turtle.turnRight()
	  turtle.dig()
	  turtle.forward()
	  turtle.turnRight()
	elseif (i % 2 == 1) then
	  turtle.turnLeft()
	  turtle.dig()
	  turtle.forward()
	  turtle.turnLeft()
	end
	for i=1,(x-1) do
	  turtle.dig()
	  turtle.forward()
	end
  end
--Problem Code: It should print "It worked" and stop the loop when  i  is equal to z
  print(i)
  print(z)
  if (i == z) then
	print("It worked")
	break
  end
--x
  if not (i == z) then
	turtle.digUp()
	turtle.up()
	turtle.turnLeft()
	turtle.turnLeft()
  end
end
OmegaVest #2
Posted 19 December 2012 - 05:04 AM
Well, I don't know this is the problem (because you never said if you DID prove i and z eventually become equivalent, and I can't check it presently), but I would see if changing the inner for loops iteration variable to something other than i helps at all.
Artix3 #3
Posted 19 December 2012 - 05:11 AM
Well, I don't know this is the problem (because you never said if you DID prove i and z eventually become equivalent, and I can't check it presently), but I would see if changing the inner for loops iteration variable to something other than i helps at all.

I say in the post that I had prints that proved that i and z were equal but it doesn't run the if then. And I've tried changing i to a seperate iz variable and having iz count up inside the loop, but that didn't work either.
theoriginalbit #4
Posted 19 December 2012 - 05:47 AM
I cant see any issues here o.O

Except your if statement to stop the loop when i == z. the loop does that you know, so that if i == z is redundant, just use the i ~= z one. maybe this, it shouldn't change anything but lets give it a try anyways just to rule it out completely if i ~= z then replacing if not i == z then


Btw when you ever do code like this

if (i == z) then
  -- do the stuff
end

if not (i == z) then
  -- do the other stuff
end

just remember that its logically the same to do this


if (i == z) then
  -- do the stuff
else
  -- do that stuff
end

only this last one is more efficient. :)/>
Artix3 #5
Posted 19 December 2012 - 06:06 AM
I cant see any issues here o.O

Except your if statement to stop the loop when i == z. the loop does that you know, so that if i == z is redundant, just use the i ~= z one. maybe this, it shouldn't change anything but lets give it a try anyways just to rule it out completely if i ~= z then replacing if not i == z then


Btw when you ever do code like this

if (i == z) then
  -- do the stuff
end

if not (i == z) then
  -- do the other stuff
end

just remember that its logically the same to do this


if (i == z) then
  -- do the stuff
else
  -- do that stuff
end

only this last one is more efficient. :)/>

To be fair the if you're referring to is actually just a debugging line of code, the if not below should cause the loop to break on the last iteration anyway, but neither the if not or the debugging code actually breaks the loop.

I just tried changing

if not (i == z) then
  --dig up and go up
end
to

if (i ~= z) then
  --dig up and go up
end
and the turtle still runs the code beneath it even if both i and z are 3.

Here's a couple of pictures to see if that'll help debug.



In that picture, the end of the loop has the turtle dig up and go up. On the last iteration, he should ignore that, as shown in the code. The picture shows he did otherwise.



As shown in the output, the variable i is 3 and the variable z is 3, so it should have noticed that in the if then and not run the code.
Lyqyd #6
Posted 19 December 2012 - 06:36 AM
Here's a couple more debug prints for you.

print(type(z))
print(type(i) == type(z))
Artix3 #7
Posted 19 December 2012 - 06:53 AM
Here's a couple more debug prints for you.

print(type(z))
print(type(i) == type(z))

Alright, so I added that and it tells me that z is a string and i is a number. I'm going to try adding a tonumber to the variables.

That fixed it. Thanks, guess it was just a type incompatibility.
Epicmonkeydude #8
Posted 19 December 2012 - 07:37 AM
I know this isn't what you need but I would suggest doing something like this to save fuel. This way your mining three layers at once.

turtle.dig()
turtle.forward()
turtle.digDown()
turtle.digUp()
and for two just remove the dig up or down function.
Hope this helps you in some way… :D/>