Wait for a function to end before continue
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:
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 |
This is when I typically use the update tic. Update tic callbacks are "asynchronous". Your wait loop is "synchronous". It technically causes more churn, and locks the process until that loop ends.
Code:
local firstRunCalled = false |
Klingo, the snippet you posted looks ripped out of context, and unlike what you described. isCompleted is assigned exactly once, never changes value, so that loop is either done immediately, or runs forever :)
I'd slightly modify Wykkyd's method. In your event handler, check if an update func is registered - if yes, return immediately (or note that the whole process needs restarting; depends on what your addon does with the event), otherwise do required preparations and register for update. In update you can check elapsed time, call other functions when they're due, and after the last one is finished, unregister (or restart if there was another event). |
Quote:
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:
|
There are two even better ways to accomplish this that do not use any sort of a timer to poll to see if it's ready.
#1 - Have the script you assign to the zo_callLater call actually call the next function when it's finished. Lua Code:
#2 - Use callbacks. Register for a custom callback (a custom event) which is fired by the zo_callLater call, and have your callback handler be the second function. Lua Code:
|
Quote:
|
Quote:
|
Thanks a lot for all the feedback, it's much appreciated!
The solution with callbacks definitely seems to be the best way to go on with. |
All times are GMT -6. The time now is 07:13 PM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2014 - 2022 MMOUI