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

Too Long Without Yielding

Started by AlwayzPatrick, 11 April 2015 - 07:44 AM
AlwayzPatrick #1
Posted 11 April 2015 - 09:44 AM
Hi i have build a computercraft program and i used it for Greg's SG
but if i use it 5 seconds later it says this: Too Long Without Yielding
what must i do

-CODE-

SG = peripheral.find("stargate")
mon = peripheral.wrap("top")

–SG + MON–
St, C = SG.stargateState()
LA = SG.localAddress()
RA = SG.remoteAddress()

function INF()
while true do
mon.setBackgroundColor(colors.white)
mon.setTextColor(colors.red)
mon.setCursorPos(1,1)
mon.write("The Stargate = "..St)
mon.setCursorPos(1,2)
mon.write("The Local Addres = "..LA)
mon.setCursorPos(1,3)
mon.write("Locked Chevrons = "..C)
mon.setCursorPos(1,4)
mon.write("Dialling To = "..RA)
end
end
INF()
Creator #2
Posted 11 April 2015 - 06:13 PM
add sleep(0) in the while true do loop. ;)/>
Dog #3
Posted 11 April 2015 - 06:20 PM
The way you've structured your code, I have a feeling that it isn't going to work the way you want it to.

Currently, you set your variables, then you just print them to the screen over and over again, but you never set them to anything else since they are outside the loop - some of them (like remoteAddress) will change over time so you want to update them regularly. Also, you should localize your variables (and, by extension, your functions) unless you need or want them to be accessible to other programs on the same computer - locals are also slightly faster which is always good. The following will ensure you get 'up to date' information in your loop.

local SG = peripheral.find("stargate")
local mon = peripheral.wrap("top")
local LA = SG.localAddress()

local function INF()
  while true do
    local St, C = SG.stargateState()
    mon.setBackgroundColor(colors.white)
    mon.setTextColor(colors.red)
    mon.setCursorPos(1,1)
    mon.write("The Stargate = "..St)
    mon.setCursorPos(1,2)
    mon.write("The Local Addres = "..LA)
    mon.setCursorPos(1,3)
    mon.write("Locked Chevrons = "..C)
    mon.setCursorPos(1,4)
    mon.write("Dialling To = "..SG.remoteAddress())
    sleep(0)
  end
end

INF()
Edited on 11 April 2015 - 04:29 PM