Turtle Script - Anyone see my mistake?

  • Please make sure you are posting in the correct place. Server ads go here and modpack bugs go here
  • The FTB Forum is now read-only, and is here as an archive. To participate in our community discussions, please join our Discord! https://ftb.team/discord

ksbd

New Member
Jul 29, 2019
46
0
0
I've been attempting a code to chop a 4x4 extrabiomes fir tree in order to get saplings for biofuel. I threw this little routine together to test before writing more code:
Code:
-- VARIABLES!
 
local sapling
local bonemeal
local wood
local fuel
 
-- FUNCTIONS!
 
function refresh()
    sapling = turtle.getItemCount(1)
    bonemeal = turtle.getItemCount(2)
    wood = turtle.getItemCount(3)
    fuel = turtle.getItemCount(16)
    if (sapling < 5) or (bonemeal < 2) or (wood < 2) or (fuel < 21) then
        print("Refresh placeholder")
    end
end
 
function sapling()
    print("Placing Saplings...") print("")
    refresh()
    turtle.select(1)
    turtle.place()
    turtle.turnLeft()
    turtle.forward()
    turtle.turnRight()
    turtle.place()
    turtle.turnRight()
    turtle.place()
    turtle.turnRight()
    turtle.forward()
    turtle.turnLeft()
    turtle.turnLeft()
    turtle.place()
    print("Saplings are placed!")
end
 
function bonemeal()
    print("Growing the tree")
    refresh()
    turtle.select(2)
    turtle.place()
end
 
function cutUp()
    print("Going up!")
    refresh()
    turtle.select(3)
    while turtle.compare() do
        turtle.dig()
        turtle.digUp()
        turtle.up()
    end
    print("Finished!")
end
 
function cutDown()
    print("Going down!")
    refresh()
    turtle.select(3)
    while turtle.compare() do
        turtle.dig()
        turtle.digDown()
        turtle.down()
    end
    print("Finished!")
end
 
function refuel()
    print("Refueling!")
    if turtle.getFuelLevel() < 300 then
        turtle.select(16)
        for i = 1 , 64 do
            turtle.refuel()
        end
    end
    print("Finished!")
end
 
-- MAIN PROGRAM!
 
refuel()
sapling()
bonemeal()
cutUp()
turtle.turnRight()
turtle.forward()
turtle.turnLeft()
cutDown()

I get "Biogen:86: attempt to call number" line 86 is under --MAIN PROGRAM! on the bonemeal() function. It places the saplings and prints that it's done so, then returns the error. However, if I run the bonemeal() function on its own, I get no error. Same goes for refuel() and saplings(). I can't figure out what's wrong... I imagine it's something incredibly simple I've overlooked, if anyone could point it out, I'd appreciate it.
 

TheLoneWolfling

New Member
Jul 29, 2019
260
-6
0
In lua, there is no distinction between functions and variables - functions are simply variables that point to a variable of type function.

When you call the refresh function, you redefine bonemeal as
Code:
bonemeal = turtle.getItemCount(2)
See the issue? When you call bonemeal after this statement executes, you are attempting to call a number.

Either you need to define bonemeal as a local within the refresh function (add a local to the above quoted statement), or you need to rename the function bonemeal.

Note that you would have the same error with sapling() if you called sapling after calling refresh.
 
  • Like
Reactions: ksbd and Skirty_007

ksbd

New Member
Jul 29, 2019
46
0
0
And the simple answer was found. I knew it'd be something stupid like that.
Thank you very much.
 

slay_mithos

New Member
Jul 29, 2019
1,288
0
0
The good thing about that is that you can store a function in a variable, and just call the variable with the arguments of that function later on.

It is really useful in some specific cases, like when you do something that needs to turn, but the direction is based on math that only needs to be done once in a while, and not every time.

But if you don't have a clue of what I am talking about, then yes, stick with strict naming so that this doesn't happen.
Like calling your function "useBonemeal", or "growTree".
The guideline is to try to use actions/verbs in function names (refuel, plantSapling...).

It is an error that most of us did at some point, but most languages will warn you way earlier, because of type mismatches.
 

TheLoneWolfling

New Member
Jul 29, 2019
260
-6
0
The good thing about that is that you can store a function in a variable, and just call the variable with the arguments of that function later on.

It is really useful in some specific cases, like when you do something that needs to turn, but the direction is based on math that only needs to be done once in a while, and not every time.

But if you don't have a clue of what I am talking about, then yes, stick with strict naming so that this doesn't happen.
Like calling your function "useBonemeal", or "growTree".
The guideline is to try to use actions/verbs in function names (refuel, plantSapling...).

It is an error that most of us did at some point, but most languages will warn you way earlier, because of type mismatches.
I use it extensively - it allows you to wrap functions easily. For instance in my EnderMine program I have a function that takes three functions, and returns a function that calls the first function, then the second, then the third. I use it to wrap the turtle.move functions to automatically refuel if necessary, and to wrap the turtle.dig functions so that they will unload if necessary, among other things.
 

slay_mithos

New Member
Jul 29, 2019
1,288
0
0
Read the second post, it just tells you what is wrong...

Basically, just rename either the function or the variable (and their uses), and all will be fine.
 

ThemsAllTook

