[READ-ONLY] Mirror of https://github.com/thoda-dev/du-nested-coroutines. A small script for DU to avoid any CPU load error
0

Configure Feed

Select the types of activity you want to include in your feed.

v3.1.0 -- support for coroutines in system > onFlush

coroutines runned in system > onUpdate will be runned based on FPS

coroutines runned in system > onFlush ill be runned 60 times by second

Thomas (Jan 4, 2023, 2:02 PM +0100) b5658252 231970e5

+79 -19
+7
Source/system/onFlush.lua
··· 1 + --[[ 2 + DU-Nested-Coroutines by Jericho 3 + Permit to easier avoid CPU Load Errors 4 + Source available here: https://github.com/Jericho1060/du-nested-coroutines 5 + ]]-- 6 + 7 + NestCo:flush()
+71 -18
Source/unit/onStart.lua
··· 5 5 ]]-- 6 6 local DU_Nested_Coroutines = { 7 7 __index = { 8 - coroutines = {}, 9 - functions = {}, 10 - main = coroutine.create(function() end), 8 + coroutines = { 9 + update = {}, 10 + flush = {} 11 + }, 12 + functions = { 13 + update = {}, 14 + flush = {} 15 + }, 16 + main = { 17 + update = coroutine.create(function() end), 18 + flush = coroutine.create(function() end), 19 + }, 11 20 update = function(self) 12 - local status = coroutine.status(self.main) 21 + local status = coroutine.status(self.main.update) 22 + if status == "dead" then 23 + self.main.update = coroutine.create(function() self:runUpdate() end) 24 + elseif status == "suspended" then 25 + assert(coroutine.resume(self.main.update)) 26 + end 27 + end, 28 + flush = function(self) 29 + local status = coroutine.status(self.main.flush) 13 30 if status == "dead" then 14 - self.main = coroutine.create(function() self:run() end) 31 + self.main.flush = coroutine.create(function() self:runFlush() end) 15 32 elseif status == "suspended" then 16 - assert(coroutine.resume(self.main)) 33 + assert(coroutine.resume(self.main.flush)) 17 34 end 18 35 end, 19 - run = function(self) 20 - for k,co in pairs(self.coroutines) do 36 + runUpdate = function(self) 37 + for k,co in pairs(self.coroutines.update) do 21 38 local status = coroutine.status(co) 22 39 if status == "dead" then 23 - self.coroutines[k] = coroutine.create(self.functions[k]) 40 + self.coroutines.update[k] = coroutine.create(self.functions.update[k]) 41 + elseif status == "suspended" then 42 + assert(coroutine.resume(co)) 43 + end 44 + end 45 + end, 46 + runFlush = function(self) 47 + for k,co in pairs(self.coroutines.flush) do 48 + local status = coroutine.status(co) 49 + if status == "dead" then 50 + self.coroutines.flush[k] = coroutine.create(self.functions.flush[k]) 24 51 elseif status == "suspended" then 25 52 assert(coroutine.resume(co)) 26 53 end 27 54 end 28 55 end, 29 - init = function(self, functions) 56 + onUpdate = function(self, functions) 30 57 for k,f in pairs(functions) do 31 - self.functions[k] = f 32 - self.coroutines[k] = coroutine.create(f) 58 + self.functions.update[k] = f 59 + self.coroutines.update[k] = coroutine.create(f) 60 + end 61 + end, 62 + onFlush = function(self, functions) 63 + for k,f in pairs(functions) do 64 + self.functions.flush[k] = f 65 + self.coroutines.flush[k] = coroutine.create(f) 33 66 end 34 67 end 35 68 } ··· 41 74 add the functions you want to run as coroutines here 42 75 you can remove co1 and co2 that are given as example 43 76 ]] 44 - local functions = {} 45 - functions.co1 = function () 77 + local functions = { 78 + update = {}, 79 + flush = {} 80 + } 81 + 82 + --Functions to load as coroutines that will be runned in system > onUpdate (based on FPS) 83 + functions.update.co1 = function () 46 84 for i=0, 10 do 47 - system.print("coroutine 1 --- "..i) 85 + system.print("coroutine 1 --- update --- "..i) 48 86 coroutine.yield(self.co1) -- pause the coroutine 1 49 87 end 50 88 end 51 - functions.co2 = function () 89 + functions.update.co2 = function () 52 90 for i=0, 10 do 53 - system.print("coroutine 2 --- "..i) 91 + system.print("coroutine 2 --- update --- "..i) 92 + coroutine.yield(self.co2) -- pause the coroutine 2 93 + end 94 + end 95 + 96 + --Functions to load as coroutines that will be runned in system > onFlush (60 times / s) 97 + functions.flush.co1 = function () 98 + for i=0, 10 do 99 + system.print("coroutine 1 --- flush --- "..i) 100 + coroutine.yield(self.co1) -- pause the coroutine 1 101 + end 102 + end 103 + functions.flush.co2 = function () 104 + for i=0, 10 do 105 + system.print("coroutine 2 --- flush --- "..i) 54 106 coroutine.yield(self.co2) -- pause the coroutine 2 55 107 end 56 108 end ··· 58 110 --[[ 59 111 do not change the following 60 112 ]] 61 - NestCo:init(functions) 113 + NestCo:onUpdate(functions.update) --loading coroutines for system > onUpdate 114 + NestCo:onFlush(functions.flush) --loading coroutines for system > onFlush
+1 -1
config.json
··· 1 - {"slots":{"0":{"name":"slot1","type":{"events":[],"methods":[]}},"1":{"name":"slot2","type":{"events":[],"methods":[]}},"2":{"name":"slot3","type":{"events":[],"methods":[]}},"3":{"name":"slot4","type":{"events":[],"methods":[]}},"4":{"name":"slot5","type":{"events":[],"methods":[]}},"5":{"name":"slot6","type":{"events":[],"methods":[]}},"6":{"name":"slot7","type":{"events":[],"methods":[]}},"7":{"name":"slot8","type":{"events":[],"methods":[]}},"8":{"name":"slot9","type":{"events":[],"methods":[]}},"9":{"name":"slot10","type":{"events":[],"methods":[]}},"-1":{"name":"unit","type":{"events":[],"methods":[]}},"-3":{"name":"player","type":{"events":[],"methods":[]}},"-2":{"name":"construct","type":{"events":[],"methods":[]}},"-4":{"name":"system","type":{"events":[],"methods":[]}},"-5":{"name":"library","type":{"events":[],"methods":[]}}},"handlers":[{"code":"--[[\n DU-Nested-Coroutines by Jericho\n Permit to easier avoid CPU Load Errors by using nested coroutines and adapt cycles on the FPS\n Source unminified available here: https://github.com/Jericho1060/du-nested-coroutines\n]]--\nlocal a={__index={coroutines={},functions={},main=coroutine.create(function()end),update=function(self)local b=coroutine.status(self.main)if b==\"dead\"then self.main=coroutine.create(function()self:run()end)elseif b==\"suspended\"then assert(coroutine.resume(self.main))end end,run=function(self)for c,d in pairs(self.coroutines)do local b=coroutine.status(d)if b==\"dead\"then self.coroutines[c]=coroutine.create(self.functions[c])elseif b==\"suspended\"then assert(coroutine.resume(d))end end end,init=function(self,e)for c,f in pairs(e)do self.functions[c]=f;self.coroutines[c]=coroutine.create(f)end end}}NestCo={}setmetatable(NestCo,a)\n\n--[[\n add the functions you want to run as coroutines here\n you can remove co1 and co2 that are given as example\n]]\nlocal functions = {}\nfunctions.co1 = function ()\n for i=0, 10 do\n system.print(\"coroutine 1 --- \"..i)\n coroutine.yield(self.co1) -- pause the coroutine 1\n end\nend\nfunctions.co2 = function ()\n for i=0, 10 do\n system.print(\"coroutine 2 --- \"..i)\n coroutine.yield(self.co2) -- pause the coroutine 2\n end\nend\n\n--[[\n do not change the following\n]]\nNestCo:init(functions)","filter":{"args":[],"signature":"onStart()","slotKey":"-1"},"key":"0"},{"code":"--[[\n DU-Nested-Coroutines by Jericho\n Permit to easier avoid CPU Load Errors\n Source available here: https://github.com/Jericho1060/du-nested-coroutines\n]]--\n\nNestCo:update()","filter":{"args":[],"signature":"onUpdate()","slotKey":"-4"},"key":"1"}],"methods":[],"events":[]} 1 + {"slots":{"0":{"name":"slot1","type":{"events":[],"methods":[]}},"1":{"name":"slot2","type":{"events":[],"methods":[]}},"2":{"name":"slot3","type":{"events":[],"methods":[]}},"3":{"name":"slot4","type":{"events":[],"methods":[]}},"4":{"name":"slot5","type":{"events":[],"methods":[]}},"5":{"name":"slot6","type":{"events":[],"methods":[]}},"6":{"name":"slot7","type":{"events":[],"methods":[]}},"7":{"name":"slot8","type":{"events":[],"methods":[]}},"8":{"name":"slot9","type":{"events":[],"methods":[]}},"9":{"name":"slot10","type":{"events":[],"methods":[]}},"-1":{"name":"unit","type":{"events":[],"methods":[]}},"-3":{"name":"player","type":{"events":[],"methods":[]}},"-2":{"name":"construct","type":{"events":[],"methods":[]}},"-4":{"name":"system","type":{"events":[],"methods":[]}},"-5":{"name":"library","type":{"events":[],"methods":[]}}},"handlers":[{"code":"--[[\n DU-Nested-Coroutines by Jericho\n Permit to easier avoid CPU Load Errors by using nested coroutines and adapt cycles on the FPS\n Source unminified available here: https://github.com/Jericho1060/du-nested-coroutines\n]]--\nlocal a={__index={coroutines={update={},flush={}},functions={update={},flush={}},main={update=coroutine.create(function()end),flush=coroutine.create(function()end)},update=function(self)local b=coroutine.status(self.main.update)if b==\"dead\"then self.main.update=coroutine.create(function()self:runUpdate()end)elseif b==\"suspended\"then assert(coroutine.resume(self.main.update))end end,flush=function(self)local b=coroutine.status(self.main.flush)if b==\"dead\"then self.main.flush=coroutine.create(function()self:runFlush()end)elseif b==\"suspended\"then assert(coroutine.resume(self.main.flush))end end,runUpdate=function(self)for c,d in pairs(self.coroutines.update)do local b=coroutine.status(d)if b==\"dead\"then self.coroutines.update[c]=coroutine.create(self.functions.update[c])elseif b==\"suspended\"then assert(coroutine.resume(d))end end end,runFlush=function(self)for c,d in pairs(self.coroutines.flush)do local b=coroutine.status(d)if b==\"dead\"then self.coroutines.flush[c]=coroutine.create(self.functions.flush[c])elseif b==\"suspended\"then assert(coroutine.resume(d))end end end,onUpdate=function(self,e)for c,f in pairs(e)do self.functions.update[c]=f;self.coroutines.update[c]=coroutine.create(f)end end,onFlush=function(self,e)for c,f in pairs(e)do self.functions.flush[c]=f;self.coroutines.flush[c]=coroutine.create(f)end end}}NestCo={}setmetatable(NestCo,a)\n\n--[[\n add the functions you want to run as coroutines here\n you can remove co1 and co2 that are given as example\n]]\nlocal functions = {\n update = {},\n flush = {}\n}\n\n--Functions to load as coroutines that will be runned in system > onUpdate (based on FPS)\nfunctions.update.co1 = function ()\n for i=0, 10 do\n system.print(\"coroutine 1 --- update --- \"..i)\n coroutine.yield(self.co1) -- pause the coroutine 1\n end\nend\nfunctions.update.co2 = function ()\n for i=0, 10 do\n system.print(\"coroutine 2 --- update --- \"..i)\n coroutine.yield(self.co2) -- pause the coroutine 2\n end\nend\n\n--Functions to load as coroutines that will be runned in system > onFlush (60 times / s)\nfunctions.flush.co1 = function ()\n for i=0, 10 do\n system.print(\"coroutine 1 --- flush --- \"..i)\n coroutine.yield(self.co1) -- pause the coroutine 1\n end\nend\nfunctions.flush.co2 = function ()\n for i=0, 10 do\n system.print(\"coroutine 2 --- flush --- \"..i)\n coroutine.yield(self.co2) -- pause the coroutine 2\n end\nend\n\n--[[\n do not change the following\n]]\nNestCo:onUpdate(functions.update) --loading coroutines for system > onUpdate\nNestCo:onFlush(functions.flush) --loading coroutines for system > onFlush\n","filter":{"args":[],"signature":"onStart()","slotKey":"-1"},"key":"0"},{"code":"--[[\n DU-Nested-Coroutines by Jericho\n Permit to easier avoid CPU Load Errors\n Source available here: https://github.com/Jericho1060/du-nested-coroutines\n]]--\n\nNestCo:update()","filter":{"args":[],"signature":"onUpdate()","slotKey":"-4"},"key":"1"},{"code":"--[[\n DU-Nested-Coroutines by Jericho\n Permit to easier avoid CPU Load Errors\n Source available here: https://github.com/Jericho1060/du-nested-coroutines\n]]--\n\nNestCo:flush()","filter":{"args":[],"signature":"onFlush()","slotKey":"-4"},"key":"2"}],"methods":[],"events":[]}