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

[Lua][Error] Turtle API Help

Started by Connor_13_, 29 January 2013 - 03:16 PM
Connor_13_ #1
Posted 29 January 2013 - 04:16 PM
hey everyone,

so the api i have made so far works except for one thing… i want to reset the value of the variables within the loops back to what they were originally declared as.

here is my code… the issue should be stated towards the bottom of the code.
Spoiler

function test2()
--Length/Width
print("Lenth/ Width:")
x = tonumber(read())
--Height
print("Height:")
y = tonumber(read())
i = 0
j = 0
k = 0
l = 0
turtle.refuel(1)
--First Horizontal
while i < x do
  turtle.dig()
  turtle.forward()
  i = i + 1
end
turtle.turnRight()
turtle.dig()
turtle.forward()
turtle.turnRight()
--The remainder of Horizontal
while j < x do
  while k < x - 1 do
   turtle.dig()
   turtle.forward()
   k = k + 1
  end
  turtle.turnLeft()
  turtle.dig()
  turtle.forward()
  turtle.turnLeft()
  k = k + 1
  while l < x - 1 do
   turtle.dig()
   turtle.forward()
   l = l + 1
  end
  turtle.turnRight()
  turtle.dig()
  turtle.forward()
  turtle.turnRight()
  --Must reset variables somehow before repeat
end
end
Edit: My code requires my variables that are stated to be 0 (example: i = 0 etc..) to be reset to zero after the loop has been completed.. thats what i dont know how to do.
GravityScore #2
Posted 29 January 2013 - 04:39 PM
Ummm… Why not just reset them by setting them all back to zero? Just copy and paste your declaration code at the bottom of your loop.

Code:
Spoiler


y = tonumber(read())
i = 0
j = 0
k = 0
l = 0
turtle.refuel(1)
--First Horizontal
while i < x do
  turtle.dig()
  turtle.forward()
  i = i + 1
end
turtle.turnRight()
turtle.dig()
turtle.forward()
turtle.turnRight()
--The remainder of Horizontal
while j < x do
  while k < x - 1 do
   turtle.dig()
   turtle.forward()
   k = k + 1
  end
  turtle.turnLeft()
  turtle.dig()
  turtle.forward()
  turtle.turnLeft()
  k = k + 1
  while l < x - 1 do
   turtle.dig()
   turtle.forward()
   l = l + 1
  end
  turtle.turnRight()
  turtle.dig()
  turtle.forward()
  turtle.turnRight()
  k = 0
  l = 0
  -- Note: setting x and j to zero will end the loop immediately. Is that the desired behaviour? I don't think it is.
end
end
ChunLing #3
Posted 29 January 2013 - 06:15 PM
More fundamentally, why are you using while loops rather than numeric for loops?