···11+--[[
22+ DU-Nested-Coroutines by Jericho
33+ Permit to easier avoid CPU Load Errors
44+ Source available here: https://github.com/Jericho1060/du-nested-coroutines
55+]]--
66+77+NestCo:flush()
+71-18
Source/unit/onStart.lua
···55]]--
66local DU_Nested_Coroutines = {
77 __index = {
88- coroutines = {},
99- functions = {},
1010- main = coroutine.create(function() end),
88+ coroutines = {
99+ update = {},
1010+ flush = {}
1111+ },
1212+ functions = {
1313+ update = {},
1414+ flush = {}
1515+ },
1616+ main = {
1717+ update = coroutine.create(function() end),
1818+ flush = coroutine.create(function() end),
1919+ },
1120 update = function(self)
1212- local status = coroutine.status(self.main)
2121+ local status = coroutine.status(self.main.update)
2222+ if status == "dead" then
2323+ self.main.update = coroutine.create(function() self:runUpdate() end)
2424+ elseif status == "suspended" then
2525+ assert(coroutine.resume(self.main.update))
2626+ end
2727+ end,
2828+ flush = function(self)
2929+ local status = coroutine.status(self.main.flush)
1330 if status == "dead" then
1414- self.main = coroutine.create(function() self:run() end)
3131+ self.main.flush = coroutine.create(function() self:runFlush() end)
1532 elseif status == "suspended" then
1616- assert(coroutine.resume(self.main))
3333+ assert(coroutine.resume(self.main.flush))
1734 end
1835 end,
1919- run = function(self)
2020- for k,co in pairs(self.coroutines) do
3636+ runUpdate = function(self)
3737+ for k,co in pairs(self.coroutines.update) do
2138 local status = coroutine.status(co)
2239 if status == "dead" then
2323- self.coroutines[k] = coroutine.create(self.functions[k])
4040+ self.coroutines.update[k] = coroutine.create(self.functions.update[k])
4141+ elseif status == "suspended" then
4242+ assert(coroutine.resume(co))
4343+ end
4444+ end
4545+ end,
4646+ runFlush = function(self)
4747+ for k,co in pairs(self.coroutines.flush) do
4848+ local status = coroutine.status(co)
4949+ if status == "dead" then
5050+ self.coroutines.flush[k] = coroutine.create(self.functions.flush[k])
2451 elseif status == "suspended" then
2552 assert(coroutine.resume(co))
2653 end
2754 end
2855 end,
2929- init = function(self, functions)
5656+ onUpdate = function(self, functions)
3057 for k,f in pairs(functions) do
3131- self.functions[k] = f
3232- self.coroutines[k] = coroutine.create(f)
5858+ self.functions.update[k] = f
5959+ self.coroutines.update[k] = coroutine.create(f)
6060+ end
6161+ end,
6262+ onFlush = function(self, functions)
6363+ for k,f in pairs(functions) do
6464+ self.functions.flush[k] = f
6565+ self.coroutines.flush[k] = coroutine.create(f)
3366 end
3467 end
3568 }
···4174 add the functions you want to run as coroutines here
4275 you can remove co1 and co2 that are given as example
4376]]
4444-local functions = {}
4545-functions.co1 = function ()
7777+local functions = {
7878+ update = {},
7979+ flush = {}
8080+}
8181+8282+--Functions to load as coroutines that will be runned in system > onUpdate (based on FPS)
8383+functions.update.co1 = function ()
4684 for i=0, 10 do
4747- system.print("coroutine 1 --- "..i)
8585+ system.print("coroutine 1 --- update --- "..i)
4886 coroutine.yield(self.co1) -- pause the coroutine 1
4987 end
5088end
5151-functions.co2 = function ()
8989+functions.update.co2 = function ()
5290 for i=0, 10 do
5353- system.print("coroutine 2 --- "..i)
9191+ system.print("coroutine 2 --- update --- "..i)
9292+ coroutine.yield(self.co2) -- pause the coroutine 2
9393+ end
9494+end
9595+9696+--Functions to load as coroutines that will be runned in system > onFlush (60 times / s)
9797+functions.flush.co1 = function ()
9898+ for i=0, 10 do
9999+ system.print("coroutine 1 --- flush --- "..i)
100100+ coroutine.yield(self.co1) -- pause the coroutine 1
101101+ end
102102+end
103103+functions.flush.co2 = function ()
104104+ for i=0, 10 do
105105+ system.print("coroutine 2 --- flush --- "..i)
54106 coroutine.yield(self.co2) -- pause the coroutine 2
55107 end
56108end
···58110--[[
59111 do not change the following
60112]]
6161-NestCo:init(functions)
113113+NestCo:onUpdate(functions.update) --loading coroutines for system > onUpdate
114114+NestCo:onFlush(functions.flush) --loading coroutines for system > onFlush
+1-1
config.json
···11-{"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":[]}
11+{"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":[]}