Saturday, April 21, 2012

delay in lua

Hi

I'm trying to write a mod to output multiple lines of text to the chat channel and was trying to find out a way of putting a delay (say 5 seconds) between each line|||Code:
function ActionButton_OnUpdate()
if (MyAddon_LastTime == nil) then
MyAddon_LastTime = GetTime()
else
if (GetTime() >= MyAddon_LastTime + 5) then

-- Do stuff here every 5 seconds --

MyAddon_LastTime = GetTime()
end
end
end

You have to drive this kind of code by hooking events or use an On_Update from a frame. I use the Blizzard bar function ActionButton_OnUpdate() which is driven MANY times a second.|||Be carefull with using OnUpdate() though... That code is processed multiple times per second, so it needs to be as light as possible.

Better to trigger off of events, but from what you're describing, yes, OnUpdate is your trigger...|||The problem with triggering from events is that you might not get one for more than 5 seconds, or whatever time measurement you want to use. The only way to have complete accuracy ,as far as timing goes, is to use an On_Update. If time accuracy is not important to what you are trying to do then yes, hooking events is the way to go.|||Thanks guys tried the OnUpdate() and it works nicely , will try the OnEvent handler later to see how they compare|||You may want to count passed time yourself using first argument for OnUpdate handler. I didn't check how heavy GetTime implementation is, but addition and getting two variables from local stack is sure to be faster than table lookup (function is in _G) and function call.

BTW, you can hide frame with OnUpdate handler when you no longer need to watch delay to stop receiving updates and nullify any performance penalty and bring it up again when you need it. In that case you wouldn't have to worry about microsavings much.|||A neater timeout function looks like this:


Code:
function fnBGW_OnUpdate(arg1) -- arg1 is in secs so may me small like 0.020s

glb_elapsedTime = glb_elapsedTime + arg1;

if(glb_elapsedTime >= TIME_TICKS) then
glb_elapsedTime = glb_elapsedTime - TIME_TICKS;
fnBGW_TimeOut();
end
end

The argument to OnUpdate is in secs so it may be small 0.020 (50Hz). Arg1 is time elapsed since last call to OnUpdate. This one saves a call to GetTime else it's the same|||I've been trying every timeout method I could find.

The ones posted here do not work in my application.

The GetTime() looked a little closer, but I only need a one time 5 second delay.

Here is was I need to wait 5 seconds before executing.


Code:
function LevelUp_OnEvent(event)
if ( (event == "PLAYER_LEVEL_UP") ) then
playerName = UnitName("player");
levlup = UnitLevel("player");
SendChatMessage("DING! "..playerName.." has reached level "..levelUp.." !", "GUILD", mylang, "");
end
end

This code is getting the level info too fast, and reporting the LAST level not the level gained.

I've used levelup = UnitLevel("player")+1;

as a temporary fix.

But I worry that if used on a slow comp, or lag comes into play it might report the level wrong (one level up).

Would like to just see this wait 5 seconds before execution, then the level info gathered should be the new correct level.|||Quote:








I've been trying every timeout method I could find.

The ones posted here do not work in my application.

The GetTime() looked a little closer, but I only need a one time 5 second delay.

Here is was I need to wait 5 seconds before executing.


Code:
function LevelUp_OnEvent(event)
if ( (event == "PLAYER_LEVEL_UP") ) then
playerName = UnitName("player");
levlup = UnitLevel("player");
SendChatMessage("DING! "..playerName.." has reached level "..levelUp.." !", "GUILD", mylang, "");
end
end

This code is getting the level info too fast, and reporting the LAST level not the level gained.

I've used levelup = UnitLevel("player")+1;

as a temporary fix.

But I worry that if used on a slow comp, or lag comes into play it might report the level wrong (one level up).

Would like to just see this wait 5 seconds before execution, then the level info gathered should be the new correct level.






When the PLAYER_LEVEL_UP event occurs, then the details you need are actually passed as arguments, and the NEW Player level can be found in arg1 so you shouldn't need any delaying mechanism if I have understood the thread (?)...


Code:
function LevelUp_OnEvent(event)
if ( (event == "PLAYER_LEVEL_UP") ) then
local playerName = UnitName("player");
local levlup = arg1;
SendChatMessage("DING! "..playerName.." has reached level "..levelUp.." !", "GUILD", mylang, "");
end
end

Other useful information is passed in the other default args, such as how much Stamina increase they earned that level...

Check out http://www.wowwiki.com/Events_P_(Par...rBank,_Player)

and scroll down to the PLAYER_LEVEL_UP event...

No comments:

Post a Comment