computercraft, quick question.

  • 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

Bibble

New Member
Jul 29, 2019
1,089
0
0
You can nest loops to your hearts content. I think strange things might start happening if you have more than about 100 levels, but don't quote me on that.

I'd advise leaving the loop wrapping until the end. You can write the code, make sure it works and does what you want then you can loop it and all will be good.
 

Peppe

New Member
Jul 29, 2019
836
0
1
Another option to source 1 book would be to put an redstone engine or aurarchtic gate on your book chest/barrel.

Have the turtle emit a pulse to the engine or the gate and it will send out 1 book. -- you will need to time your pulse to the proper length to get just one. Or you could use a transposer to pull out one item.

Your turtle can emit redstone in any direction, so you could have a pipe + gate that activates pipe wire back to the source chest and pipe a book in over a distance. Or emit to a line of redpower wire/redstone dust back to the source engine/gate. Redpower tube back to a transposer might be the simplest and most reliable method.

Turtles are inventories and pipes/tubes will connect to them.

Redstone API is pretty straight forward:
http://computercraft.info/wiki/Redstone_(API)
Simple example for a 1 second pulse out the back:
redstone.setOutput("back", true)
sleep(1)
redstone.setOutput("back", false)


You could then follow a pattern of:
loop start
if no item in slot 1 request an item from source through pulsing side X​
if have item in slot and greater than 30 try enchant​
if succeed drop item -- you can drop forward, up, or down - or turn to drop in other forward directions​
handle enchant failure (probably drop everything and try again from the start)​
loop end

In this way your script should handle more than just books.
You could put items in a chest you want enchanted ending with a stack of book and it will pull them from the chest one by to enchant.
 

zilvarwolf

New Member
Jul 29, 2019
541
0
0
Haha ya thats kinda the purpose of this Im trying to make the switch to uber enchanted thaumcraft tools and ditch the drill & saw and need for the lap pack but after manually enchanting the stuff and standing there for 30 levels of xp and doing it all over again it got old. Plus I see this as a good way to start learning some computercraft and in turn learn some lua programming.


Maybe I am being to picky trying to have it pull from a chest I mean I can fit a stack of books into a slot and I cant see me using a stack up very fast. Maybe I will just try to get code working that doesnt need to pull from a chest first and then once I figure that out advance to fully automating it.
For mine, I have 2 turtles in a 7-wide tier 5 blaze spawner with some killing turtles and water. It takes several minutes for each of the turtles (takes 2 to cover the whole area, and they overlap the middle) to level to or above 30. I set it up midday Sunday with a half-stack of books in each turtle. When I logged off a few hours later, they still had more than 10 books each.

That could point to inefficiency in my code or setup, or blazes dying to water damage, or just general stupid, but a full stack of books lasts a lot longer than it takes for me to get the materials to MAKE two full stacks of books (not that this is a fair comparison..not with a tier 5 cow spawner right next door....)
 

Jeff Fisher

New Member
Jul 29, 2019
316
0
0
How can I write if m.enchant is equal or greater then 30?

this code below doesnt seem to work for some reason; In not sure it I totally understand this piece of code. I know the first part says While the variable Enchanted equals false then do.... but the next line confuses me.. is the next line supposed to be setting m.enchant(30) as the variable? what actually tells it to execute m.enchant(30)..
Like I said I know nothing about programming but I would of thought you would have to tell it enchanted is the variable. and then have it while enchanted == false do and then m.enchant.(30) after that. When I look at this codes my mind thinks ok its saying echanted is equal to m.enchant(30) and but i dont see where it says what to do after the while enchanted == false.

Code:
while enchanted == false do
enchanted = m.enchant(30)
 

Jeff Fisher

New Member
Jul 29, 2019
316
0
0
will this work instead of what you had there?

Code:
enchanted = m.enchant(30)
while enchanted == false do
m.enchant(30)
end
 

Peppe

New Member
Jul 29, 2019
836
0
1
When coding i always like to reference the APIs.

So for your enchanting turtle go to the enchant section in the misc peripherals thread:
http://www.computercraft.info/forums2/index.php?/topic/4587-cc15mc147-miscperipherals-31/

