Spoiler
local que = os.queueEvent
local coYield = coroutine.yield
local un = unpack
local tostr = tostring
local counter = 0
local function yield()
que("yield event")
local event = {coYield()}
while tostr(event[1]) ~= "yield event" do
que(unpack(event))
event = {coYield()}
end
end
local overwrite = {
"term",
"rednet"
}
for k,v in pairs(overwrite) do
for key, value in pairs(_G[v]) do
if key ~= "receive" then
_G[v][key] = function(...)
counter = counter+1
if counter >= 50 then
yield()
counter = 0
end
return value(...)
end
end
end
end
Essentially what this is doing right now is overwriting all of the functions in the term and rednet api, to where a counter counts anytime any of the functions is used and if the counter is 50, then the function will automatically yield. So far in my tests this works as expected, but I am aware this is a rather hacky solution, and as such might have a flaw in it.
Current problems I have thought of:
- One thing I have already thought of is if the event queue is at the max then an event will be lost through this method, however, very rarely does a computer reach a full event queue.
- If a event is queued while the events are being rotated, then the events will be out of order