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

More Efficient Way To Empty Inventory?

Started by J-Nutz, 20 May 2014 - 01:30 PM
J-Nutz #1
Posted 20 May 2014 - 03:30 PM
I am working on a felling program right now and was wondering if you had a simpler way of clearing the extra stuff in the inventory? Here's my code.


--[[
	 Background info
	 Saplings go in slot 1
	 Bonemeal goes in slot 2
	 Coal goes in slot 16
	 Trying to empty the wood it cuts and any extra
	 Saplings and Bonemeal
--]]

-- Empty Inventory

function emptyInventory()

for inv = 3, 15, 1 do
turtle.select(inv)
  if turtle.compareTo(1) == true then
   turtle.dropDown()
  elseif turtle.compareTo(2) == true then
   turtle.turnLeft()
   turtle.drop()
   turtle.turnRight()
  end
end

end

-- End Empty Inventory
Edited on 20 May 2014 - 01:31 PM
theoriginalbit #2
Posted 20 May 2014 - 03:40 PM
not overly… its a pretty standard code…

you could improve it in this way though

local function emptyInventory()
  turtle.turnLeft()
  for i = 3, 15 do
	turtle.select( i )
	if turtle.compareTo(1) then
	  turtle.dropDown()
	elseif turtle.compareTo(2) then
	  turtle.drop()
	end
  end
  turtle.turnRight()
end

as you can see there's just some minor improvements, such as removing the redundant == true check, and the possibility of the Turtle turning a lot.
Edited on 20 May 2014 - 01:41 PM
J-Nutz #3
Posted 20 May 2014 - 03:57 PM
not overly… its a pretty standard code…

you could improve it in this way though

local function emptyInventory()
  turtle.turnLeft()
  for i = 3, 15 do
	turtle.select( i )
	if turtle.compareTo(1) then
	  turtle.dropDown()
	elseif turtle.compareTo(2) then
	  turtle.drop()
	end
  end
  turtle.turnRight()
end

as you can see there's just some minor improvements, such as removing the redundant == true check, and the possibility of the Turtle turning a lot.

Ahh, Thanks for the tips. Being a first time Lua programmer, these little things help me learn early and keep me from making mistakes and keeping my code as simple as possible. :D/>

Can never remember the whole 'Local' thing.
Edited on 20 May 2014 - 01:58 PM