Coroutines, Loop, or update over time without freezing?
I would like to make a loop or update that continues for a few seconds.
Using while(nowTime < endTime) freezes the game till it's finished looping. Are there coroutines in lua? |
Lua does have them, but the lua variant used in eso is stripped of coroutines.
Votan created a lib (LibAsync), which can be used instead. With the lib you can create a sequence of commands which are then spread over multiple frames via the game's OnUpdate event. You can find the lib in his minimap addon. I haven't used the lib myself so the following example might be wrong. From looking at the lib's code I think it is used like this: Let's say you want to replace the following: Lua Code:
Lua Code:
Variant A has the benefit that you can pause or interrupt the task via task:Suspend() or task:Cancel() |
Edit: I threw something into the wiki.
Lua Code:
Or, more asynchronous - this should call the function around 10 times Lua Code:
|
The other way is by using EVENT_MANAGER:RegisterForUpdate("name", function, timer)
You can then EVENT_MANAGER:UnregisterForUpdate("name") to stop the function from running if/when needed. Note there's a lot of discussion over whether or not registering and unregistering events on a consistent basis is a good idea or not. I use it for a few things and I haven't noticed any negative impact from it. Also it's widely excepted that you should avoid zo_callLater most of the time unless you're only using it sparingly or for something you absolutely need it for. There is one nice advantage to RegisterForUpdate, and that is you can make a continuous throttle by reregistering the update function whenever a certain function triggers. I use this in LUI Extended to make a "x" variable duration throttle timer for Experience Gain for example. If the player goes 5 seconds without gaining XP then the saved value is dumped, reset, and the event is unregistered. If the player gains XP again during that 5 sec window, the event is reregistered and thus the timer restarts. |
Isn't zo_callLater just a verison of RegisterForUpdate(...) ? I'm not sure why everyone says zo_callLater is not ok but RegisterForUpdate is then :D
|
They both work. Registering the timer directly with EVENT_MANAGER directly is more efficient than calling zo_callLater(), but the later is easier to deal with.
This is literally what zo_callLater() does. Lua Code:
On an added note, if you use EVENT_MANAGER:RegisterForUpdate() and don't unregister the function, it'll just keep calling at the interval you gave it. This is something zo_callLater() simply can't do without calling it more times. |
Awesome! Thank you all so much!
|
Quote:
EVENT_MANAGER:RegisterForUpdate() is nice in a way that as long as you use a fixed string as the first parameter it will still just run once at every intervall. A second registration attempt with the same name simply fails (it also returns false then). So it's best to decide on a per case basis which one to use. |
Quote:
|
All times are GMT -6. The time now is 07:41 AM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2014 - 2022 MMOUI