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

Im doing something wrong and I dont know what

Started by dr_bleblo, 08 August 2015 - 06:44 PM
dr_bleblo #1
Posted 08 August 2015 - 08:44 PM
So I'm working on a new turtle mining program thats works verticly rather than horizontally to be faster but I can't find a way to make it gravel proof while mining directly upwards unless I hardcore the number of times I want it to go up wich is completely unacceptable here's all the code that applies to the problem (too much to show all of it)
—-Skip some stuff—-
print("What is my current Y level?")
local yLevel = read()
—-Skip some stuff—-
local function mineUp(num)
local var = 1
while var~=num do
if turtle.detect()==true then
turtle.digup()
else
var = var+1
turtle.up()
os.sleep(0.3)
end
end
end
—Skip some stuff—
mineUp(yLevel)

there is nearly 100 lines of code that I've all ready written so obviously some stuff is missing particularly the stuff around mineUp(yLevel) almost everything is essential but I can tweak the program easily enough to fit any issues you pros find (within reason) when I hardcode num to 4/what ever other number I wanna test with it works fine but when I have it like this the turtle just shoots off into space and I can't figure out why becuase for all intensive purposes num/yLevel IS 4(number I've been testing with) right? is there anyway to fix this? any way at all?
Communeguy #2
Posted 08 August 2015 - 09:41 PM
It's shooting off into space because, as I understand it, there's no upper bound in this code- it's going to move or dig up by the number of times you put in as the yLevel, less 1.

My interpretation anyway. Your mileage may vary, because I don't mess with turtles much.

Maybe someone can tell me I'm wrong, but that's what the compiler in my head is trying to do with this.

ETA": If you post the full code to pastebin I can try plugging it into a turtle and see if I can find the bad line. In spite of how often I have to come for help on this board, I can at least usually find the broken thing, even if I can't actually fix it.

ETA2: Yeah, tonumber() is probably the ticket. So derp. I don't usually deal with waiting for user input much, either.
Edited on 08 August 2015 - 07:54 PM
Dog #3
Posted 08 August 2015 - 09:50 PM
I noticed one thing…

read() returns a string even if you type in a number, so you'd want to convert your yLevel to a number by doing this…

local yLevel = tonumber(read())

For future reference, your code will be easier to read if you it put between [CODE] tags
Edited on 08 August 2015 - 07:52 PM
Link149 #4
Posted 08 August 2015 - 10:07 PM
As Dog just mentionned, read() returns a string.
In other words, instead of returning the numeric value 4, read() will return "4" and store it in yLevel,
causing your mineUp() function to test whether 4 is equal to "4", which is always false.

A solution here would be to use the function tonumber() to convert "4" into 4.
I don't really like this solution, as read() will actually accept any text input, not only numbers. A user
could then enter "Some random text", causing the tonumber() function to return nil.

To fix this problem, you could do something like this:


local yLevel

--# Ask the question over and over until the user enters a valid number
while type(yLevel) ~= "number" do
	print("What is my current Y level?")
	yLevel = tonumber(read())
end
Edited on 08 August 2015 - 08:08 PM
guesswhat123212 #5
Posted 17 August 2015 - 08:43 PM
what I do to solve all my issues with moving and possible blockage is something like below simple version.


function up()
moved = false
moved = turtle.up()
while not moved do
turtle.digUp()
moved = turtle.up()
end
end

this way it trys to move up and if it fails it will try to dig and then move over and over again. also if there is nothing in its way it does not waste time trying to dig. I use code like that for forward and down as well. back is a bit different (I always try to avoid back)

edit: forgot to include an end.
Edited on 17 August 2015 - 06:44 PM
H4X0RZ #6
Posted 17 August 2015 - 08:50 PM
what I do to solve all my issues with moving and possible blockage is something like below simple version.


function up()
moved = false
moved = turtle.up()
while not moved do
turtle.digUp()
moved = turtle.up()
end
end

this way it trys to move up and if it fails it will try to dig and then move over and over again. also if there is nothing in its way it does not waste time trying to dig. I use code like that for forward and down as well. back is a bit different (I always try to avoid back)

edit: forgot to include an end.

 
function  up() 
  while not turtle.up() do
    turtle.digUp()
  end
end

Shortened your code a bit :)/>
Balthamel #7
Posted 18 August 2015 - 01:13 AM

while not turtle.moveUp() then
   while turtle.detectUp() then
    if not turtle.digUp() then
	  --you have hit something like bedrock or warded
    end
   end
end

This should keep trying to dig the block above until there is no black above and then move up.
It does not allow for a mob standing on the turtle preventing movement.

oops did not see the above post.
Edited on 17 August 2015 - 11:14 PM
H4X0RZ #8
Posted 18 August 2015 - 06:23 AM

while not turtle.moveUp() then
   while turtle.detectUp() then
    if not turtle.digUp() then
	  --you have hit something like bedrock or warded
    end
   end
end

This should keep trying to dig the block above until there is no black above and then move up.
It does not allow for a mob standing on the turtle preventing movement.

oops did not see the above post.
Yours also detects unmineable blocks. 👏 but you should add an .attackUp() so it attacks animals standing on top of it.
Edited on 18 August 2015 - 04:23 AM
guesswhat123212 #9
Posted 18 August 2015 - 05:28 PM
Shortened your code a bit :)/>

that's a very good point, I started using that long ago when I was testing things and never stopped to make it shorter like that.