View Single Post
09-11-16, 10:55 PM   #9
kurapica.igas
A Chromatic Dragonspawn
Join Date: Aug 2011
Posts: 152
I do create a Task system with several features like :

Lua Code:
  1. Task.DirectCall(func, ...)           -- Call the method as soon as possible(call it directly or next phase if the operation time is fully used)
  2.  
  3. Task.NextCall(func, ...)            -- Call in next phase(next OnUpdate)
  4.  
  5. Task.DelayCall(delay, func, ...)  -- Call the func with a delay, just like the C_Timer.After
  6.  
  7. Task.EventCall(event, func)      -- Call the func when the event is fired, no args since the event would provide
  8.  
  9. Task.NoCombatCall(func, ...)   -- Call the func when the player is not in combat, used for secure frames.
  10.  
  11. Task.WaitCall([delay, ][event, ...,] func)  -- Call the func when meet the delay or any event is fired.
  12.  
  13. Task.ThreadCall(func, ...)         -- Call the func in a thread from the thread pool.

Also several features for thread only
Lua Code:
  1. Task.Continue()                      -- Check if the current thread should keep running or wait for next time
  2.  
  3. Task.Next()                           -- Make the current thread wait for next phase
  4.  
  5. Task.Delay(delay)                  -- Delay the current thread for several second
  6.  
  7. Task.Event(event)                  -- Make the current thread wait for a system event, the args would be returned
  8.  
  9. Task.Wait([delay, ][event, ... ,]) -- Make the current thread wait for a delay or any of the events,if it's an event, the event and it's args would be returned.

For a simple container refresh, the code would be like

Lua Code:
  1. local toggle = true
  2.  
  3. Task.ThreadCall(function()
  4.     while toggle do
  5.         -- Make sure the operation only process when not in combat
  6.         if InCombatLockdown() then Task.Event("PLAYER_REGEN_ENABLED") end
  7.  
  8.         for bag = 0, 4 do
  9.             for slot = 1, GetContainerNumSlots(bag) do
  10.                 -- do something
  11.  
  12.                 -- To smooth the updating
  13.                 Task.Continue()
  14.             end
  15.         end
  16.  
  17.         -- Wait for next update
  18.         Task.Event("BAG_UPDATE_DELAYED")
  19.     end
  20. end)

It'd be useful to put all codes together in some conditions.

Last edited by kurapica.igas : 09-11-16 at 10:58 PM.
  Reply With Quote