This page documents a user-defined function that you can copy and paste into your addon. Replace <PREFIX> with your AddOn's prefix to avoid conflicts between different versions of these functions.
← User defined functions < GameTime:Get
This is an ultra-lite library for retrieving server time in second + millisecond precision. It will stay at hour+minute precision for the first minute played, but will be accurate after that.
hour, minute, second = <PREFIX>_GameTime:Get()
Function Parameters[]
Returns[]
- hour, minute, second
- hour
- Integer number 0-59
- minute
- Integer number 0-59
- second
- Floating-point number with (theoretically) millisecond precision.
Note that the actual precision is limited by your framerate. With 50FPS, the precision will be 1/50=0,02.
Details[]
- The code creates an invisible frame that measures when GetGameTime() moves into a new minute in an <OnUpdate> handler, and uses GetTime() to offset seconds/milliseconds from when it last happened. Obviously, it cannot return second precision until this has happened the first time.
Code[]
<PREFIX>_GameTime = {
-----------------------------------------------------------
-- function <PREFIX>_GameTime:Get()
--
-- Return game time as (h,m,s) where s has 3 decimals of
-- precision (though it's only likely to be precise down
-- to ~20th of seconds since we're dependent on frame
-- refreshrate).
--
-- During the first minute of play, the seconds will
-- consistenly be "00", since we haven't observed any
-- minute changes yet.
--
--
Get = function(self)
if(self.LastMinuteTimer == nil) then
local h,m = GetGameTime();
return h,m,0;
end
local s = GetTime() - self.LastMinuteTimer;
if(s>59.999) then
s=59.999;
end
return self.LastGameHour, self.LastGameMinute, s;
end,
-----------------------------------------------------------
-- function <PREFIX>_GameTime:OnUpdate()
--
-- Called by: Private frame <OnUpdate> handler
--
-- Construct high precision server time by polling for
-- server minute changes and remembering GetTime() when it
-- last did
--
OnUpdate = function(self)
local h,m = GetGameTime();
if(self.LastGameMinute == nil) then
self.LastGameHour = h;
self.LastGameMinute = m;
return;
end
if(self.LastGameMinute == m) then
return;
end
self.LastGameHour = h;
self.LastGameMinute = m;
self.LastMinuteTimer = GetTime();
end,
-----------------------------------------------------------
-- function <PREFIX>_GameTime:Initialize()
--
-- Create frame to pulse OnUpdate() for us
--
Initialize = function(self)
self.Frame = CreateFrame("Frame");
self.Frame:SetScript("OnUpdate", function() self:OnUpdate(); end);
end
}
<PREFIX>_GameTime:Initialize();