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

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