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

program not functioning as intended

Started by Kyiki, 13 July 2015 - 04:06 AM
Kyiki #1
Posted 13 July 2015 - 06:06 AM
I wrote a small program to try to get my turtle to dig a square out, using variables (though i've since removed the variable from the body of the code, cause i couldn't get it to work) for a 19x11 square. The program is supposed to loop dig() 19 times, then activate turnAboutR or L based on the variable x being even or odd, using x as the width as well. However, while it's activating the dig() function, it's not completing it before moving on. I've marked it's first 17 movements after activating the program. I haven't checked to see if it's endless or if it'll stop on it's own;
  1. dig forward
  2. dig up
  3. dig forward
  4. dig up
  5. turn right
  6. turn right
  7. move forward
  8. turn right
  9. move forward
  10. turn right
  11. dig forward
  12. dig up
  13. move forward
  14. dig up
  15. dig forward
  16. move forward
  17. dig up


  1. local args = { }
  2. vari = tonumber(args[1])
  3. x = 11
  4. currentSlot = turtle.getSelectedSlot()
  5. local function refuel(currentSlot)
  6. if turtle.getFuelLevel() < 13 then
  7. turtle.select(16)
  8. turtle.refuel(1)
  9. turtle.select(currentSlot)
  10. print( "refueled" )
  11. return currentSlot
  12. end
  13. end
  14. function turnAboutL()
  15. turtle.forward()
  16. turtle.turnLeft()
  17. turtle.forward()
  18. turtle.turnLeft()
  19. return true
  20. end
  21. function turnAboutR()
  22. turtle.forward()
  23. turtle.turnRight()
  24. turtle.forward()
  25. turtle.turnRight()
  26. return true
  27. end
  28. function dig(vari)
  29. refuel()
  30. for a=1,vari,1 do
  31. turtle.dig()
  32. turtle.digUp()
  33. turtle.forward()
  34. turtle.digUp()
  35. return true
  36. end
  37. end
  38. while x > 0 do
  39. dig()
  40. sleep(1)
  41. print("dig function ended")
  42. if dig() then
  43. if (x % 2 == 0) then
  44. turnAboutL()
  45. print("Left turn complete")
  46. else
  47. turnAboutR()
  48. print("Right turn complete")
  49. end
  50. end
  51. if turnAboutR() or turnAboutL() then
  52. x = x - 1
  53. end
  54. end
Edited on 13 July 2015 - 05:26 AM
Bomb Bloke #2
Posted 13 July 2015 - 07:48 AM
When you do:

if dig() then

… it has to execute the dig() function to figure out if it's true or not. That involves attempting to move the turtle forward by "vari" amount of blocks.

So if you do:

dig()
sleep(1)
print("dig function ended")

if dig() then

… then you're calling dig() twice, and therefore asking the turtle to move forward by "vari" * 2 amount of blocks.

Likewise, this line:

if turnAboutR() or turnAboutL() then

… results in the turtle attempting to turn right, and then immediately attempting to turn left.
Lyqyd #3
Posted 13 July 2015 - 08:00 AM
The turnAboutR function explicitly returns true, so the function after the or will not execute.
Bomb Bloke #4
Posted 13 July 2015 - 08:01 AM
Hah! That's a good point. That line's only half broken. ;)/>
Kyiki #5
Posted 13 July 2015 - 12:40 PM
So does the if then activate it's condition? Do I need to put
If dig() == true then
instead? The line that has x = x - 1 should only get one of turnaboutR or turnAboutL, because only one should be activated per completion of dig(). At least that was my intention. How would I go about correcting it? Thanks for all the advice, it's been greatly appreciated!
TheOddByte #6
Posted 13 July 2015 - 01:51 PM
- snip -
Well, that line does the same thing as this

if dig() then

Like this example

if dig() == false then
would be the same as

if not dig() then

And I'm wondering, why're you using a loop here? Or rather, why're you returning true in the loop?

local function dig(vari)
    refuel()
    for a=1,vari,1 do
        turtle.dig()
        turtle.digUp()
        turtle.forward()
        turtle.digUp()
        return true --# This exits the loop, thus it only loops one time
    end
end
This would only loop one time and return true immediately, so you'd have to return true outside of the loop in order for this function to work properly.
Kyiki #7
Posted 13 July 2015 - 02:36 PM
Oh wow, yeah didn't mean for that to return true in the loop, that makes sense now. What the turtle was doing also does, since it's doing one loop of dig() and then turning. Thanks a lot!