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

Train Station 'Too Long Without Yielding'

Started by Nisorin, 22 January 2013 - 09:39 AM
Nisorin #1
Posted 22 January 2013 - 10:39 AM
Got a small train station set up under my house. One computer hooked up to a detector rail (ID13) and a wireless Turtle (ID12) at the end of the Boarding Track(Railcraft). The Turtle works fine, pulling the items from the chest cart and taking them into the house when it gets a message from the wireless computer. Problem is, I keep running into an odd error with the computer.

About seven or eight seconds after running the program on the computer, it comes up with this error: trainHomeServer:7: Too long without yielding. I've been making a few different programs with CC now, though this is my first attempt at a train station, but I've never gotten this error before. I've searched a bit online and have only found things saying that the infinite loop hasn't 'yielded.' or something. Can someone explain what it means by yielding, and help me edit the code to fix the issue?


shell.run("id")
local trainIn = false
rednet.open("left")

while true do
     if rs.getOutput("right", true) then
          if trainIn == false then
               sleep(1)
               rednet.send(12, "ARRIVAL")
               print("ARRIVED")
               trainIn = true
               sleep(2)
     end

          if trainIn == true then
               trainIn = false
               print("DEPARTED")
               sleep(2) 
          end
     end
end
theoriginalbit #2
Posted 22 January 2013 - 10:43 AM
that is because computers need to yield to let other computers run. if a computer hasn't yielded in ~10 seconds then CC terminates the program.

To fix this you must have some way for the loop to always yield, not just in the if conditions… so just add a sleep(0) to the end of the while loop…
Nisorin #3
Posted 22 January 2013 - 10:46 AM
That was a lot simpler than I figured it would be. Thank you.

EDIT: Also used the wrong function on line 6. Should have been getInput. Program's working as intended.
redeye83 #4
Posted 22 January 2013 - 12:46 PM
why are you using a turtle to unload chestcarts?
Unloaders and pipes would be easyer and better as you could hook it upto a sorting system etc.
ChunLing #5
Posted 22 January 2013 - 01:45 PM
Generally, a failure to yield is a problem that needs more than just adding a sleep(0) to your loop. That makes the error go away, it doesn't change the fact that the program is hogging computer resources. In this case, you should add an os.pullEvent("redstone") so that the loop only activates when there is a change in the redstone input. Other minor changes you may want to make:
shell.run("id")
local trainIn = false
rednet.open("left")

while true do
    os.pullEvent("redstone")
    if rs.getOutput("right", true) then
        if trainIn then
            trainIn = false
            print("DEPARTED")
            sleep(2)
        else
            sleep(1)
            rednet.send(12, "ARRIVAL")
            print("ARRIVED")
            trainIn = true
            sleep(2)
        end
    end
end