Thread Tools Display Modes
03/29/15, 07:14 AM   #1
Area51
AddOn Author - Click to view addons
Join Date: Dec 2014
Posts: 20
Possible to update variable every xxx seconds?

Trying to find an efficient way to update a variable every 10 seconds... any help appreciated.
  Reply With Quote
03/29/15, 07:34 AM   #2
Ayantir
 
Ayantir's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 1,019
Lua Code:
  1. myvar = "foo"
  2. every10s() -- <-- will set myvar to something
  3. myvar= "plonk"
  4.  
  5. -- 10s after value "plonk" will be overriden.
  6.  
  7. function every10s()
  8.      myvar="something"
  9.      zo_callLater(every10s(), 10 * 10000)
  10. end

Last edited by Ayantir : 03/29/15 at 07:38 AM.
  Reply With Quote
03/29/15, 07:36 AM   #3
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
Originally Posted by Ayantir View Post
Lua Code:
  1. myvar = "foo"
  2. zo_callLater(function() myvar="something" end, 10 * 1000)
This will be called only once. If you want call function every 10 seconds, use:
Lua Code:
  1. local function MyFunction()
  2.     --do something
  3. end
  4.  
  5. EVENT_MANAGER:RegisterForUpdate("someUniqueIdentifier", 10000, MyFunction) --10000 milliseconds = 10 seconds


EDIT:
Originally Posted by Ayantir View Post
Lua Code:
  1. myvar = "foo"
  2. every10s()
  3.  
  4. function every10s()
  5.      myvar="something"
  6.      zo_callLater(every10s(), 10 * 10000)
  7. end
Even if this will work, I can't recommend this solution. It will basically register for update after 10 seconds, the same way as I did in my code, then it will unregister function for update and then again register for update... To much unnecessary steps involved.

How is zo_callLater defined?
Lua Code:
  1. local ZO_CallLaterId = 1
  2.  
  3. function zo_callLater(func, ms)
  4.     local id = ZO_CallLaterId
  5.     local name = "CallLaterFunction"..id
  6.     ZO_CallLaterId = ZO_CallLaterId + 1
  7.  
  8.     EVENT_MANAGER:RegisterForUpdate(name, ms,
  9.         function()
  10.             func(id)
  11.             EVENT_MANAGER:UnregisterForUpdate(name)
  12.         end)
  13.     return id
  14. end

Last edited by Garkin : 03/29/15 at 07:43 AM.
  Reply With Quote
03/29/15, 07:40 AM   #4
Ayantir
 
Ayantir's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 1,019
Originally Posted by Garkin View Post
This will be called onlt once. If you want call function every 10 seconds, use:
Lua Code:
  1. local function MyFunction()
  2.     --do something
  3. end
  4.  
  5. EVENT_MANAGER:RegisterForUpdate("someUniqueIdentifier", 10000, MyFunction) --10000 milliseconds = 10 seconds

Yeah, I edited Bour both snips are working also. there are others..
  Reply With Quote
03/29/15, 07:40 AM   #5
Area51
AddOn Author - Click to view addons
Join Date: Dec 2014
Posts: 20
Thanks

Thank you both Exactly what i needed.
  Reply With Quote
03/31/15, 05:39 PM   #6
SpellBuilder
 
SpellBuilder's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2014
Posts: 10
I have connected question, so not to create separate thread I will ask here:

Is there any "sleep()" function? For example I want to blink with some color for 500ms and then return it's state to original one. Right now I am doing it with double nested zo_callLater() functions (second one is needed to simulate dead-time before next blink can happen). Now after reading this thread I realize, that maybe this is not very efficient way to do things, and I do not need really my function to be non-blocking. I can live normally with blocking execution of the function in which I would have two "sleep()" functions if it exists.
  Reply With Quote
03/31/15, 05:48 PM   #7
circonian
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 613
Originally Posted by SpellBuilder View Post
I have connected question, so not to create separate thread I will ask here:

Is there any "sleep()" function? For example I want to blink with some color for 500ms and then return it's state to original one. Right now I am doing it with double nested zo_callLater() functions (second one is needed to simulate dead-time before next blink can happen). Now after reading this thread I realize, that maybe this is not very efficient way to do things, and I do not need really my function to be non-blocking. I can live normally with blocking execution of the function in which I would have two "sleep()" functions if it exists.
Is your sleep() function the function that does the blinking or is it for something else? I'm not sure what you mean about having two "sleep()" functions.

I'm not sure, but I think this is what you want:
I'm guessing that your blinking is done in the sleep() function.
Lua Code:
  1. EVENT_MANAGER:RegisterForUpdate("blinkingUpdate", 500, sleep)

Every 500ms it will call the function sleep. So if that is what handles your blink code, it would run every 500 ms.

