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

Too long without wielding CC 1.7

Started by graywolf69, 15 June 2015 - 01:09 AM
graywolf69 #1
Posted 15 June 2015 - 03:09 AM
I'm getting a too long without yielding error, no idea how to fix it (I do know that I'm not doing it right, but haven't done CC in a while). Here's the program, I know I'm supposed to pastebin but it is too short to bother.

cap = peripheral.wrap("back")

mon = peripheral.wrap("top")

maxE = cap.getMaxEnergyStored("west")

curE = cap.getEnergyStored("west")

mon.clear()
mon.setCursorPos(1,1)
while true do
mon.write(curE.."/"..maxE)
end

Thanks!
Edited on 15 June 2015 - 01:13 AM
apemanzilla #2
Posted 15 June 2015 - 03:14 AM
Simple answer: Add this in the while loop, after writing to the monitor:

sleep(1)
Additionally, you never update the curE and maxE variables. Try this code:

cap = peripheral.wrap("back")
mon = peripheral.wrap("top")
mon.clear()
mon.setCursorPos(1,1)
while true do
  mon.write(cap.getEnergyStored("west").."/"..cap.getMaxEnergyStored("west"))
  sleep(1)
end 
Complicated answer: "Yielding" allows CC to run other computers as well as yours. If you run a program without yielding in some form (sleeping, pulling an event, etc) the program will crash because it can't run other computers if one is hogging the thread.
Edited on 15 June 2015 - 01:16 AM
graywolf69 #3
Posted 15 June 2015 - 04:34 AM
So basically, this will make it update every second instead of constantly, which isn't allowed? Great! Thanks!
DarkEyeDragon #4
Posted 15 June 2015 - 12:52 PM
So basically, this will make it update every second instead of constantly, which isn't allowed? Great! Thanks!
Basicly. also if you want to refresh it asap you could use sleep(0.1) in this case it would still refresh very quickly. Altough i don't recommend to use it if you don't need it. It could start slowing down your server/computer quite fast if you have enough of those scripts running heavy tasks.