View Single Post
09/07/14, 01:33 PM   #4
Sasky
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 231
Originally Posted by Klingo View Post
Hi

I have a function that is triggered by an event and within this function there are several sub-functions that are called.
The first sub-functions makes use of some "zo_callLater" and the end of this sub-function is reached earlier than all the "zo_callLater" are ended (they might take 2-3 seconds). So the second sub-function is already called although not all "zo_callLater" from the first sub-function have ended, which causes some problems in the addon.

Currently I deal with this situation in the following way:
The first sub-function returns "true" as soon as all "zo_callLater" calls are completed. I then check this value in a loop if it is "true" and only in that case allow the main function to call the second sub-function.
Lua Code:
  1. local mainFunction()
  2.     -- call the first sub-function that contains some 'zo_callLater'
  3.     local isCompleted = callFirstSubFunction()
  4.  
  5.     while (isCompleted == nil) do
  6.         -- do nothing; wait
  7.     end
  8.  
  9.     -- call the second sub-function
  10.     callAnotherSubFunction()
  11. end

It basically works, but I am not sure how bad (performance-wise) this solution is.
I don't see any game-freezing effects like a regular endless-loop would cause, but I thought there might be a better solution for this?

Klingo
That's called busy waiting and is probably the worst-performance for waiting on something. You eat resources checking constantly, even though you don't need to.

The solution Wykkyd provided basically tones down how often you check.

If you have access to all functions you could also do something like this, where the check is at the end of each sub-call. This would keep the times you check for updates to a minimum.

Lua Code:
  1. local foo_done = false
  2. local bar_done = false
  3.  
  4. function checkEnd()
  5.     if foo_done and bar_done then
  6.         --call end stuff
  7.     end
  8. end
  9.  
  10. function foo()
  11.     --Do stuff
  12.     foo_done = true
  13.     checkEnd()
  14. end
  15.  
  16. function bar()
  17.     --Do other stuff
  18.     bar_done = true
  19.     checkEnd()
  20. end
  21.  
  22. function mainFunction()
  23.     foo_done = false
  24.     bar_done = false
  25.     --asynchronous calls
  26.     zo_callLater(foo, 100)
  27.     zo_callLater(bar, 100)
  28. end
  Reply With Quote