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

Tree farm help While statement

Started by Tohrik, 13 April 2016 - 05:47 AM
Tohrik #1
Posted 13 April 2016 - 07:47 AM
Spoiler

function deposit()
turtle.select(2)
turtle.dropDown()
end
function chop()
local success,data = turtle.inspect()
  if data.name == "minecraft:log"
  then
	turtle.dig()
	turtle.forward()
while turtle.detectUp() == true
do
  turtle.digUp()
  turtle.up()
end
while turtle.detectDown() == false do
  turtle.down()
end
  turtle.back()
  turtle.select(3)
  turtle.place()
  turtle.select(1)
end
end
function fuellevel()
local fuel = turtle.getFuelLevel()
  if fuel<20 then
	turtle.select(1)
	turtle.refuel(1)
  end
end
function movealong()
  for i=1,3,1 do
  leftright()
  turtle.forward()
  turtle.forward()
  end
end
function leftright()
  turtle.turnLeft()
  chop()
  turtle.turnRight()
  turtle.turnRight()
  chop()
  turtle.turnLeft()
end
function actualmove()
   movealong()
   leftright()
   turtle.forward()
   deposit()
   turtle.forward()
   movealong()
end
function turnright()
   turtle.forward()
   deposit()
   turtle.turnRight()
   turtle.forward()
   turtle.forward()
   turtle.turnRight()
   turtle.forward()
end
function turnleft()
   turtle.forward()
   deposit()
  turtle.turnLeft()
  turtle.forward()
  turtle.forward()
  turtle.turnLeft()
  turtle.forward()
end
function loopdelooping()
  for tempvalue=1,3,1 do
	 actualmove()
	 turnright()
	 actualmove()
	 turnleft()
end
  actualmove()
  turnright()
  actualmove()
  turtle.forward()
  turtle.deposit()
  turtle.turnRight()
  for h=1,14,1 do
	 turtle.forward()
  end
  turtle.turnRight()
  turtle.forward()
end
function suckmeup()
  turtle.suck()
end
  local chopme = true
	 while chopme do
		fuelcheck()
		loopdelooping()
	   suckmeup()
	   turtle.suckup()
end
So this is my second time using Lua. This is for a 15x5 tree farm.
My problem is that in my while statement it doesn't want to seem to execute turtle.suck and it only goes around one time. What am I doing wrong?

Sorry if my code is a bit messy and thanks in advance.
Lupus590 #2
Posted 13 April 2016 - 08:58 AM
turtle.suckUp()

capital u
Tohrik #3
Posted 13 April 2016 - 09:29 AM
Changed it, still just pushes it the sapling forward.
Bomb Bloke #4
Posted 13 April 2016 - 10:47 AM
I've got a hunch it's got something to do with your mystery "fuelcheck()" function.

It's not defined in this script, so either you're not showing us the code you've got on your turtle, or you've executed some other script which defined it. Because you're not localising your functions, they stay in memory (in the shell's environment table, used for storing globals) until the turtle shuts down, meaning that this later script can still call them…

http://lua-users.org/wiki/ScopeTutorial

I'm also not sure when you're wanting the turtle to suck. At the moment you're only ordering it once per complete loop, so certainly most saplings are going to be pushed around.
Tohrik #5
Posted 13 April 2016 - 05:51 PM
I'm wanting the saplings to be sucked up continuously,.since there isn't a specific point in time that a sapling would drop. Sorry about the fuelcheck function that's meant to be changed to fuellevel().
Bomb Bloke #6
Posted 14 April 2016 - 01:15 AM
In that case, consider tying suction to a function for moving forward:

local function forward(amount)
  for i = 1, amount do
    turtle.suck()
    turtle.suckUp()
    repeat until turtle.forward()  --# turtle.forward() returns false if it fails for some reason, eg a mob in the way
  end
end