A turtle home block()
#1
Posted 27 April 2014 - 01:48 PM
My idea was to add a new home block. This block gets put under the turtle. This is home position for the turtle. The home block can be right clicked on and one option. A number, the player adds a number to this home block.
In the lua code. 2 new commands.
if turtle.still() then
turtle.home(#)
end
The turtle.still checks to see if the turlte is moving. This returns true or false. If not. Then the turtle will go to the home starting block using the # in the turtle.home(#) command.
#2
Posted 27 April 2014 - 01:57 PM
#3
Posted 27 April 2014 - 02:27 PM
For example:
while not turtle.forward() do turtle.dig() turtle.attack() end
The above code will make the turtle move once, and the script won't carry on until that action has completed successfully. Every attempt that ends in failure will lead to the turtle swinging whatever weapon/tool it has around before it makes the next one.
It's also worth reading up on the GPS API. This allows a system to determine where in the world it's located - great if you want to code turtles to navigate themselves to a certain location regardless of their starting point.
#5
Posted 27 April 2014 - 02:39 PM
#6
Posted 27 April 2014 - 06:22 PM
apemanzilla, on 27 April 2014 - 01:57 PM, said:
Well I don't mean any thing like that. Just a block that the turtle returns too. I don't have an option to handle errors like this. If that's what you want to call it. This means I will have to build a wall a round where the turtle is just to keep players and animals out.
Bomb Bloke, on 27 April 2014 - 02:27 PM, said:
For example:
while not turtle.forward() do turtle.dig() turtle.attack() end
The above code will make the turtle move once, and the script won't carry on until that action has completed successfully. Every attempt that ends in failure will lead to the turtle swinging whatever weapon/tool it has around before it makes the next one.
It's also worth reading up on the GPS API. This allows a system to determine where in the world it's located - great if you want to code turtles to navigate themselves to a certain location regardless of their starting point.
Yes, That's right. How ever when this happens the turtle just sits there and doesn't nothing. Theres no way to program the turtle to say hey, Your not moving, so do some thing. Or have the turtle react to a problem. You can only program it for very basic stuff. Unless you wanted to sit a round and watch it.
theoriginalbit, on 27 April 2014 - 02:33 PM, said:
Yes I know. That's because it hasn't been added yet.
Bomb Bloke, on 27 April 2014 - 02:39 PM, said:
Yes, That's correct.
if turtle.still() then -- this would check if the turtle is moving. If not. the code below runs.
turtle.home(1) -- this would send the turtle back to block 1.
end
#7
Posted 27 April 2014 - 06:56 PM
iceman11a, on 27 April 2014 - 06:22 PM, said:
You do realize that you don't really need turtle.still because a Turtle is not moving if you are not calling any turtle move functions (turtle.forward, turtle.up, turtle.down). Also, you can make your code react if the Turtle cannot move for quite some time. I think this is one of the main ideas behind ComputerCraft: you have to fight with problems you struggle with using programming.
iceman11a, on 27 April 2014 - 06:22 PM, said:
BombBloke's code does just that: if the Turtle cannot move it tries to dig a block and fight it's way through.
#8
Posted 27 April 2014 - 09:17 PM
MKlegoman357, on 27 April 2014 - 06:56 PM, said:
iceman11a, on 27 April 2014 - 06:22 PM, said:
You do realize that you don't really need turtle.still because a Turtle is not moving if you are not calling any turtle move functions (turtle.forward, turtle.up, turtle.down). Also, you can make your code react if the Turtle cannot move for quite some time. I think this is one of the main ideas behind ComputerCraft: you have to fight with problems you struggle with using programming.
iceman11a, on 27 April 2014 - 06:22 PM, said:
BombBloke's code does just that: if the Turtle cannot move it tries to dig a block and fight it's way through.
I don't think you have the idea. I mean the turtle stops when an player or animal or player block it's path. When this happens the turtle just stops all together. Even when the program is still running. I tested this and that's what happens. The idea is to check this and take some new action. I would have to build a wall a round the area I have the turlte running. and I didn't want to do that. I guess I'll have too.
#9
Posted 27 April 2014 - 09:29 PM
iceman11a, on 27 April 2014 - 09:17 PM, said:
How did you test this then? All turtle move functions return true if the turtle has moved or false if not:
if turtle.forward() then --// if the turtle moved forward
print("Turtle has moved forward successfuly")
else --// if the turtle couldn't move forward
if turtle.detect() then --// if there is a block in front of the turtle
print("A block is blocking the turtle")
else --// there are no blocks in front of the turtle, it means that a player is blocking the way
print("A player or a mob is blocking the turtle")
end
end
Edited by MKlegoman357, 27 April 2014 - 09:47 PM.
#10
Posted 27 April 2014 - 09:45 PM
MKlegoman357, on 27 April 2014 - 09:29 PM, said:
iceman11a, on 27 April 2014 - 09:17 PM, said:
How did you test this then? All turtle move functions return true if the turtle has moved or not:
if turtle.forward() then --// if the turtle moved forward
print("Turtle has moved forward successfuly")
else --// if the turtle couldn't move forward
if turtle.detect() then --// if there is a block in front of the turtle
print("A block is blocking the turtle")
else --// there are no blocks in front of the turtle, it means that a player is blocking the way
print("A player or a mob is blocking the turtle")
end
end
Maybe I've understood you wrong, but if a turtle function fails it will return false, if it succeeds it will return true. It's not always going to be true, for example if you type turtle.forward() this will return true if the turtle actually moves, if it doesn't the function will return false. But from your answer I just read "All turtle move functions return true if the turtle has moved or not"
#12
Posted 28 April 2014 - 12:16 AM
MKlegoman357, on 27 April 2014 - 09:29 PM, said:
iceman11a, on 27 April 2014 - 09:17 PM, said:
How did you test this then? All turtle move functions return true if the turtle has moved or false if not:
if turtle.forward() then --// if the turtle moved forward
print("Turtle has moved forward successfuly")
else --// if the turtle couldn't move forward
if turtle.detect() then --// if there is a block in front of the turtle
print("A block is blocking the turtle")
else --// there are no blocks in front of the turtle, it means that a player is blocking the way
print("A player or a mob is blocking the turtle")
end
end
Just set up a while loop to for forward and block the turtles path. He will stop and not go on. Get the idea. If you setup some thing for the turtle to go forward. Then you have to tell the turtle how far to go. If some thing got in the turtles way. Then the turtle can wait until that person or animal moves out of the way. There's no way to check for this. If you stop the turtle before in completes it program. The turtle stops. You have to play with it to get it to go back to the starting position or destroy it. and start all over.
get the idea.
augustas656, on 27 April 2014 - 09:45 PM, said:
MKlegoman357, on 27 April 2014 - 09:29 PM, said:
iceman11a, on 27 April 2014 - 09:17 PM, said:
How did you test this then? All turtle move functions return true if the turtle has moved or not:
if turtle.forward() then --// if the turtle moved forward
print("Turtle has moved forward successfuly")
else --// if the turtle couldn't move forward
if turtle.detect() then --// if there is a block in front of the turtle
print("A block is blocking the turtle")
else --// there are no blocks in front of the turtle, it means that a player is blocking the way
print("A player or a mob is blocking the turtle")
end
end
Maybe I've understood you wrong, but if a turtle function fails it will return false, if it succeeds it will return true. It's not always going to be true, for example if you type turtle.forward() this will return true if the turtle actually moves, if it doesn't the function will return false. But from your answer I just read "All turtle move functions return true if the turtle has moved or not"
Sorry your not under standing me. If you start a turtle on a route. and then some thing stops it. Then what. Even when the program is still running. The turtle sits and does nothing.
That's my point. The idea is to set up some thing that will tell the turtle to return to its starting point, If some thing like this happen or for any other reason.
As long as every one says out of the way. the turtle runs fine. The problem is that is it gets stopped. nothing happen.
#13
Posted 28 April 2014 - 12:29 AM
#14
Posted 28 April 2014 - 01:01 AM
Bomb Bloke, on 28 April 2014 - 12:29 AM, said:
lol, well the idea is for the tree turtle to do it's job, that's all I wanted to do. I do have a new question for you.
I need to do some thing like this. I just can't get it to work. It tells me that "for" requires a number.
function chek(num)
for i =1, num do
turtle.forward()
end
end
I knwo how to pass values from the funtion to the code. I just can't get it to work the other way a round. Any ideas.
#16
Posted 28 April 2014 - 02:41 AM
local function goForward(moveAmount)
for i=1,moveAmount do
while not turtle.forward() do
turtle.attack()
turtle.dig()
end
end
end
I tried some thing like this and it didn't work. I got an error saying that "for" has to be a number.
function check(num)
for i = 1, num do
--do some thing here
end
end
when I tried to do what I have above. I still got the same error.
#17
Posted 28 April 2014 - 06:35 AM
#18
Posted 28 April 2014 - 11:50 AM
#19
Posted 31 May 2014 - 11:43 PM
while not turtle.forward() do turtle.dig() turtle.attack() end
I am writing a program to excavate a underground area and I was wondering weather that would be an easier way because could I do
turtle.forward() turtle.forward()Enough times with the other loop rather than
turtle.dig() turtle.forward() turtle.dig()Over and over
Edited by sk8mn, 31 May 2014 - 11:45 PM.
#20
Posted 01 June 2014 - 01:36 AM
Generally speaking, if you're typing out the same function calls over and over in your script in a similar pattern, then you're doing it wrong and should be implementing while/for/repeat loops instead.
The code block I provided makes the turtle attempt to go forward, and if that fails, it will attempt to clear the way in front of it and try again as many times as need be. If there's a stack of gravel in front of it, for eg, then those four lines will clear the lot and move forward once that's done.
You'll find most digging scripts use nearly identical constructs, though the addition of fuel checks is common.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











