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

Bridge building turtle problem

Started by dennisboy1111, 02 November 2012 - 09:12 AM
dennisboy1111 #1
Posted 02 November 2012 - 10:12 AM
It builds this bridge but when it runs out of stone bricks it builds with air and i want it to choose his next inventory slot, i just don't know how to add it

[attachment=644:Turtle.png]

for i =0,10 do
turtle.forward()
turtle.placeDown()
turtle.turnLeft()
turtle.forward()
turtle.placeDown()
turtle.forward()
turtle.placeDown()
turtle.turnRight()
turtle.forward()
turtle.placeDown()
turtle.turnRight()
turtle.forward()
turtle.placeDown()
turtle.forward()
turtle.placeDown()
turtle.turnLeft()
print(......bzzz......)
else
end

jag #2
Posted 02 November 2012 - 11:04 AM
Why do you got a else in there?
DrBreakalot #3
Posted 02 November 2012 - 11:06 AM
Try taking a look at turtle.select() and turtle.getItemCount()
jag #4
Posted 02 November 2012 - 11:12 AM
What you could do is

turtle.select(1)
local slot = 1

function selectItem()
  if turtle.getItemCount(slot) == 0 then
    if slot == 16 then
      return false
    end
    for i = slot+1, 16 do
      if turtle.getItemCount > 0 then
        slot = i
        turtle.select(slot)
        return true
      end
    end
    return false
  end
end


for i =0,10 do
  turtle.forward()
  while not selectItem do sleep(.5) end
  turtle.placeDown()
  turtle.turnLeft()
  turtle.forward()
  while not selectItem do sleep(.5) end
  turtle.placeDown()
  turtle.forward()
  while not selectItem do sleep(.5) end
  turtle.placeDown()
  turtle.turnRight()
  turtle.forward()
  while not selectItem do sleep(.5) end
  turtle.placeDown()
  turtle.turnRight()
  turtle.forward()
  while not selectItem do sleep(.5) end
  turtle.placeDown()
  turtle.forward()
  while not selectItem do sleep(.5) end
  turtle.placeDown()
  turtle.turnLeft()
  print(......bzzz......)
end
Now this code could've been made better, but to give you the basic idea..
ChunLing #5
Posted 02 November 2012 - 11:49 AM
I tend to use math.fmod()+n to ensure a value stays in a given bound rather than the if structure. I have to wonder whether there's a performance gain or it just makes my code shorter. I know that it is a little bit more robust, at least.
dennisboy1111 #6
Posted 03 November 2012 - 02:40 AM
Why do you got a else in there?
The else was from the code when i tryed to add the itemcount but it always crashed so i tryed without it and forgot to remove the else[quote



name='jag_e_nummer_ett' timestamp='1351854741' post='46589']
What you could do is

turtle.select(1)
local slot = 1

function selectItem()
  if turtle.getItemCount(slot) == 0 then
	if slot == 16 then
	  return false
	end
	for i = slot+1, 16 do
	  if turtle.getItemCount > 0 then
		slot = i
		turtle.select(slot)
		return true
	  end
	end
	return false
  end
end


for i =0,10 do
  turtle.forward()
  while not selectItem do sleep(.5) end
  turtle.placeDown()
  turtle.turnLeft()
  turtle.forward()
  while not selectItem do sleep(.5) end
  turtle.placeDown()
  turtle.forward()
  while not selectItem do sleep(.5) end
  turtle.placeDown()
  turtle.turnRight()
  turtle.forward()
  while not selectItem do sleep(.5) end
  turtle.placeDown()
  turtle.turnRight()
  turtle.forward()
  while not selectItem do sleep(.5) end
  turtle.placeDown()
  turtle.forward()
  while not selectItem do sleep(.5) end
  turtle.placeDown()
  turtle.turnLeft()
  print(......bzzz......)
end
Now this code could've been made better, but to give you the basic idea..

I tryed this code and and it needs to be print"…….bzzz……." but thats my fault
when i tryed this code and it runs out of blocks in the first slot it keeps building with slot one selected,

So, this is the code i have right now,



for i =0,10 do
turtle.select(1)
local slot = 1
function selectItem()
  if turtle.getItemCount(slot) == 0 then
    if slot == 16 then
	  return false
    end
    for i = slot+1, 16 do
	  if turtle.getItemCount > 0 then
	    slot = i
	    turtle.select(slot)
	    return true
	  end
    end
    return false
  end
end

  turtle.forward()
  while not selectItem do sleep(.5) end
  turtle.placeDown()
  turtle.turnLeft()
  turtle.forward()
  while not selectItem do sleep(.5) end
  turtle.placeDown()
  turtle.forward()
  while not selectItem do sleep(.5) end
  turtle.placeDown()
  turtle.turnRight()
  turtle.forward()
  while not selectItem do sleep(.5) end
  turtle.placeDown()
  turtle.turnRight()
  turtle.forward()
  while not selectItem do sleep(.5) end
  turtle.placeDown()
  turtle.forward()
  while not selectItem do sleep(.5) end
  turtle.placeDown()
  turtle.turnLeft()
  print"......bzzz......"
end
it builds but when it runs out it doesn't select the next slot anyway to fix this ?
jag #7
Posted 03 November 2012 - 02:56 AM

while not selectItem() do slot = 1 sleep(.5) end
I forgot the () <- brackets and to add slot = 1 so that it resets itself.
dennisboy1111 #8
Posted 08 November 2012 - 08:38 AM
k thx