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

Dye harvesting program failing with strange error?

Started by FredMSloniker, 16 February 2013 - 08:27 AM
FredMSloniker #1
Posted 16 February 2013 - 09:27 AM
I wrote a program to use bone meal to make flowers on a grassy surface, then shear all the tall grass and stuff and dump it into a chest. However, my only rudimentary understanding of Lua must be failing me, because I'm getting odd errors.

The code is on Pastebin, but for posterity (I'll be changing it, after all!), here's its current state:


-- Dye harvesting program
-- clips the grass in front of the turtle, then moves forward.
function clipAndMove(n)
  for i=1,n do
	if turtle.detect() then
	  turtle.dig()
	end
	assert(turtle.forward())
  end
end
-- turns left, clips n spaces forward, turns left, clips n spaces forward.
function lClip(n)
  for i=1,2 do
	turtle.turnLeft()
	clipAndMove(n)
  end
end
-- sends a turtle with n units bone meal in slot 1 to perform n harvest cycles.
function harvestCycle(n)
  for i=1,n do
	lClip(3) -- move into position to place the bone meal.
	turtle.select(1) -- place the bone meal.
	assert(turtle.place())
	sleep(3)
	clipAndMove(1) -- move into position to begin the harvest spiral.
	turtle.turnRight()
	for j=1,9 do -- the harvest spiral.
	  lClip(j)
	end
	turtle.turnLeft()
	clipAndMove(9) -- the last leg of the harvest spiral.
	for j=2,16 do -- deposit our harvest in the output chest.
	  turtle.select(j)
	  turtle.drop()
	end
	if i=n then -- and on our last pass, there may be stuff in slot 1 too.
	  turtle.select(1)
	  turtle.drop()
	end
	turtle.turnLeft() -- move back to our starting point.
	clipAndMove(2)
	turtle.turnRight()
  end
end
-- the base program.
clipAndMove(1) -- move up to the input chest.
turtle.turnRight()
repeat -- use up all the bone meal in the input chest.
  turtle.select(1) -- attempt to collect a stack of bone meal.
  boneMealLeft=turtle.suck()
  if boneMealLeft then -- deploy the stack of bone meal and harvest the grass.
	harvestCycle(turtle.getItemCount(1))
  end
until !boneMealLeft
turtle.turnLeft() -- since we've used up all the bone meal, park by the recharge station.
turtle.back()

I load it onto the turtle with 'pastebin get XsxWbJ0f harvest', then type 'harvest', and get the error:

bios:338: [string "harvest"]:39: 'then' expected

…but as far as I can see, there is a 'then' on line 39. And given that, before I noticed a logic error and fixed it (namely, adding lines 39-42), the error complained of an unexpected symbol on what's now line 58, I suspect the real problem is somewhere else. Someone help me out! I want delicious dyes to make fireworks with!

(And on an unrelated note: what's with the 'code' tag replacing six spaces with a tab and two spaces, but not replacing four spaces with a tab?)
Lyqyd #2
Posted 16 February 2013 - 09:46 AM
Split into new topic.

Change `if i=n then` to use the comparison operator (==) rather than the assignment operator (=).
FredMSloniker #3
Posted 16 February 2013 - 01:17 PM
Thanks! There were a few other things I had to change to get it working, but that got me past the logjam. ("=" versus "==", we meet again.)