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

I need help with yielding

Started by EverFrost, 22 January 2014 - 02:23 PM
EverFrost #1
Posted 22 January 2014 - 03:23 PM
some times it says "too long without yeilding"

–Variables
local m = peripheral.wrap("left")
local gatefr = peripheral.wrap("gateReader_7", "gateReader_8")
local gateba = peripheral.wrap("gateReader_6")
local rSide = "bottom"
local stat1
local stat2
x , y = term.getSize()


–Functions
function drAcross(line, char , col)
term.setCursorPos(1 , line)
term.setTextColor(col)
for i = 1, x do
term.write(char)
end
end

function drCenter(line , text , col)
term.setCursorPos((x-string.len(text))/2, line)
term.setTextColor(col)
term.write(text)
end

function text()
m.setTextColor(colors.red)
term.setTextColor(colors.red)
m.setCursorPos(1,1)
term.setCursorPos(5,4)
end

–Code
m.clear()
term.clear()
drAcross(1, "*", 2 )
drCenter(2, "Everland energy system control", 4 )
drAcross(3, "*", 2 )

while(true) do
stat1 = gatefr.get()
stat2 = gateba.get()

if stat1["Full Energy"] == true then
rs.setOutput(rSide, false )
text()
m.write("Enengines OFF ")
term.write("Energy cube is full, engine OFF ")
end

if stat2["No Energy"] == true then
rs.setOutput(rSide, true )
text()
m.write("Engines ON ")
term.write("Energy cube is NOT full, engine ON")
end

sleep(2)
end
Edited on 23 January 2014 - 01:57 AM
Bab #2
Posted 22 January 2014 - 05:54 PM
Try adding some sleep(0)'s to while loop and to the for loop in drAcross
Bomb Bloke #3
Posted 22 January 2014 - 06:05 PM
local gatefr = peripheral.wrap("gateReader_7", "gateReader_8")
Wait, what?

In regards to the yielding, the error is triggered by the script failing to pause at least once every ten seconds. As only one ComputerCraft script is ever processed at a time, a script that takes more then this as a chunk is considered a hog and so the interpreter halts it. Yielding (by eg sleeping) gives a chance for other scripts to do their thing.

I would guess that some times the stat1 = gatefr.get() / stat2 = gateba.get() calls take too long. Are the peripherals located in the same chunk as the computer? Is the whole system chunk loaded? Does the MineCraft server have a really low tick rate?

Try adding some sleep(0)'s to while loop and to the for loop in drAcross
That function only gets called during init, and something else is far wrong if those simple loops are taking more then ten seconds.
Edited on 22 January 2014 - 05:10 PM
surferpup #4
Posted 22 January 2014 - 09:31 PM
Dumb question on my part – why are you wrapping two peripherals in this line?


local gatefr = peripheral.wrap("gateReader_7", "gateReader_8")
Edited on 22 January 2014 - 08:32 PM
EverFrost #5
Posted 23 January 2014 - 11:20 AM
it looks like
Spoiler



And the second front gateReader was in other chunk.

Thanks all.
surferpup #6
Posted 24 January 2014 - 03:58 PM
That looks pretty neat. Does it say where it is going too long without yielding? I am still suspicious of this line:

local gatefr = peripheral.wrap("gateReader_7", "gateReader_8")

Maybe I am wrong, but I would break it up a bit:


local gatefr0 = peripheral.wrap("gateReader_7")
local gatefr1 =  peripheral.wrap("gateReader_8")

Then change these lines:


stat1 = gatefr.get()
stat2 = gateba.get()

if stat1["Full Energy"] == true then

To this:



stat0 = gatefr0.get()
stat1 = gatefr1.get()
stat2 = gateba.get()

if stat0["Full Energy"] == true  and  stat1["Full Energy"]==true then

See what you think.
Edited on 24 January 2014 - 02:59 PM
EverFrost #7
Posted 25 January 2014 - 05:57 AM
See what you think.

I think that I would use only one gateRedar in front, not two
surferpup #8
Posted 25 January 2014 - 11:33 PM
Perhaps I misunderstood. In your code, you have written in two gate readers to the same peripheral.wrap command. Your code reads:


local gatefr = peripheral.wrap("gateReader_7", "gateReader_8")

I am suspicious of this, so I assumed you wanted two gate readers and broke them out to do what your code attempts to do. I am wondering if cleaning up this problem as I described helps with your yielding problem.

Do you still have a yielding problem?