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

Need help with a basic code

Started by Kasea, 16 January 2014 - 03:43 AM
Kasea #1
Posted 16 January 2014 - 04:43 AM
What is the code for changing slots, like when slot 1 is empty change to slot 2, when slot 2 is empty change to slot 3 etc..


oh, and how do i repeat things? like a certain command line(s)
Edited on 16 January 2014 - 04:02 AM
wieselkatze #2
Posted 16 January 2014 - 05:02 AM
You could you use sth. like this:


for i = 1, 16 do
  if turtle.getItemCount(i) > 0 then
  turtle.select(i)
  break
  end
end
Edited on 16 January 2014 - 06:43 AM
CometWolf #3
Posted 16 January 2014 - 05:14 AM

for i = 1, 16 do
  turtle.select(i) -- select slot
  if turtle.getItemCount(i) > 0 then -- check slot for items
    break --ends loop if item in slot
  end
end
You forgot select(). Anyways, this is a basic for loop. The way it works is it repeats the given block of code an amount of times depending on the arguments given. It takes a variable name (i), a number to start at (1) and a number to end at (16). It also has a optional 3rd argument, which is the number to increase or decrease i per loop.
Bomb Bloke #4
Posted 16 January 2014 - 05:19 AM
Note that changing slots is quite slow. In practise you'd only want to switch to the one which holds the items.
CometWolf #5
Posted 16 January 2014 - 05:29 AM
Right, my bad haha. I thought that select only worked on the selected slot for a sec there. Put it above break instead.
wieselkatze #6
Posted 16 January 2014 - 07:44 AM
You forgot select().

Yeah, my bad, didn't think of that ^^ So, now it should work