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

[Turtle] Going forward till detection of block.

Started by xizors, 21 April 2013 - 12:56 AM
xizors #1
Posted 21 April 2013 - 02:56 AM
Hi! :)/>

Im trying to make my turtle go forward until it detects a block, but I keep getting errors.


    function check()
	  if turtle.detect() then
	  end
    end
	
    else 
	  turtle.forward()
	  sleep(0.5)
	  check()
    end

Any idea what is wrong?

Thanks.
Alekso56 #2
Posted 21 April 2013 - 03:02 AM
remove the ELSE and the END since it has no function unless you use an if.

i'll explain what's wrong with the code:

function check() -- you make an function.  That's OK
if turtle.detect() then --  you fail to specify what it should do when it detects a block
end -- end   no more elses after this
end -- end of function

else -- error:  no if present
turtle.forward() -- OK
sleep(0.5) -- OK
check() -- run function
end -- end of what?
the code above will never loop, since you haven't told it to.

i suggest doing this:

while not turtle.detect() do
turtle.forward()
sleep(0.5)
end
theoriginalbit #3
Posted 21 April 2013 - 03:14 AM
in addition to what Alekso56 said I suggest this code:

while not turtle.detect() do
  if not turtle.forward() then
    if turtle.attack() then -- check if a mob is in the way
      while turtle.attack() do end -- attack it until it is gone
    elseif turtle.getFuelLevel() == 0 then -- check if we ran out of fuel
      -- handle it in some way of your choosing
    end
  end
  -- if you wanted to sleep you can put it here, but there is no need for it (more info below)
end

Note that I do not have sleep(0.5), that is because in this case the turtle functions are yielding, so we will not have the program error due to "Too long without yielding"
xizors #4
Posted 21 April 2013 - 03:30 AM
Thanks both of you! :)/>

And I was allso wondering, since I am using this to mine obsidian. How do I control the turtle wireless from a computer?
And am I able to control it without looking into the computer. Of course I have to right click it, but instead of looking into the computer and see what I am writing. I would rather see what my turtle was actually doing instead, while executing the commands.
theoriginalbit #5
Posted 21 April 2013 - 03:35 AM
How do I control the turtle wireless from a computer?
There are actually no pre-made, pre-shipped programs to do this. You would have to download or make one.

And am I able to control it without looking into the computer. Of course I have to right click it, but instead of looking into the computer and see what I am writing. I would rather see what my turtle was actually doing instead, while executing the commands.
Not without using an advanced monitor so it can just be touch screen control.
xizors #6
Posted 21 April 2013 - 05:28 AM
How do I control the turtle wireless from a computer?
There are actually no pre-made, pre-shipped programs to do this. You would have to download or make one.

And am I able to control it without looking into the computer. Of course I have to right click it, but instead of looking into the computer and see what I am writing. I would rather see what my turtle was actually doing instead, while executing the commands.
Not without using an advanced monitor so it can just be touch screen control.

Allright, thanks.

I guess I'll just have to make it easier for myself by making some repetitive scripts :P/>