···11# du-nested-coroutines
22 A small script for DU to avoid any CPU load error
33+44+### How to use
55+Write your functions in the unit > start event, in the table `MyCoroutines`.
66+77+You can use the stardard coroutine.yield method in your function by always placing as parameter the table `coroutinesTable[index]` replacing `index` with the index of your function (1 for the 1st function, 2 for the second and so on)
88+99+#### example
1010+1111+```lua
1212+MyCoroutines = {
1313+ function()
1414+ for i=0, 10 do
1515+ system.print("function 1 --- "..i)
1616+ coroutine.yield(coroutinesTable[1])--start with index 1 and so on
1717+ end
1818+ end,
1919+ function()
2020+ for i=0, 10 do
2121+ system.print("function 2 --- "..i)
2222+ coroutine.yield(coroutinesTable[2])--the second fonction yiel is with index 2
2323+ end
2424+ end
2525+}
2626+```
···11+coroutinesTable = {}
22+--all functions here will become a coroutine
33+MyCoroutines = {
44+ function()
55+ for i=0, 10 do
66+ system.print("function 1 --- "..i)
77+ coroutine.yield(coroutinesTable[1])--start with index 1 and so on
88+ end
99+ end,
1010+ function()
1111+ for i=0, 10 do
1212+ system.print("function 2 --- "..i)
1313+ coroutine.yield(coroutinesTable[2])--the second fonction yiel is with index 2
1414+ end
1515+ end
1616+}
1717+1818+function initCoroutines()
1919+ for _,f in pairs(MyCoroutines) do
2020+ local co = coroutine.create(f)
2121+ table.insert(coroutinesTable, co)
2222+ end
2323+end
2424+2525+initCoroutines()
2626+2727+runCoroutines = function()
2828+ for i,co in ipairs(coroutinesTable) do
2929+ if coroutine.status(co) == "dead" then
3030+ coroutinesTable[i] = coroutine.create(MyCoroutines[i])
3131+ end
3232+ if coroutine.status(co) == "suspended" then
3333+ assert(coroutine.resume(co))
3434+ end
3535+ end
3636+end
3737+3838+MainCoroutine = coroutine.create(runCoroutines)
+6
Source/update/update.lua
···11+if coroutine.status(MainCoroutine) == "dead" then
22+ MainCoroutine = coroutine.create(runCoroutines)
33+end
44+if coroutine.status(MainCoroutine) == "suspended" then
55+ assert(coroutine.resume(MainCoroutine))
66+end