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

Crafty Turtle Locking Up

Started by tomatosoup, 25 August 2014 - 02:05 AM
tomatosoup #1
Posted 25 August 2014 - 04:05 AM
I have a turtle running on a server with the following code:


while true do
for i=1,16 do
if turtle.getItemCount(i) > 0 then
  turtle.craft()
  turtle.drop()
end
for i=1,16 do
if turtle.getItemCount(i) > 20 then
  turtle.select(i)
  turtle.dropup()
end
end
end
end

It was running fine until a few minutes ago and then it suddenly started locking up on either line 1 or 4, is this a server issue? Or is there something wrong with my code?
Bomb Bloke #2
Posted 25 August 2014 - 05:28 AM
It's "turtle.dropUp()", not "turtle.dropup()". Case sensitivity matters.

When a computer/turtle starts running code, ComputerCraft starts a ten second timer. If that code doesn't yield before that timer ends then ComputerCraft crashes that computer. After each yield, processing continues with a new time limit.

The reason why is that running your code chews up valuable server processing power, and so it shouldn't be able to monopolise it. In fact, only ONE CC device can run code at a time: While one is doing something, none of the others can do anything until it yields.

Whether or not it takes more then ten seconds for your code to execute has a lot to do with the power of the Minecraft server it's running on, and how often you allow your code to yield. Pulling events (eg getting typed characters) or sleeping triggers a yield, and many commands (eg turtle movements or getting text input from the user) have to pull events to work anyway.

Your code doesn't perform any actions that yield unless items are constantly injected into the turtle's inventory. If there's a lull in the input for a while, it'll crash.

You could make use of the turtle_inventory event to yield until the inventory receives an item. Just drop os.pullEvent("turtle_inventory") in as the first line within your loop.