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

turtle.select() & While loops?

Started by Rolibar, 23 March 2012 - 07:59 PM
Rolibar #1
Posted 23 March 2012 - 08:59 PM
Hi there! I was recently trying to make a simple program for a turtle to make a one block high wall with alternating materials, switching between slot 1 and 2.

Here's the code:


x = 0

while x < 5 do
turtle.select(1)
turtle.place()
turtle.turnRight()
turtle.forward()
turtle.turnLeft()
turtle.select(2)
turtle.place()
x = x+1
end


For some reason it'll place the first type of block just fine when making the wall. Unfortunately after it places block 2, it will only place block 2 repeatedly, never using block 1 again despite turtle.select(1) at the start of the loop.

Is there a problem with using the turtle.select() command in a while loop? It seems to get stuck after the first loop and will only place block type 2.

Sorry if this is a stupid question, I'm trying to learn. D:
PatriotBob #2
Posted 23 March 2012 - 09:14 PM
Your problem is that at the end of the loop it places block #2, the goes back to the top of the loop, selects block #1 and tries to place it and fails…

It never moved between block #2 being place and attempting to place block #1

Try


local x = 0 // I don't cluttering global space
while x < 5 do

turtle.select(1)
turtle.place()
turtle.turnRight()
turtle.forward()
turtle.turnLeft()
turtle.select(2)
turtle.place()
turtle.turnRight()
turtle.forward()
turtle.turnLeft()
x = x+1
end

And also remember that most (if not all) turtle functions attempt to do the action and will return true on success, false if not…

So you can always do something like this…


if turtle.place() then
	// Continue on...
else
	error("Oh agony I've botched something and can't place a block")  // Or make the turtle spin in place or something visual so you know there was a problem... ghetto debuging, I know...
end

Edit: Was trying to font style my code :|
Edited on 23 March 2012 - 08:15 PM
Rolibar #3
Posted 23 March 2012 - 09:30 PM
Your problem is that at the end of the loop it places block #2, the goes back to the top of the loop, selects block #1 and tries to place it and fails…

It never moved between block #2 being place and attempting to place block #1

Try


local x = 0 // I don't cluttering global space
while x < 5 do

turtle.select(1)
turtle.place()
turtle.turnRight()
turtle.forward()
turtle.turnLeft()
turtle.select(2)
turtle.place()
turtle.turnRight()
turtle.forward()
turtle.turnLeft()
x = x+1
end

And also remember that most (if not all) turtle functions attempt to do the action and will return true on success, false if not…

So you can always do something like this…


if turtle.place() then
	// Continue on...
else
	error("Oh agony I've botched something and can't place a block")  // Or make the turtle spin in place or something visual so you know there was a problem... ghetto debuging, I know...
end

Edit: Was trying to font style my code :|

Well I feel like a hurpaderp. No wonder it wasn't placing the first type, like you said, it was trying to place it in an existing block.

Yeah, I see exactly what I did wrong there. Thanks for helping me out!