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

Switching between slots

Started by Football52, 18 May 2015 - 01:02 AM
Football52 #1
Posted 18 May 2015 - 03:02 AM
So the only problem I'm having here is getting the turtle to switch between slots when the selected slot has 1 item in it. I can get through the first stack, and the second stack but it wont move to the 3rd, 4th and so on.

below is my code


function forward(blockCount)
  turtle.select(blockCount)

	if turtle.getItemCount(blockCount) == 1 or blockCount > 14 then
	  blockCount = blockCount + 1
	  turtle.select(blockCount)
	end

	turtle.placeDown()
	turtle.forward()
end

function turnRight()
  turtle.placeDown()
  turtle.turnRight()
  turtle.forward()
  turtle.turnRight()
end

function turnLeft()
  turtle.placeDown()
  turtle.turnLeft()
  turtle.forward()
  turtle.turnLeft()
end

rows = 30
columns = 30
blockCount = 1

for i = 0, columns do
  for j = 0, rows do
	forward(blockCount)
  end
  if i == 0 or i % 2 == 0 then
	turnRight()
  else
	turnLeft()
  end
end
Bomb Bloke #2
Posted 18 May 2015 - 03:35 AM
This chunk here:

        if turtle.getItemCount(blockCount) == 1 or blockCount > 14 then
          blockCount = blockCount + 1
          turtle.select(blockCount)
        end

You'd be better off expressing it as a while loop:

        while turtle.getItemCount(blockCount) == 1 or blockCount > 14 do
          blockCount = blockCount + 1
          -- Maybe reset blockCount back to 1 at this point, if it exceeds 16?
          turtle.select(blockCount)
        end

You might also consider switching to "turtle.getItemCount(blockCount) < 2 or blockCount == 14", as you probably want to skip past empty slots as well as ones only containing 1 item, and it's doubtful that you'd want to go up forever once exceeding a certain slot.
valithor #3
Posted 18 May 2015 - 05:21 AM
Bomb's solution is actually a different way of accomplishing the same goal. However, seeing the way you have set everything up this is probably more like what you meant to do the first time.

I will explain using examples:


function example(arg)
  print(arg)
end

example("hi") --# prints hi
In the above example argument is something you would pass to the function, but it should be noted that the arg variable is local to the function. That means that it can not be accessed outside of the function. This is just a short demonstration of function arguments.


arg = "hi"
function example()
  print(arg)
end

example() --# prints hi
This is just showing function arguments are not needed, which is where the error in your code originates

Now to get to your problem. You defined blockCount outside of the function, and also have a argument to the function named blockCount. When accessing the variable inside the function itself you will only ever access the local one, and I will demonstrate this with the following example.

value = 1
function example(value)
  value = value+1 --# changes the value of the local "value" variable not global
  print(value) --# prints value of local "value" variable
end

example(value) --# passing global "value" variable to function, and it prints 2
print(value) --# passing global "value" variable, and it prints 1

Now in this example you pass the variable "value" to the example function, and it adds 1 to the local "value" variable. The problem is because the function argument and global variable are the same, the function will only ever access the local variable. This means that the highest it will ever be able to reach is 2 because 1+1 = 2.

My suggestion is remove blockCount from the forward function's arguments, as it is not needed, and causes problems.
Edited on 18 May 2015 - 03:34 AM