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

[Lua][Turtle][Debug]Turtle Hopper Help

Started by xInDiGo, 16 January 2013 - 10:27 AM
xInDiGo #1
Posted 16 January 2013 - 11:27 AM
so i'm working on my own turtle hopper, that will take loot from a chest above it, and drop it in a hopper below. the basic process is it scans its inventory for items. if its inventory is empty it should fill it up, then try to drop the items below it. if it can't drop the items then the turtle will sleep for a few moments before trying again. basically a loop of checking its inventory, filling its inventory, and trying to drop its inventory below it. here's what i'm working with:


local function drop()
for x=1,16 do
  if turtle.getItemCount(x) > 0 then
   turtle.select(x)
   turtle.dropDown()
  else
   sleep(3)
end
local function grab()
for x=1,16 do
  if turtle.getItemCount(x) > 64 then
   turtle.select(x)
   turtle.suckUp()
  else
   sleep(3)
end
grab()
drop()

i'm a beginner at this stuff so i may require a little bit more explanation of why this isn't working. thanks for your time!
Orwell #2
Posted 16 January 2013 - 11:37 AM
You're missing lots of ends. Also, 'turtle.getItemCount(x) > 64' will never be true, as the maximum size of an item stack is 64. :D/>
Here is what I made of it for now:

local function drop()
  for x=1,16 do
	if turtle.getItemCount(x) > 0 then
	  turtle.select(x)
	  turtle.dropDown()
	else
	  sleep(3)
	end -- close 'if ... else' statement
  end -- close 'for' statement
end -- close function

local function grab()
  for x=1,16 do
	if turtle.getItemCount(x) < 64 then  -- when the slot is not full, if that is what you want
	  turtle.select(x)
	  turtle.suckUp()
	else
	  sleep(3)
	end  -- close 'if ... else'
  end -- close 'for'
end -- close function

grab()
drop()
xInDiGo #3
Posted 16 January 2013 - 11:50 AM
ah yes! i knew i was missing an end or two from the errors i got but wasn't sure where. and the other was was a typo, thanks for catching that! :] so i've got the basic principle running, now i just need to add this to some kind of loop!

i was hoping


while true do
grab()
drop()
end

but that didn't work as i thought it would. it worked!! but now i don't know how to implement a turn off by pressing anykey or something like that :P/>
Orwell #4
Posted 16 January 2013 - 11:53 AM
An idea for the loop:

repeat
  grab()
  drop()
  os.startTimer(0.1)
  local event = os.pullEvent()
until event == "key"

Edit: also, when you make it sleep 3 seconds, it won't try to repeat the same action. It will just continue on after sleeping.
xInDiGo #5
Posted 16 January 2013 - 11:59 AM
orwell you're awesome! :D/> but can you explain what the os.startTimer and os.pullEvent are for? i'm assuming the os.pullEvent is the command to search for an event like a keypress, but what other events are there?

also, is there a reason you mentioned that about the sleep? i just put on a small delay so its not so spamy, is there a better method to this?
Orwell #6
Posted 16 January 2013 - 12:41 PM
You can find all about os.pullEvent on the wiki: http://www.computercraft.info/wiki/Os.pullEvent .
Events are being queued when they are passed to the system. os.pullEvent pulls the next event from that queue.
The first return value is the type of event that occured, the others are event specific.

The thing is that os.pullEvent will hold until an event could be pulled (it yields at that point). So when we check for a key event to be pulled, we don't want it to stay there until a random event occured. That's why I used 'os.startTimer', which makes a 'timer' event occur after a given time. That way we can be sure that os.pullEvent will continue after a specified amount of time. Thus, the variable 'event' will equal "timer" in most cases and "key" in the event of a key press.