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

Turtle doesn't switch to the next slot

Started by n1ghtk1ng, 05 January 2013 - 10:21 AM
n1ghtk1ng #1
Posted 05 January 2013 - 11:21 AM
I hope the title isn't misleading. I made a program that makes the outline of my tower, layer by layer, the design is like this :
M = marble
B = stone Bricks
BMMMB and so on.
For some reason, whenever it runs out of blocks, it doesn't switch to the next slot(even though I have a function that does so)


--[[ VARIABLES ]]--
local slot = 1
local b = turtle.getItemCount(slot)
local turn = 0
local running = true
--[[ FUNCTIONS ]]--
function switchBrick()
BrickSlot1 = 1
BrickSlot2 = 3
BrickSlot3 = 5

slot = BrickSlot1
turtle.select(slot)
if turtle.select(slot) == true and b == false then
  slot = BrickSlot2
  if turtle.select(slot) == true and b == false then
   slot = BrickSlot3
   if turtle.select(slot) == true and b == false then
    running = false
    print("Out of Stone Bricks!")
   end
  end
end
end
function switchMarble()
MarbleSlot1 = 2
MarbleSlot3 = 4
MarbleSlotA = 6,16

slot = MarbleSlot1
if turtle.select(slot) == true and b == false then
  slot = MarbleSlot3
  if turtle.select(slot) == true and b == false then
   slot = 6
   if turtle.select(slot) == true and b == false then
    slot = 7
    if turtle.select(slot) == true and b == false then
	 running = false
    end
   end
  end
end
end
function towerMain()
switchBrick()
turtle.back()
turtle.place()
switchMarble()
turtle.back()
turtle.place()
turtle.back()
turtle.place()
end
--[[ MAIN CODE ]]--
while running do
towerMain()
if turtle.back() == false then
  turtle.turnLeft()
  turn = turn + 1
end
turtle.place()

if turn == 4 then
  turtle.up()
  turtle.back()
  turtle.turnLeft()
  turn = 0
end
end
remiX #2
Posted 05 January 2013 - 11:25 AM
I don't think turtle.select() returns anything and put
local b = turtle.getItemCount(slot)
so it will update b everytime you call the function
n1ghtk1ng #3
Posted 05 January 2013 - 11:51 AM
I don't think turtle.select() returns anything and put
local b = turtle.getItemCount(slot)
so it will update b everytime you call the function
Unless I misunderstood that, It does the same thing
Also, turtle.select() returns a boolean
http://www.computercraft.info/wiki/Turtle.select

--[[ VARIABLES ]]--
local slot = 1
local b = turtle.getItemCount(slot)
local turn = 0
local running = true
--[[ FUNCTIONS ]]--
function switchBrick()
BrickSlot1 = 1
BrickSlot2 = 3
BrickSlot3 = 5

slot = BrickSlot1
turtle.select(slot)
if turtle.select(slot) == true and b == false then
  slot = BrickSlot2
  local b = turtle.getItemCount(slot)
  if turtle.select(slot) == true and b == false then
   slot = BrickSlot3
   local b = turtle.getItemCount(slot)
   if turtle.select(slot) == true and b == false then
    running = false
    print("Out of Stone Bricks!")
   end
  end
end
end
function switchMarble()
MarbleSlot1 = 2
MarbleSlot3 = 4
MarbleSlotA = 6,16

slot = MarbleSlot1
if turtle.select(slot) == true and b == false then
  slot = MarbleSlot3
  local b = turtle.getItemCount(slot)
  if turtle.select(slot) == true and b == false then
   slot = 6
   local b = turtle.getItemCount(slot)
   if turtle.select(slot) == true and b == false then
    slot = 7
    local b = turtle.getItemCount(slot)
    if turtle.select(slot) == true and b == false then
	 running = false
    end
   end
  end
end
end
function towerMain()
switchBrick()
turtle.back()
turtle.place()
switchMarble()
turtle.back()
turtle.place()
turtle.back()
turtle.place()
end
--[[ MAIN CODE ]]--
while running do
towerMain()
if turtle.back() == false then
  turtle.turnLeft()
  turn = turn + 1
end
turtle.place()

if turn == 4 then
  turtle.up()
  turtle.back()
  turtle.turnLeft()
  turn = 0
end
end
grand_mind1 #4
Posted 05 January 2013 - 01:06 PM
I think this is a problem but I havent tested it. You define b as turtle.getItemCount() which returns an integer but in your first function you try to find if it is true or false.
ChunLing #5
Posted 05 January 2013 - 06:20 PM
Yeah, that's definitely a problem. b is never going to be false. In fact, it is never going to be anything other than the count of blocks in slot 1 when the program started. And all those other locally scoped b's never get allocated. I do not even know what you're trying to pull there. Try this:
function switchBrick()
for i=1,5,2 do
	if turtle.getItemCount(i) > 0 then
		turtle.select(i)
		return true
	end
end
	print("Out of Stone Bricks!")
return false
end
Basically, we check slots 1, 3, and 5 for having more than 0 items. If any of them do, then we select that slot and return true. Otherwise, we print "Out of Stone Bricks!" and return false. I think you may have been trying to do essentially this, but if there was anything else that function was supposed to do then you're going to have to tell me because it is not very readable.

switchMarble() is also problematical, unfortunately I'm not sure that the structure is exactly comparable, frankly you'll have to explain that one.
Edited on 05 January 2013 - 05:23 PM
n1ghtk1ng #6
Posted 07 January 2013 - 09:10 AM
Yeah, that's definitely a problem. b is never going to be false. In fact, it is never going to be anything other than the count of blocks in slot 1 when the program started. And all those other locally scoped b's never get allocated. I do not even know what you're trying to pull there. Try this:
function switchBrick()
for i=1,5,2 do
	if turtle.getItemCount(i) > 0 then
		turtle.select(i)
		return true
	end
end
	print("Out of Stone Bricks!")
return false
end
Basically, we check slots 1, 3, and 5 for having more than 0 items. If any of them do, then we select that slot and return true. Otherwise, we print "Out of Stone Bricks!" and return false. I think you may have been trying to do essentially this, but if there was anything else that function was supposed to do then you're going to have to tell me because it is not very readable.

switchMarble() is also problematical, unfortunately I'm not sure that the structure is exactly comparable, frankly you'll have to explain that one.
Oh god, that was a stupid mistake, thanks !