Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/thoda-dev/du-nested-coroutines. A small script for DU to avoid any CPU load error
Something went wrong. Try again.
3.9 kB · 114 lines
Lua
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115--[[ DU-Nested-Coroutines by Jericho Permit to easier avoid CPU Load Errors by using nested coroutines and adapt cycles on the FPS Source unminified available here: https://github.com/Jericho1060/du-nested-coroutines]]--local DU_Nested_Coroutines = { __index = { coroutines = { update = {}, flush = {} }, functions = { update = {}, flush = {} }, main = { update = coroutine.create(function() end), flush = coroutine.create(function() end), }, update = function(self) local status = coroutine.status(self.main.update) if status == "dead" then self.main.update = coroutine.create(function() self:runUpdate() end) elseif status == "suspended" then assert(coroutine.resume(self.main.update)) end end, flush = function(self) local status = coroutine.status(self.main.flush) if status == "dead" then self.main.flush = coroutine.create(function() self:runFlush() end) elseif status == "suspended" then assert(coroutine.resume(self.main.flush)) end end, runUpdate = function(self) for k,co in pairs(self.coroutines.update) do local status = coroutine.status(co) if status == "dead" then self.coroutines.update[k] = coroutine.create(self.functions.update[k]) elseif status == "suspended" then assert(coroutine.resume(co)) end end end, runFlush = function(self) for k,co in pairs(self.coroutines.flush) do local status = coroutine.status(co) if status == "dead" then self.coroutines.flush[k] = coroutine.create(self.functions.flush[k]) elseif status == "suspended" then assert(coroutine.resume(co)) end end end, onUpdate = function(self, functions) for k,f in pairs(functions) do self.functions.update[k] = f self.coroutines.update[k] = coroutine.create(f) end end, onFlush = function(self, functions) for k,f in pairs(functions) do self.functions.flush[k] = f self.coroutines.flush[k] = coroutine.create(f) end end }}NestCo = {}setmetatable(NestCo, DU_Nested_Coroutines)
--[[ add the functions you want to run as coroutines here you can remove co1 and co2 that are given as example]]local functions = { update = {}, flush = {}}
--Functions to load as coroutines that will be runned in system > onUpdate (based on FPS)functions.update.co1 = function () for i=0, 10 do system.print("coroutine 1 --- update --- "..i) coroutine.yield(self.co1) -- pause the coroutine 1 endendfunctions.update.co2 = function () for i=0, 10 do system.print("coroutine 2 --- update --- "..i) coroutine.yield(self.co2) -- pause the coroutine 2 endend
--Functions to load as coroutines that will be runned in system > onFlush (60 times / s)functions.flush.co1 = function () for i=0, 10 do system.print("coroutine 1 --- flush --- "..i) coroutine.yield(self.co1) -- pause the coroutine 1 endendfunctions.flush.co2 = function () for i=0, 10 do system.print("coroutine 2 --- flush --- "..i) coroutine.yield(self.co2) -- pause the coroutine 2 endend
--[[ do not change the following]]NestCo:onUpdate(functions.update) --loading coroutines for system > onUpdateNestCo:onFlush(functions.flush) --loading coroutines for system > onFlush