Jump to content




Turtle doesn't switch to the next slot


5 replies to this topic

#1 n1ghtk1ng

  • Members
  • 58 posts

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


#2 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

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

#3 n1ghtk1ng

  • Members
  • 58 posts

Posted 05 January 2013 - 11:51 AM

View PostremiX, on 05 January 2013 - 11:25 AM, said:

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.computerc...i/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


#4 grand_mind1

  • Members
  • 207 posts

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.

#5 ChunLing

  • Members
  • 2,027 posts

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 by ChunLing, 05 January 2013 - 06:23 PM.


#6 n1ghtk1ng

  • Members
  • 58 posts

Posted 07 January 2013 - 09:10 AM

View PostChunLing, on 05 January 2013 - 06:20 PM, said:

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 !





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users