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

functions with until?

Started by Cheesety210, 01 April 2013 - 02:59 AM
Cheesety210 #1
Posted 01 April 2013 - 04:59 AM
I'm getting an error on line 3about an end expected. Here is my code

function mineup()
	    until turtle.detectUp() == false then
			    turtle.digUp()
			    turtle.up()
	    else
			    until turtle.detectDown() == true then
			    turtle.down()
			    end
	    end
end
function ddf()
	    if turtle.detect() == true then
	    turtle.dig()
	    turtle.forward()
	    else
	    turtle.forward()
	    end
end

function checkfuel()
	    if turtle.getFuelLevel <= 10 then
	    turtle.select(1)
	    turtle.refuel()
	    end
end
print("Hello. Welcome to the Cheesety210 Tree cutting program")
print("Please put your fuel in the first slot.")
turtle.turnRight()
turtle.turnRight()
turtle.turnRight()
turtle.turnRight()
clear
print("I, your turtle, should be placed at the bottom of a tree")
print("right next to the tree. A diagram is below.")
print(" [ ] = A log, and T is the turtle")
print("[ ] [ ] ")
print("[ ] [ ] ")
print(" T	  ")
print(" What is the width/length of the tree? In the example above it's 2.")
local length = read()
if length == 1 then
	    checkfuel()
	    ddf()
	    mineup()
end
if length == 2 then
	    checkfuel()
	    ddf()
	    mineup()
	    checkfuel()
	    ddf()
	    mineup()
	    checkfuel()
	    turtle.turnRight()
	    ddf()
	    mineup()
	    checkfuel()
	    turtle.turnRight()
	    ddf()
	    mineup()
	    checkfuel()
	    turtle.forward()
end

if length == 3 then
	    ddf()
	    mineup()
	    checkfuel()
	    ddf()
	    mineup()
	    checkfuel()
	    ddf()
	    mineup()
	    checkfuel()
	    turtle.turnRight()
	    ddf()
	    mineup()
	    checkfuel()
	    turtle.turnRight()
	    ddf()
	    mineup()
	    checkfuel()
	    ddf()
	    mineup()
	    checkfuel()
	    turtle.turnLeft()
	    ddf()
	    mineup()
	    checkfuel()
	    turtle.turnLeft()
	    ddf()
	    mineup()
	    checkfuel()
	    ddf()
	    mineup()
end
print("I am done with this tree!")
I'm not exactly sure how to use until's in a function.
SadKingBilly #2
Posted 01 April 2013 - 05:38 AM
Not this:

until turtle.detectUp() == false then
	turtle.digUp()
	turtle.up()
else

But this:

repeat
	turtle.digUp()
	turtle.up()
until turtle.detectUp() == false

All you had to do was search Google for "Lua repeat": magic.
Cheesety210 #3
Posted 01 April 2013 - 05:43 AM
Not this:

until turtle.detectUp() == false then
	turtle.digUp()
	turtle.up()
else

But this:

repeat
	turtle.digUp()
	turtle.up()
until turtle.detectUp() == false

All you had to do was search Google for "Lua repeat": magic.
Oh, I googled "Lua Until". Thanks.
Cheesety210 #4
Posted 01 April 2013 - 05:53 AM

repeat
	turtle.digUp()
	turtle.up()
until turtle.detectUp() == false

Could I do:

repeat
    turtle.digUp()
    turtle.up()
until turtle.detectUp() == false then
repeat
turtle.down()
until turtle.detectDown == true
sjele #5
Posted 01 April 2013 - 06:00 AM
The syntax for a repeat loop is this:


repeat
    --Your code that will be repeated
until (condition that makes it stop)

An exsample of this:

local x = 1
repeat
    x = x + 1
    print(x)
until x == 10
Cheesety210 #6
Posted 01 April 2013 - 06:08 AM
The syntax for a repeat loop is this:


repeat
	--Your code that will be repeated
until (condition that makes it stop)

An exsample of this:

local x = 1
repeat
	x = x + 1
	print(x)
until x == 10

So my code would be:

function mineup()
repeat
turtle.digUp
turtle.up
until turtle.detectUp() == false
repeat
turtle.down()
until turtle.detectDown() == true
end
MudkipTheEpic #7
Posted 01 April 2013 - 06:10 AM
The syntax for a repeat loop is this:


repeat
	--Your code that will be repeated
until (condition that makes it stop)

An exsample of this:

local x = 1
repeat
	x = x + 1
	print(x)
until x == 10

So my code would be:

function mineup()
repeat
turtle.digUp
turtle.up
until turtle.detectUp() == false
repeat
turtle.down()
until turtle.detectDown() == true
end

More like:


function mineup()
repeat
turtle.digUp()
turtle.up()
until turtle.detectUp() == false
repeat
turtle.down()
until turtle.detectDown() == true
end
sjele #8
Posted 01 April 2013 - 06:11 AM
You forgot () after all your turtle.(something) calls.


function mineup()
  repeat
	turtle.digUp()
	turtle.up()
  until turtle.detectUp() == false
  repeat
	 turtle.down()
  until turtle.detectDown() == true
end

This should work

[edit]Ninja'ed[/edit]