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

If inside For Loop

Started by bubblesayoyo, 05 October 2012 - 11:08 PM
bubblesayoyo #1
Posted 06 October 2012 - 01:08 AM
What I'm trying to accomplish is to get a turtle to clear a path in front of it, turn to the side then come back. I have two functions (clearlong and wturn.) I want to input the values say 10 long and 3 wide. Basically I don't want it to turn after the last 'clearing.' So if I gave it the 3x3 I want it to clearlong 3 times and only turn 2 times. I think I have this right however it is still happening every time.

Here is my function…

function clearing()
for j=1, wide do
  clearlong()
  if j~= then
   wturn()
  end
end
end

When I test it with just numbers it seems to be working fine, however, when using my functions it just won't work. I'm just learning and trying out this coding I'm sorry if this is a simple question so any help would be very helpful, thanks.

wturn
function wturn()
if direction==1 then
  turtle.turnRight()
   if turtle.detect() then
	turtle.dig()
	digcount=digcount+1
	term.clearLine()
	term.setCursorPos(1,1)
	write("Current Dig Count Is: "..digcount)
   end
  turtle.forward()
  turtle.turnRight()
  direction=direction+1
elseif direction==2 then
  turtle.turnLeft()
   if turtle.detect() then
	turtle.dig()
	digcount=digcount+1
	term.clearLine()
	term.setCursorPos(1,1)
	write("Current Dig Count Is: "..digcount)
   end
   turtle.forward()
   turtle.turnLeft()
   direction=direction-1
end
end

clearlong
function clearlong()
for i=1,long do
   if turtle.detect() then
	turtle.dig()
	digcount=digcount+1
	term.clearLine()
	term.setCursorPos(1,1)
	write("Current Dig Count Is: "..digcount)
   end
  turtle.forward()
end
end
Orwell #2
Posted 06 October 2012 - 01:10 AM
Here is my function…

function clearing()
for j=1, wide do
  clearlong()
  if j~= then
   wturn()
  end
end
end

There should come something after 'if j~=' on line 4.
Orwell #3
Posted 06 October 2012 - 01:12 AM
Ignore this. Quoted myself >.<
bubblesayoyo #4
Posted 06 October 2012 - 01:21 AM
There is, it says 'wide' which is the argument that I have it in the beginning. So if I wanted a 4x2 it would be 2! I must have deleted somehow when copying it over to here. SORRY!
This is how it is in my turtle right now…
function clearing()
for j=1, wide do
  clearlong()
  if j~=wide then
   wturn()
  end
end
end
Orwell #5
Posted 06 October 2012 - 01:27 AM
There is, it says 'wide' which is the argument that I have it in the beginning. So if I wanted a 4x2 it would be 2! I must have deleted somehow when copying it over to here. SORRY!
This is how it is in my turtle right now…
function clearing()
for j=1, wide do
  clearlong()
  if j~=wide then
   wturn()
  end
end
end

What's your full code? Are the variables wide and direction declared somewhere?
bubblesayoyo #6
Posted 06 October 2012 - 09:56 PM
Here is the totality of it. basically i put clear long wide height and it will start clearing. Its working great, except its still running an 'extra' wturn on the last run of the 'clearing() for loop.' So say i put clear 4 2 1. (I haven't put height in yet thats why it's not here yet) I want it to clearlong 4 times and wturn once. If it was clear 4 4 1. It should clearlong 4 times and wturn 3 times. So inside my for loop I want to run clearing() everytime and wturn on every run EXCEPT the last time. Thanks for the help btw.
local tArgs = {...}
local long = tArgs[1]
local wide = tArgs[2]
local height = tArgs[3]
local digcount=0
direction=1
-----321GO-----
shell.run("cl")
shell.run("flevel")
sleep(1)
if long==nil and wide ==nil and height==nil then
write("n")
print("MUST SUPPLY LENTH, WIDTH &amp; HEIGHT!")
write("n")
write("What is the length: ")
long=read()
write("What is the width: ")
wide=read()
write("What is the height: ")
height=read()
end
if wide==nil and height==nil then
write("n")
print("MUST SUPPLY WIDTH &amp; HEIGHT")
write("n")
write("What is the width: ")
wide=read()
write("What is the height: ")
height=read()
end
if height==nil then
write("n")
print("MUST SUPPLY HEIGHT")
write("n")
write("What is the height: ")
wide=read()
end
write("Clearing..."..long.."x"..wide.."x"..height.." area")
sleep(1.5)
write("n")
print("3...")
sleep(.4)
print("2...")
sleep(.3)
print("1...")
sleep(.3)
print("GO!")
sleep(.8)
term.clear()
term.setCursorPos(1,1)
write("Current Dig Count Is: "..digcount)
shell.run("POS")
-----Functions-----
function clearlong()
for i=1,long do
   if turtle.detect() then
	turtle.dig()
	digcount=digcount+1
	term.clearLine()
	term.setCursorPos(1,1)
	write("Current Dig Count Is: "..digcount)
   end
  turtle.forward()
end
end
function wturn()
if direction==1 then
  turtle.turnRight()
   if turtle.detect() then
	turtle.dig()
	digcount=digcount+1
	term.clearLine()
	term.setCursorPos(1,1)
	write("Current Dig Count Is: "..digcount)
   end
  turtle.forward()
  turtle.turnRight()
  direction=direction+1
elseif direction==2 then
  turtle.turnLeft()
   if turtle.detect() then
	turtle.dig()
	digcount=digcount+1
	term.clearLine()
	term.setCursorPos(1,1)
	write("Current Dig Count Is: "..digcount)
   end
   turtle.forward()
   turtle.turnLeft()
   direction=direction-1
end
end

function clearing()
for j=1, wide do
  clearlong()
  if j~=wide then
   wturn()
  end
end
end
-----MAIN PROGRAM-----
clearing()
term.clear()
term.setCursorPos(1,1)
print("All Clear")
print("The Final Dig Count Is: "..digcount)
--shell.run("goto", startx, starty, startz)

so i've done some testing myself and i find that if I manually set wide=2 then it works as intended. however when i give it the argument of 2 then it doesn't work.