There is a get levels function mentioned there.
getLevels()

You could then do an if m.getLevels() gt 30 then enchant

I would use that to decide when to try an enchant.

The loop you have will try to do a level 30 enchant forever. It stops when the enchant succeeds. Functionally probably pretty similar, but I personally would prefer to only try when it will succeed.
 

Jeff Fisher

New Member
Jul 29, 2019
316
0
0
Yah I have th
When coding i always like to reference the APIs.

So for your enchanting turtle go to the enchant section in the misc peripherals thread:
http://www.computercraft.info/forums2/index.php?/topic/4587-cc15mc147-miscperipherals-31/

There is a get levels function mentioned there.
getLevels()

I would use that to decide when to try an enchant.

The loop you have will try to do a level 30 enchant forever. It stops when the enchant succeeds. Functionally probably pretty similar, but I personally would prefer to only try when it will succeed.
ya Ive had that page bookmarked actually :) I have the functions that can be used on the xpturtle but Im not sure how to say in lua when m.getLevels is = or gretter then 30 then...

if m.getLevels() == > 30 then .. Will that work?
 

Peppe

New Member
Jul 29, 2019
836
0
1
>=

should work

== is equals

LUA logic operators should find you a good page to bookmark
 

Jeff Fisher

New Member
Jul 29, 2019
316
0
0
will this work instead of what you had there?

Code:
enchanted = m.enchant(30)
while enchanted == false do
m.enchant(30)
end


This works but like you said It will keep trying constantly I would like to have it check ever like 5 seconds instead.
 

Jeff Fisher

New Member
Jul 29, 2019
316
0
0
So far I have this:
Code:
m = peripheral.wrap("right")
m.setAutoCollect(true)
 
enchanted = m.enchant(30)
while enchanted == false do
m.enchant(30)
sleep (10)
end

It seems to work and keeps trying to enchant until it has 30 levels.

Now to put in the code to get the books and dump the books into a chest.

But for now I need to get some sleep I cant think clearly. Thanks for the help I might need some more when I go to work on this again after I wake up lol. I have always wanted to learn a programming language so this is great for me. Looks like Lua will be the first one I learn. Thanks again for the help.
 

zilvarwolf

New Member
Jul 29, 2019
541
0
0
So far I have this:
Code:
m = peripheral.wrap("right")
m.setAutoCollect(true)
 
enchanted = m.enchant(30)
while enchanted == false do
m.enchant(30)
sleep (10)
end

It seems to work and keeps trying to enchant until it has 30 levels.

Now to put in the code to get the books and dump the books into a chest.

But for now I need to get some sleep I cant think clearly. Thanks for the help I might need some more when I go to work on this again after I wake up lol. I have always wanted to learn a programming language so this is great for me. Looks like Lua will be the first one I learn. Thanks again for the help.

That's a pretty elegant way to do it, actually.

Mine is something like (and this is off the top of my head)
Code:
enc = peripheral.wrap("right")
enc.setAutoCollect(true)
 
while true do
  sleep(20)
  if enc.getLevels() >= 30 then
    turtle.select(16)
    if turtle.getItemCount() == 0 then
        print("No more books!  Keep up!")
        break
    end
    turtle.transferTo(1,1)
    turtle.select(1)
    enc.enchant(30)
    if turtle.drop() ~= true then
        print("Chest full or something dummy...keep up!")
        break
    end
  end 
end
 

slay_mithos

New Member
Jul 29, 2019
1,288
0
0
I feel the need to correct a small mistake:

Code:
m = peripheral.wrap("right")
m.setAutoCollect(true)
 
local enchanted = false
while not enchanted do
enchanted = m.enchant(30)
sleep (10)
end

if you don't refresh the "enchanted" variable, it won't ever go out of the while loop, might as well do a while true then.

Contrary to the previous posts that would keep enchanting if the first try (the one outside the while loop) didn't succeed, this one will actually stop after enchanting once, assuming you have something enchant-able in the currently selected slot.

hope it helps

EDIT:
Well, the above post actually contains a fully functional code, so my correction is a bit out off the loop

Just a thing, to put into a chest, the function should be drop(), unless it changed into place(), but I doubt it.