WoWInterface (
-   Lua/XML Help (
-   -   Is there any way to throttle an arbitrary function? (

Odjur 09-13-19 04:38 AM

Is there any way to throttle an arbitrary function?
I am currently writing my functions to run for N iterations before calling them again, but it would be much more convenient and consistent to be able to pass my functions off to a handler/scheduler and have them take care of them for me. This is an example of what I am trying to do:


local thread
local delay
local count = 0

local queue = function()
        for a = 1, 10000000 do
                count = a

C_Timer.NewTicker(0, function()
        if queue then
                if not thread then
                        thread = coroutine.create(function()
                                delay = C_Timer.NewTicker(0, function()
                                        if coroutine.status(thread) == "dead" then
                                                queue = nil
                                                thread = nil

I was trying to pass my functions to the variable 'queue' and have them turned into coroutines that would be yielded every frame. However it seems that C_Timer.NewTicker doesn't run asynchronously, so it never gets to yield the coroutine.

Is there any way to do what I am trying to do? It seems strange to me that I can't come up with a clean way to throttle my functions. Thanks for your help.

MooreaTv 09-13-19 05:05 AM

To yield every frame you have to call yield inside the loop not after the loop is finished

yield only yields the current coroutine not another one (which wouldn't be possible given the lua interpreter in wow is single threaded, which you can be thankful for otherwise we'd have locking issues)

kurapica.igas 09-13-19 06:22 AM

I have a lib named Scorpio, it has a task schedule system works like:

Lua Code:
  1. Scorpio "TestAddon" "1.0.0"
  3. -- __Async__ is used to wrap the target function, so when the
  4. -- function is called, it'd be processed in a coroutine, the coroutine
  5. -- will be recycled when the target function finished its job
  6. __Async__()
  7. function LoopJob()
  8.     while true do
  9.         NextEvent("PLAYER_REGEN_DISABLED")  -- wait the event
  11.         print("Start combat")
  13.        local cnt = 0
  15.         repeat
  16.             Delay(1)    -- delay for 1 sec
  17.             cnt = cnt + 1
  18.             print("[Combat]", cnt)
  19.         until not InCombatLockdown()
  21.         print("End combat")
  22.     end
  23. end
  25. -- Start the loop
  26. LoopJob()

With the system, we can avoid the callback hell and keep any logic in one function. And the better part of the system is if we put all hard works to it, it really can smooth the function calls(delay the call if there is too much operations in one phase) and make the fps no dropping.

So, if you are interesting, you can find the lib at, it's also uploaded to the curseforge.

For your example, you may check the Next and Continue API provided by the Scoprio, the Next is used to delay the process to the next phase, the Continue is used to check if there is enough time in the current phase to continue the process, otherwise, it'd delay it to the next phase.

All times are GMT -6. The time now is 09:32 AM.

vBulletin © 2020, Jelsoft Enterprises Ltd
© 2004 - 2020 MMOUI