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

[lua] bug in program variable won't increment

Started by splatt9990, 05 August 2012 - 09:52 PM
splatt9990 #1
Posted 05 August 2012 - 11:52 PM
This is my program:
Spoiler
function digFloor(k)
i=1
j=1
msg=tostring(k)
rednet.send(5, msg) --debugging effort
if k==1 or k==3 or k==5 then --sets the direction to turn for the first line in a layer
	 forward=true
  else
	 forward=false
  end
  for j=1,10,1 do --cuts out a 10x10x1 layer of blocks
	  for i=1,9,1 do
		turtle.dig()
		turtle.forward()
	  end
  if j~=10 then --prevents the turning from breaking the pattern on the last instance of loop
	   if forward == false then
		 turtle.turnLeft()
		 turtle.dig()
		 turtle.forward()
		 turtle.turnLeft()
		 forward=true
	   else
		 turtle.turnRight()
		 turtle.dig()
		 turtle.forward()
		 turtle.turnRight()
		 forward=false
	   end
  else
	 turtle.turnLeft()
	 turtle.turnLeft()
  end
  end
end
i=0
k=1
down=true
x,y= rednet.receive() --receives the command to start the process
print(y) --confirmation of message
  if turtle.detect() then --checks to see if layer at turtle's level
	down=false
  end
if y=="quarry" then
for i=1,5,1 do
  if down==false then
	digFloor(k)
k=k+1
down=true
  else --if not then digs down one and continues for 5 layers
	turtle.digDown()
	turtle.down()
	digFloor(k)
k=k+1
down=true
  end
end
shell.run("go", "left", "2") --goes back to start point
shell.run("go", "forward", "9")
shell.run("go", "right")
shell.run("go", "forward", 9)
shell.run("go", "up", "5")
  for i=1,9,1 do --drops off the contents into a chest
   turtle.select(i)
   turtle.drop(64)
  end
end


When I run it however, the variable k (a counter) doesn't increment like it should and causes the turtle to go in the wrong direction. I have no idea why it's not incrementing like it should.
Please Help

Edit: if you can't figure out why, offering a work around would also be acceptable..
kotorone1 #2
Posted 06 August 2012 - 05:47 AM
you use the function rednet.open() in another program? I don't see that here.
also

if y=="quarry" then
for i=1,5,1 do
if down==false then
digFloor(k)
k=k+1
down=true
else --if not then digs down one and continues for 5 layers
turtle.digDown()
turtle.down()
digFloor(k)
k=k+1
down=true
end
end[/color]
I'm no expert, but it looks like your "for i" doesn't work end until k = k+1 is written twice. I'm not sure if this is the problem, but that might be somewher to start.
Cranium #3
Posted 06 August 2012 - 09:40 AM
you use the function rednet.open() in another program? I don't see that here.
rednet.open() is not needed for turtles, since rednet is always open on the right side.
Noodle #4
Posted 06 August 2012 - 10:24 AM
It is required
rednet.open("right")
It must be.