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

Need some help with first program

Started by kyle079, 19 June 2013 - 05:06 PM
kyle079 #1
Posted 19 June 2013 - 07:06 PM
I just wrote my first program and cannot figure out why it is not running correctly. The goal is to have a turtle pull from a chest on its left, enchant the book, then drop it in the chest on the right. Currently it will turn left then end the program. Anyone have any tips?

--Wrap peripherals
local m = peripheral.wrap("right")

--set autocollect xp true
m.setAutoCollect(true)

--set variables
currLevel = m.getLevels()

--functions
--pulls books from chest
function getBooks()
print("Getting Book")
turtle.select(1)
turtle.suck()
  if false then
   print("Need more books!!")
   sleep(15)
   getBooks()
  else
   turtle.drop(turtle.getItemCount(1)-1)
  end
end

--places enchanted books into chest
function storeBooks()
turtle.drop()
  if false then
   print("Chest full!!")
   pause(15)
   storeBooks()
  end
end

--main enchant function
function enchantBook()
   turtle.turnLeft()
   getBooks()
   m.enchant(30)
   turtle.turnRight()
   turtle.turnRight()
   storeBooks()
   turtle.turnLeft()
end

--main loop
while true do
print("Currently Level: "..currLevel.."")
if currLevel >=30 then
  enchantBook()
else
  sleep(10)
end
end

Lyqyd #2
Posted 19 June 2013 - 11:22 PM
Split into new topic.
Bomb Bloke #3
Posted 19 June 2013 - 11:34 PM
function getBooks()
print("Getting Book")
turtle.select(1)
turtle.suck()
  if false then                            -- This checks to see if "false" is "true". It's never true. So, you
   print("Need more books!!")              -- want to use "if turtle.suck() then" and delete the line above.
   sleep(15)
   getBooks()                              -- Do NOT have functions call themselves as a way of looping.
  else
   turtle.drop(turtle.getItemCount(1)-1)
  end
end

You might re-write it as:

function getBooks()
  print("Getting Book")
  turtle.select(1)
  while not turtle.suck() do
    print("Need more books!!")
    sleep(15)
  end
  turtle.drop(turtle.getItemCount(1)-1)
end

The rest of your code has similar issues, but this is hopefully enough to make them clear to you.
Tjakka5 #4
Posted 20 June 2013 - 02:51 AM
The problem might be that the turtle only checks once what levels it has, consider replacing the mainloop with this:


--main loop
while true do
currLevel = m.getLevels()
print("Currently Level: "..currLevel.."")
if currLevel >=30 then
  enchantBook()
else
  sleep(10)
end
end
kyle079 #5
Posted 20 June 2013 - 12:07 PM
Thanks for the tips, I will make some changes to the code and see if I can get it up and running.