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

Loop keeps going?

Started by Vysse, 11 January 2013 - 07:27 PM
Vysse #1
Posted 11 January 2013 - 08:27 PM
I am making an automatic farmer and one of my loops keeps going infinitely it seems.

Pretty sure this is the code causing problems but I will post all of it just in case.


function harv(length,width,p)
gofwd(length,true)
turtle.turnRight()
gofwd(1,true)
turtle.turnRight()
local i = 1
repeat
  gofwd(length-1,true)
  lor(i)
  gofwd(1,true)
  lor(i)
  i = i + 1
until i == width
if p == true then
  plant(length,width)
end
end

All the code.

local args = {...}
local L = args[1]
local W = args[2]
local Pl = args[3]

function seedsel()
local i = 1
repeat
  i = i + 1
until turtle.getItemCount(i) > 0
return i
end

function gofwd(steps,dig)
local i = 1
repeat
  if dig == true then
   turtle.dig()
  end
  turtle.forward()
  i = i + 1
until i == steps + 1
end

function gobck(steps,dig)
local i = 1
repeat
  turtle.back()
  if dig == true then
   turtle.dig()
   turtle.select(seedsel)
   turtle.place()
  end
  i = i + 1
until i == steps + 1
end

function lor(num)
if num % 2 == 0 then
  turtle.turnRight()
else
  turtle.turnLeft()
end
end

function harv(length,width,p)
gofwd(length,true)
turtle.turnRight()
gofwd(1,true)
turtle.turnRight()
local i = 1
repeat
  gofwd(length-1,true)
  lor(i)
  gofwd(1,true)
  lor(i)
  i = i + 1
until i == width
if p == true then
  plant(length,width)
end
end

function plant(length,width)
turtle.turnRight()
turtle.turnRight()
local i = 1
repeat
  gobck(length-1,true)
  lor(i)
  gobck(1,true)
  lor(i)
  i = i + 1
until i == width
gobck(length,true)
end

function fuelcheck()
local fuelReq = L * W * 2
if turtle.getFuelLevel() < fuelReq then
  print("Not enough fuel. Required fuel is "..fuelReq..".")
  return false
else
  return true
end
end

function seedcheck()
local seedsreq = L * W
print("Please place "..seedsreq.." into the inventory. Then press ENTER")
local r = read("*")
end
if fuelcheck() then
seedcheck()
print("Harvesting the "..L.." x "..W.." area.")
harv(L,W,Pl)
end
theoriginalbit #2
Posted 11 January 2013 - 09:00 PM
add into the loop (just before the until)

print( "i = "..i.." width = "..width )

what are the values?


Nevermind I see the issue, change the args lines at the top to be

tonumber( args[ x ] )
where x is the arg number

the problem was you were comparing a number with a string… which will always be false
Edited on 11 January 2013 - 08:02 PM
Vysse #3
Posted 11 January 2013 - 09:16 PM
add into the loop (just before the until)

print( "i = "..i.." width = "..width )

what are the values?


Nevermind I see the issue, change the args lines at the top to be

tonumber( args[ x ] )
where x is the arg number

the problem was you were comparing a number with a string… which will always be false

lol I already did the first bit for debugging and it properly catches the width but i will try that second part and report back ty for the help. :D/>
Vysse #4
Posted 11 January 2013 - 09:18 PM
add into the loop (just before the until)

print( "i = "..i.." width = "..width )

what are the values?


Nevermind I see the issue, change the args lines at the top to be

tonumber( args[ x ] )
where x is the arg number

the problem was you were comparing a number with a string… which will always be false

lol I already did the first bit for debugging and it properly catches the width but i will try that second part and report back ty for the help. :D/>


that did it :D/> would tobool work to make it a boolean? I am new to lua am a vb.net programmer myself :P/>.
theoriginalbit #5
Posted 11 January 2013 - 09:20 PM
I don't think there is a tobool or toboolean, and googling seems to indicate there isn't… i always just do this

local var = ( someVar == "true" )
Vysse #6
Posted 11 January 2013 - 09:25 PM
I don't think there is a tobool or toboolean, and googling seems to indicate there isn't… i always just do this

local var = ( someVar == "true" )

your kinda confusing me here why are you comparing in a declaration?
theoriginalbit #7
Posted 11 January 2013 - 09:33 PM
ok how about this… does this make a little more sense?


function toboolean( str )
  if str ~= "true" or str ~= "false" then
	return nil
  end
  return str == "true"
end
then using it


someVar = toboolean( args[1] )
Vysse #8
Posted 11 January 2013 - 09:42 PM
ok how about this… does this make a little more sense?


function toboolean( str )
  if str ~= "true" or str ~= "false" then
	return nil
  end
  return str == "true"
end
then using it


someVar = toboolean( args[1] )

lol yes ty lua is confusing to me ty for all the help.
theoriginalbit #9
Posted 11 January 2013 - 09:43 PM
thats ok… we all started somewhere with Lua :)/>

just an fyi… the second way is a complex version of the first way… no advantage to it, just a little easier to read…