Quantcast
WoWInterface - View Single Post - Is there any way to throttle an arbitrary function?
View Single Post
09-13-19, 06:22 AM   #3
kurapica.igas
A Flamescale Wyrmkin
Join Date: Aug 2011
Posts: 129
I have a lib named Scorpio, it has a task schedule system works like:

Lua Code:
  1. Scorpio "TestAddon" "1.0.0"
  2.  
  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
  10.  
  11.         print("Start combat")
  12.  
  13.        local cnt = 0
  14.  
  15.         repeat
  16.             Delay(1)    -- delay for 1 sec
  17.             cnt = cnt + 1
  18.             print("[Combat]", cnt)
  19.         until not InCombatLockdown()
  20.  
  21.         print("End combat")
  22.     end
  23. end
  24.  
  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 https://github.com/kurapica/Scorpio, 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.

Last edited by kurapica.igas : 09-13-19 at 06:42 AM.
  Reply With Quote