68 posts
Location
USA
Posted 27 April 2014 - 02:48 PM
If when it happens, A player or a animal get in the way of the turtle. It stops. Even if the program is still running. The turtle doesn't do any thing. This is from testing I been doing and this is the result.
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.
1610 posts
Posted 27 April 2014 - 02:57 PM
A teleportation block seems overpowered to me. Additionally, any of the turtle's moving functions will return false if they cannot move for any reason - including a player being in the way.
7083 posts
Location
Tasmania (AU)
Posted 27 April 2014 - 03:27 PM
Commands such as turtle.forward() return information about whether or not they were successful (a boolean set to either true or false, in this case). It is hence possible to
know when the turtle has failed to go forward (eg due to a player or animal), and program it to react accordingly.
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.
7508 posts
Location
Australia
Posted 27 April 2014 - 03:33 PM
turtle.home(#)
this is invalid Lua syntax…
7083 posts
Location
Tasmania (AU)
Posted 27 April 2014 - 03:39 PM
Hash in this instance is being used as a "insert number here" symbol. As in, you'd type the ID of the home block you wanted the turtle to go to in place of the #.
68 posts
Location
USA
Posted 27 April 2014 - 07:22 PM
A teleportation block seems overpowered to me. Additionally, any of the turtle's moving functions will return false if they cannot move for any reason - including a player being in the way.
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.
Commands such as turtle.forward() return information about whether or not they were successful (a boolean set to either true or false, in this case). It is hence possible to
know when the turtle has failed to go forward (eg due to a player or animal), and program it to react accordingly.
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.
turtle.home(#)
this is invalid Lua syntax…
Yes I know. That's because it hasn't been added yet.
Hash in this instance is being used as a "insert number here" symbol. As in, you'd type the ID of the home block you wanted the turtle to go to in place of the #.
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
1140 posts
Location
Kaunas, Lithuania
Posted 27 April 2014 - 07:56 PM
How ever when this happens the turtle just sits there and doesn't nothing.
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.
Theres no way to program the turtle to say hey, Your not moving, so do some thing.
BombBloke's code does just that: if the Turtle cannot move it tries to dig a block and fight it's way through.
68 posts
Location
USA
Posted 27 April 2014 - 10:17 PM
How ever when this happens the turtle just sits there and doesn't nothing.
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.
Theres no way to program the turtle to say hey, Your not moving, so do some thing.
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.
1140 posts
Location
Kaunas, Lithuania
Posted 27 April 2014 - 10:29 PM
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.
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 on 27 April 2014 - 08:47 PM
157 posts
Posted 27 April 2014 - 10:45 PM
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.
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"
1140 posts
Location
Kaunas, Lithuania
Posted 27 April 2014 - 10:50 PM
-snip-
This is what happens when I write long posts with my phone… fixed now.
68 posts
Location
USA
Posted 28 April 2014 - 01:16 AM
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.
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.
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.
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.
7083 posts
Location
Tasmania (AU)
Posted 28 April 2014 - 01:29 AM
Hey, you know what might be fun? Try putting, say, a fueled melee turtle down, standing in front of it, and running
that code I gave you earlier. See whether or not the turtle somehow manages to push past you. You might be surprised.
68 posts
Location
USA
Posted 28 April 2014 - 02:01 AM
Hey, you know what might be fun? Try putting, say, a fueled melee turtle down, standing in front of it, and running
that code I gave you earlier. See whether or not the turtle somehow manages to push past you. You might be surprised.
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.
7083 posts
Location
Tasmania (AU)
Posted 28 April 2014 - 02:05 AM
That's fine, assuming you're calling that function in a manner such as "chek(4)".
Eg, like
this, or
this.
68 posts
Location
USA
Posted 28 April 2014 - 03:41 AM
I am, See the sampel below
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.
8543 posts
Posted 28 April 2014 - 07:35 AM
We'd have to see the rest of the code to tell you for sure what's wrong. I see you have a topic over in Ask a Pro. It would be very helpful for those trying to help you if you were to post the rest of the code.
68 posts
Location
USA
Posted 28 April 2014 - 12:50 PM
I did, The rest of the code is there. Just look at the top of the post. I know it's there. I remeber posting it.
26 posts
Posted 01 June 2014 - 12:43 AM
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 on 31 May 2014 - 10:45 PM
7083 posts
Location
Tasmania (AU)
Posted 01 June 2014 - 02:36 AM
What "other loop"?
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.
26 posts
Posted 01 June 2014 - 02:47 AM
What "other loop"?
I didn't mean to write other, I meant your line of code
I am setting up a program with a turtle that will make a room 11x3x2 and I was originally going to write turtle.dig() over and over again but now with you line could I put that at the top and I will work for the whole script? As for the moment I don't want completely new code as this will be the first program I will have coded by myself without aid from a tutorial but that code block if it works how I think it does it will cut my coding time in half