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

Not entirely sure of the answer

Started by jaykaybay, 31 May 2013 - 09:12 PM
jaykaybay #1
Posted 31 May 2013 - 11:12 PM
I've been thinking about a tunneling script for a turtle that when it's inventory is full dumps it to an enderchest in slot one, but I can't seem to work out how to define a variable that is defined by the value of a turtle function,

n = return turtle.getItemCount(15) -- I think this is how you check if the turtle has inv space?
local function Dump() -- Dumps entire inventory to an ender chest in slot 1 then breaks the chest
turtle.dig()
turtle.place(1)
turtle.select(2)
turtle.drop(64)
turtle.select(3)
turtle.drop(64)
turtle.select(4)
turtle.drop(64)
turtle.select(5)
turtle.drop(64)
turtle.select(6)
turtle.drop(64)
turtle.select(7)
turtle.drop(64)
turtle.select(8)
turtle.drop(64)
turtle.select(9)
turtle.drop(64)
turtle.select(10)
turtle.drop(64)
turtle.select(11)
turtle.drop(64)
turtle.select(12)
turtle.drop(64)
turtle.select(13)
turtle.drop(64)
turtle.select(14)
turtle.drop(64)
turtle.select(15)
turtle.drop(64)
turtle.select(16)
turtle.drop(64)
turtle.select(1)
sleep(20)
turtle.dig()
end

local function Mine() -- mining script not much to see here
turtle.dig()
turtle.forward()
turtle.digUp()
end

while n<1 do -- trying to say as long as there is room in slot 15, mine
Mine()
end

if n>1 then
Dump()
end
Edited by
Lyqyd #2
Posted 31 May 2013 - 11:27 PM
Split into new topic.
Bomb Bloke #3
Posted 31 May 2013 - 11:53 PM
n = return turtle.getItemCount(15) – I think this is how you check if the turtle has inv space?
As turtles collect items, they put them into the first possible slot they can go into. That generally means that if their last slots are full, so are all the earlier ones.

while n<1 do – trying to say as long as there is room in slot 15, mine
Mine()
end
It's easier to use this, and forget about "n":

while turtle.getItemCount(15)<1 do -- trying to say as long as there is room in slot 15, mine
Mine()                             -- ... Though did you mean to say slot 16?
end

If you really, really wanted to use your own function, you could use something like this:

function invNotFull()
  return turtle.getItemCount(15)<1  -- This returns the result of the conditional check,
                                    -- which could either be "true" or "false".
end

.
.
.

while true do
  while invNotFull() do
    Mine()
  end

  Dump()
end

turtle.select(2)
turtle.drop(64)
turtle.select(3)
turtle.drop(64)
turtle.select(4)
turtle.drop(64)
.
.
.

Argh NO.

Let me introduce you to "for" loops. These little guys can count for you.

local function Dump() -- Dumps entire inventory to an ender chest in slot 1 then breaks the chest
  turtle.dig()
  turtle.place(1)

  for i=2,16 do
    turtle.select(i)
    turtle.drop()
  end

  turtle.select(1)
  sleep(20)
  turtle.dig()
end

The first time the code within the "for" block runs, "i" is set to 2, so "turtle.select(i)" will select slot 2. Once that code block is complete, "i" is incremented by 1 to a total of 3, and the code block runs again. The loop continues until "i" would exceed 16, at which point the program carries on from the point under the "for" block.

Oh, and notice that there's no need to specify a value when using "turtle.drop()" if you want to pitch the whole stack of items at once.
jaykaybay #4
Posted 01 June 2013 - 12:27 AM
Thank you, that really clears up things.