Whenever your done with it and want it to stop running you just call:
Lua Code:
  1. EVENT_MANAGER:UnregisterForUpdate("blinkingUpdate")
  Reply With Quote
03/31/15, 07:56 PM   #8
merlight
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 671
Originally Posted by SpellBuilder View Post
... and I do not need really my function to be non-blocking. I can live normally with blocking execution of the function in which I would have two "sleep()" functions if it exists.
You really don't want to block the whole game for half a second Using RegisterForUpdate with 500ms ticks is the way to go.
  Reply With Quote
03/31/15, 08:23 PM   #9
SpellBuilder
 
SpellBuilder's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2014
Posts: 10
Well, right now I have following code
Lua Code:
  1. local g_powerError = false
  2.  
  3. local function doBlink(backdrop)
  4.     if g_powerError then return end
  5.        
  6.     g_powerError = true
  7.  
  8.     -- save original center colour and colour to red
  9.     local r,g,b = backdrop:GetCenterColor()
  10.     backdrop:SetCenterColor( 0.4, 0, 0, 0.9 )
  11.  
  12.     -- make a delayed call to return original colour
  13.     zo_callLater(function()
  14.         backdrop:SetCenterColor( r, g, b, 0.9 )
  15.         zo_callLater(function() g_powerError = false end, 200)
  16.     end, 300 )
  17. end
This function can be called in some event handler. It will change color of a control to red for 300ms, but then still will have another 200ms deadtime before it can blink again due to external call. It means that all other calls to doBlink() while it is "working" will be ignored.

What I wanted to do is something like this:
Lua Code:
  1. local g_powerError = false
  2.  
  3. local function doBlink(backdrop)
  4.     if g_powerError then return end
  5.        
  6.     g_powerError = true
  7.  
  8.     -- save original center colour and colour to red
  9.     local r,g,b = backdrop:GetCenterColor()
  10.     backdrop:SetCenterColor( 0.4, 0, 0, 0.9 )
  11.  
  12.     sleep(300)
  13.  
  14.     backdrop:SetCenterColor( r, g, b, 0.9 )
  15.  
  16.     sleep(200)
  17.  
  18.     g_powerError = false
  19.  
  20. end

But I guess, that I should either stick with initial zo_callLater version, or simply directly use EVENT_MANAGER to do same work manually and do not abuse global ZO_CallLaterId counter.
  Reply With Quote
03/31/15, 11:11 PM   #10
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
Originally Posted by SpellBuilder View Post
But I guess, that I should either stick with initial zo_callLater version, or simply directly use EVENT_MANAGER to do same work manually and do not abuse global ZO_CallLaterId counter.
It is exactly as you said. There is no sleep command, all you have is EVENT_MANAGER:RegisterForUpdate(...), zo_callLater(...) which uses mentioned EVENT_MANAGER's method or OnUpdate event for top level controls.

But if you want to try something different, you can use animation. Either alpha animation for overlay texture or color animation for your control.

Lua Code:
  1. --esing function used to modify animation progress. Progress is a number between 0 and 1.
  2. local function easingFunc(progress)
  3.     --animate from 0 to 100% in the first 50ms - progress 0-0.1
  4.     if progress <= 0.1
  5.         return progress * 10
  6.     --next 200ms return 100% - progress > 0.1 and progress <= 0.3
  7.     elseif progress > 0.1 and progress <= 0.3 then
  8.         return 1
  9.     --next 50ms animate from 100% to 0% - progress > 0.3 and progress < 0.4
  10.     elseif progress > 0.3 and progress <= 0.4 then
  11.         return 1 - ((progress - 0.3) * 10)
  12.     end
  13.     --last 100ms return 0%
  14.     return 0
  15. end
  16.  
  17. local function CreateBlinkAnimation(control)
  18.     local timeline = ANIMATION_MANAGER:CreateTimeline()
  19.     timeline:SetPlaybackType(ANIMATION_PLAYBACK_ONE_SHOT)
  20.     local animation = timeline:InsertAnimation(ANIMATION_COLOR, control, 0)
  21.     --here you have to set some actual color values
  22.     animation:SetColorValues(startR, startG, startB, startA, endR, endG, endB, endA)
  23.     animation:SetDuration(500) --milliseconds
  24.     animation:SetEasingFunction(easingFunc)
  25.  
  26.     return timeline
  27. end
  28.  
  29. local function doBlink(control)
  30.     if not control.timeline then
  31.         control.timeline = CreateBlinkAnimation(control)
  32.     end
  33.  
  34.     if not control.timeline:IsPlaying() then
  35.         control.timeline:PlayFromStart()
  36.     end
  37. end
  Reply With Quote

ESOUI » AddOns » AddOn Help/Support » Possible to update variable every xxx seconds?


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off