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

getting functions to work

Started by jamzam90, 29 December 2012 - 08:37 PM
jamzam90 #1
Posted 29 December 2012 - 09:37 PM
When ever I make a function it doesnt work properly it just runs it like its a regular program just run the top function and skips the rest of them for some reason heres the code for what ive been trying to do its suppsoed to cut trees down (extra biomesXL ones)
local p = false
local z = 0
local y = true
local x = 0
function hi()
if turtle.detect() == true then
turtle.dig()
turtle.forward()
else
turtle.turnRight()
z = z+1
end
if z == 3 then
turtle.forward()
turtle.turnRight()
turtle.back()
y = false
end
end

function above()
if turtle.detectUp == true then
p = true
while p == true do
turtle.digUp()
turtle.up()
x = x+1
end
else
for i = 1,x do
turtle.down()
end
end
p = false
end

while y == true do
above()
hi()
end
Grim Reaper #2
Posted 29 December 2012 - 09:48 PM
The reason that it never seems to execute the proper function is because your invoking what is called a 'recursive loop.'

Let me break down what mean in your code:
When the interpreter reaches the following loop:

while y == true do
   above() -- Here is the problematic line.
   hi()
end

The function will 'recur' (restart) and thus never execute the 'hi()' statement after it.

Also, you never close out the function 'above' with an 'end' keyword.
Here is what your code would look like if you closed out the function:


function above()
    if turtle.detectUp() == true then
        p = true
        while p == true do
            turtle.digUp()
            turtle.up()
            x = x+1
        end
        for i = 1,x do
            turtle.down()
        end
    end

    while y == true do
        above()
        hi()
    end
end 
However, the above code doesn't solve your recursion problem. I didn't design your program so I can't exactly suggest a way to fix the bug and provide a program from the current code to do what your asking.

Hope I helped.
jamzam90 #3
Posted 29 December 2012 - 09:55 PM
its not the above() function its the hi() function thats does it skips the above one I added the line i had forgotten to pu t it still skips the hi() function
the hi() function moves it forward and digs into the tree then it should run the above() function and begin digging up then back down
remiX #4
Posted 29 December 2012 - 10:19 PM
Your code had quite a few errors, read my comments


local p = false
local y = true
local z = 0
local x = 0
function hi()
    if turtle.detect() then -- Comparing two values, so use double ==. in this case, it's a boolean so you can just use if turtle.detect() (true) or if not turtle.detect (false)
        turtle.dig()
        turtle.forward()
    else
        turtle.turnRight()
        z = z+1
    end
if z == 3 then
    turtle.forward()
    -- turtle.turnRIght(turtle.back() ... What were you trying to do here?
    end
end

function above()
    if turtle.detectUp() then
        p = true
        while p do
            turtle.digUp()
            turtle.up()
            x = x+1
            end
        for i = 1, x do
            turtle.down()
        end
    end
    while y do
        above()
        hi()
    end
end
ChunLing #5
Posted 30 December 2012 - 12:37 AM
In fact, I believe that there were sufficient serious errors in the posted code to for us to confidently assert that it is not the code that you were running.

Please post the actual code that produced the undesired results. Do not post badly retyped code and expect us to identify which errors in the original were causing your problem.
jamzam90 #6
Posted 30 December 2012 - 04:22 PM
the resone its badly retyped is becouse the edititor kept messing it up on me and i hadnt seen the messed up spot
ChunLing #7
Posted 30 December 2012 - 07:03 PM
If you've spotted and fixed it, then all is well.

If you still need help, find a method of copying the exact code that is more reliable than retyping it. This can be slightly tricky if your code is stored on a server and you don't have local access. But if you are testing your code in a local game, then you can navigate to the saves\saveName\computer\ID\ folder (where saveName is the name of the saved game and ID is the id of the computer where the program is stored) and open the files directly using an editor (many people recommend Notepad++). Then you can cut and paste the code in it's entirety, without having to retype it.

There are several compelling arguments for testing code in a local game. Easy access to code using a better editor than edit is one of them (there's also the ability to run tests in creative mode and the relative disposability of a local world).
jamzam90 #8
Posted 02 January 2013 - 03:46 PM
i put it in from notepad++
ChunLing #9
Posted 02 January 2013 - 04:33 PM
Um…put it in where?
jamzam90 #10
Posted 03 January 2013 - 01:22 PM
it in the first post just copy pasted from notepad++
ChunLing #11
Posted 04 January 2013 - 04:42 AM
Okay, what is the current problem? It looks like this has an undesired behavior where it shoots off into the sky and never comes back down.