New Member
Jul 29, 2019
386
0
0
I've been attempting a code to chop a 4x4 extrabiomes fir tree in order to get saplings for biofuel.

If I'm remembering correctly, fir saplings can't actually be used in a fermenter. Sure would be nice, since they produce so many, but I think you have to use either oak, spruce, birch, jungle, or rubber. Hope that doesn't mess up your plans too much!
 

slay_mithos

New Member
Jul 29, 2019
1,288
0
0
Well, he can still produce plant balls and use them to create the biomass.
Not as efficient, but as you get tons of saplings, from a single tree, you could rapidly have chests full of plant balls.
 

Quesenek

New Member
Jul 29, 2019
396
0
0
Well, he can still produce plant balls and use them to create the biomass.
Not as efficient, but as you get tons of saplings, from a single tree, you could rapidly have chests full of plant balls.
Probably the best Idea and easiest method since nothing including the trees that forestry adds can keep up with the amount of saplings that the fir trees give you.
 

slay_mithos

New Member
Jul 29, 2019
1,288
0
0
Well, you still need to cut them down at a reasonable rate...

Maybe 2 turtles could do the trick , but only one is not fast enough.
 

ksbd

New Member
Jul 29, 2019
46
0
0
Okay, so first off, I think support for fir saplings in the fermenter has actually been added by extrabiomes, though it doesn't seem to work just yet (They have it set as an issue on their page). Until then, plantballs will probably be the route I take.
Secondly, my base is a huge floating island with a main powerhub and a hemisphere glass domed garden. The plan was to create an auto-startup script that cut down the trees (4 main turtles cutting down firs) in order to supply the 'biocenter' of my base.
Finally, I just corrected the code by renaming the functions :)

The issue now is that I've got almost all the main functions written and working, I just have to try and cover all the possible locations the turtle could be cut off at when I exit minecraft.
I was going to do something like use a gps to get the y variable, if y is the original start height (That I put in the code) then do this, if it's > than, do this and if it's < than, do this. But don't break anything in-case you ruin the setup.
So.Much.Effort.
 

TheLoneWolfling

New Member
Jul 29, 2019
260
-6
0
The issue now is that I've got almost all the main functions written and working, I just have to try and cover all the possible locations the turtle could be cut off at when I exit minecraft.
I was going to do something like use a gps to get the y variable, if y is the original start height (That I put in the code) then do this, if it's > than, do this and if it's < than, do this. But don't break anything in-case you ruin the setup.
So.Much.Effort.
Here's what you do.

Before each thing that takes time (moving / chopping / turning/ etc.), save the current state (xoffset, yoffset, zoffset, dir, etc.) to a file. In the startup script, load said state from said file. Much easier!
 

ksbd

New Member
Jul 29, 2019
46
0
0
I didn't know you could do this... Is there a tutorial on the cc wiki, or on yt or somewhere (I'll give it a search myself) or would you be able to basically sum up how to do it by any chance? I can't actually believe this is possible, the amount of time I've taken writing programs that try to locate themselves each startup...
 

abculatter_2

New Member
Jul 29, 2019
599
0
0
Here's what you do.

Before each thing that takes time (moving / chopping / turning/ etc.), save the current state (xoffset, yoffset, zoffset, dir, etc.) to a file. In the startup script, load said state from said file. Much easier!

Do you know if such files are ever deleted or modified in any way? I've been kinda hesitant to use this, since it'd be more reliable to have it check nearby conditions, so the entire map would have to be reset for the program to fail, rather then just a file modification/deletion.
 

Abdiel

New Member
Jul 29, 2019
1,062
0
0
Files will never be deleted from a computer/turtle, unless it is destroyed. If you give it a label using the "label set <xyz>" shell command, files will even stay through destruction.

If you want my opinion, you're over-complicating things for a simple woodcutter. There are two positions in which the turtle can be: waiting for a sapling to grow, or somewhere in the middle of cutting down a tree. Upon startup, use turtle.detectDown() to check if you're on solid ground. If yes, you're waiting for a sapling to grow. If not, you were cutting down a tree: break blocks upwards as long as you find wood, then descend as far down as possible.
 

abculatter_2

New Member
Jul 29, 2019
599
0
0
Currently, I'm using the label of a disc within an adjacent disk drive to record necessary persistent info. And yeah, I knew about labeling the computer, though I'm still hesitant about it... Just paranoid from the lack of CC persistence, I suppose.

Also, you're right about it not being strictly necessary to have something so complicated. However, there are instances where that might not work; What if the turtle was in the middle of turning around? Additionally, consider that fir wood and redwood cannot be turtle.compare()'d, only detected. I don't remember if the leaves or saplings can be compared, though...
 

BanzaiBlitz

New Member
Jul 29, 2019
429
0
0
Hmm....do turtles read a redstone signal?

Could setup single turtles with identical scripts you just punch a button for and a red alloy wire lights em up on your call. You won't logout with them mid-process because you'll be aware and not overload your base with materials if you aren't ready.

Of course, you could just use Steve's Carts and not have all the headaches. Turtles are cooler though. I need to mess with them soon. :)
 

vScourge

New Member
Jul 29, 2019
71
0
0
Yes, turtles can read/send restone signals. With MiscPeripherals they can also read/send wireless redstone signals.