Originally Posted by Klingo
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:
local mainFunction() -- call the first sub-function that contains some 'zo_callLater' local isCompleted = callFirstSubFunction() while (isCompleted == nil) do -- do nothing; wait end -- call the second sub-function callAnotherSubFunction() 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:
local foo_done = false
local bar_done = false
function checkEnd()
if foo_done and bar_done then
--call end stuff
end
end
function foo()
--Do stuff
foo_done = true
checkEnd()
end
function bar()
--Do other stuff
bar_done = true
checkEnd()
end
function mainFunction()
foo_done = false
bar_done = false
--asynchronous calls
zo_callLater(foo, 100)
zo_callLater(bar, 100)
end