Thread Tools Display Modes
03/27/15, 08:05 AM   #1
Woeler
 
Woeler's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2014
Posts: 40
Simple start/stop timer

Hello guys,

Since Zenimax removed the GetCurrentRaidTime() function I would like to add my own timer that works with events. However, I have no idea how to start on this. Is there anyone out here who would be willing to help me make a simple timer in lua for my addon RaidScore? You will be creditted of course!

All I need is a timer with minutes and seconds (and maybe milliseconds) with a start, stop and reset method.

Many thanks!
  Reply With Quote
03/27/15, 09:03 AM   #2
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
Originally Posted by Woeler View Post
Hello guys,

Since Zenimax removed the GetCurrentRaidTime() function I would like to add my own timer that works with events. However, I have no idea how to start on this. Is there anyone out here who would be willing to help me make a simple timer in lua for my addon RaidScore? You will be creditted of course!

All I need is a timer with minutes and seconds (and maybe milliseconds) with a start, stop and reset method.

Many thanks!
Exactly the same thing is in Personal Timer.
Also you might want to check RaidTimer Continued - it starts / stops timer when raid starts / finishes.
  Reply With Quote
03/27/15, 09:05 AM   #3
Woeler
 
Woeler's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2014
Posts: 40
Originally Posted by Garkin View Post
Exactly the same thing is in Personal Timer.
Also you might want to check RaidTimer Continued - it starts / stops timer when raid starts / finishes.
Hey Garkin,

Thanks! Would you be ok with it if I used your RaidTimer continued as an example for the thing I will code? I will, of course, credit you appropriately.
  Reply With Quote
03/27/15, 09:13 AM   #4
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
Originally Posted by Woeler View Post
Hey Garkin,

Thanks! Would you be ok with it if I used your RaidTimer continued as an example for the thing I will code? I will, of course, credit you appropriately.
Original code was written by NickK, I have just updated it for the latest patch. You should give him credit for the original code & idea, not me.
  Reply With Quote
03/27/15, 10:47 AM   #5
Woeler
 
Woeler's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2014
Posts: 40
Ok, so I'm having an issue with this. I keep getting back a nil value. I hate these errors, because I do not know how to trace them.

Code:
12607605746921172968:4: attempt to index a nil value
stack traceback:
	12607605746921172968:4: in function '(main chunk)'

Lua Code:
  1. --Addon: RaidScore
  2. --Game: The Elder Scrolls Online: Tamriel Unlimited
  3. --Author: Woeler
  4. local LAM = LibStub:GetLibrary("LibAddonMenu-2.0")
  5. local LMP = LibStub:GetLibrary("LibMediaProvider-1.0")
  6.  
  7. --function for getting the current score
  8. function RaidScoreUpdate()
  9.     RAIDSCORECounter:SetText(string.format(GetCurrentRaidScore()))
  10. end
  11.  
  12.  
  13.  
  14.     local savedVariables --local reference for saved variables, actual object will be assigned later in EVENT_ADD_ON_LOADED event handler
  15.     local defaults = { --default values for saved variables
  16.         offsetX = 200,
  17.         offsetY = 200,
  18.         HideScores = false,
  19.         lockWindow = false,
  20.         postResults = true,
  21.         raidStart = false,
  22.     }
  23.      
  24.     --event handler for OnMoveStop
  25.     local function OnMoveStop(self)
  26.         savedVariables.offsetX = self:GetLeft()
  27.         savedVariables.offsetY = self:GetTop()
  28.     end
  29.      
  30.     --event handler for EVENT_ADD_ON_LOADED
  31.     local function OnAddonLoaded(event, addonName)
  32.         if addonName == "RaidScore" then --addonName is in general name of your addon manifext without .txt extension
  33.      
  34.             EVENT_MANAGER:UnregisterForEvent(MoveHandler, EVENT_ADD_ON_LOADED)
  35.             ZO_CreateStringId("SI_BINDING_NAME_TOGGLEHIDDEN", "Toggle hidden")
  36.             ZO_CreateStringId("SI_BINDING_NAME_POSTSCORE", "Post current score")
  37.      
  38.             --saved variables (in this case account wide)
  39.             savedVariables = ZO_SavedVars:NewAccountWide("RaidScore", 1, "namespace", defaults)
  40.      
  41.             --create top level window, so we have something to work with
  42.             RAIDSCORE:SetAnchor(TOPLEFT, GuiRoot, TOPLEFT, savedVariables.offsetX, savedVariables.offsetY) --restore window position from saved variables
  43.             RAIDSCORE:SetHandler("OnMoveStop", OnMoveStop) --set handler to event "OnMoveStop", it means when you stop moving the window
  44.             RAIDSCORE:SetHidden(savedVariables.HideScores) --set window invisible or visible
  45.             RAIDSCORE:SetMovable(savedVariables.lockWindow) --lock the window in place or not
  46.             RAIDSCORETimer:SetText("Total Time: ")
  47.            
  48.                 if IsRaidInProgress() then
  49.         Timer.TimerStart()
  50.     else
  51.         savedVariables.raidStart = nil
  52.     end
  53.        
  54.  
  55.         end
  56.     end
  57.  
  58. function ToggleHidden()
  59.     if savedVariables.HideScores == true then
  60.         RAIDSCORE:SetHidden(false)
  61.         savedVariables.HideScores = false
  62.     elseif savedVariables.HideScores == false then
  63.         RAIDSCORE:SetHidden(true)
  64.         savedVariables.HideScores = true
  65.     end
  66. end
  67.  
  68. function PostScore()
  69.     if savedVariables.postResults == true then
  70.     d("|cFF0000Current trial score: "..GetCurrentRaidScore())
  71.     end
  72. end
  73.  
  74. function PostEndScore()
  75.     if savedVariables.postResults == true then
  76.     d("|cFF0000Trial completed with a score of: "..GetCurrentRaidScore())
  77.     end
  78. end
  79.  
  80. ------Timer stuff------
  81.  
  82. local Timer = {
  83.     stop = false,
  84.     complete = false,
  85.     showTimers = false,
  86.     instance = false,
  87. }
  88.  
  89. function Timer.TimerUpdate()
  90.     if Timer.stop == true then
  91.         EVENT_MANAGER:UnregisterForUpdate("RAID_TIMER_UPDATE")
  92.         return
  93.     end
  94.  
  95.     local currentTime = GetTimeStamp()
  96.     local raidDuration = GetDiffBetweenTimeStamps(currentTime, Timer.raidStart)
  97.     local formatedTime = ZO_FormatTime(raidDuration, TIME_FORMAT_STYLE_COLONS, TIME_FORMAT_PRECISION_SECONDS)
  98.     RAIDSCORETimer:SetText(zo_strformat("Total Time: <<1>>", formatedTime))
  99. end
  100.  
  101.  
  102. function Timer.TimerStart()
  103.     Timer.stop = false
  104.     Timer.instance = true
  105.  
  106.     if Timer.savedVariables.raidStart then
  107.         Timer.raidStart = savedVariables.raidStart
  108.     else
  109.         Timer.raidStart = GetTimeStamp()
  110.         savedVariables.raidStart = Timer.raidStart
  111.     end
  112.  
  113.  
  114.  
  115.     Timer.TimerUpdate()
  116.     EVENT_MANAGER:RegisterForUpdate("RAID_TIMER_UPDATE", 1000, Timer.TimerUpdate)
  117. end
  118.  
  119. function Timer.TimerStop(event, trialName, score, totalTime)
  120.     Timer.stop = true
  121.     Timer.instance = false
  122.  
  123.     local template = "Trial |cFFFFFF<<1>>|r completed in |cFFFFFF<<2>>|r, score: |cFFFFFF<<3>>|r."
  124.     if totalTime == nil then
  125.         local currentTime = GetTimeStamp()
  126.         totalTime = GetDiffBetweenTimeStamps(currentTime, Timer.raidStart) * 1000
  127.         template = "Trial |cFFFFFF<<1>>|r failed. Total time: |cFFFFFF<<2>>|r, score: |cFFFFFF<<3>>|r."
  128.     end
  129.  
  130.     local formatedTime = ZO_FormatTimeMilliseconds(totalTime, TIME_FORMAT_STYLE_COLONS, TIME_FORMAT_PRECISION_SECONDS)
  131.     d(zo_strformat(template, trialName, formatedTime, score))
  132.  
  133.     Timer.raidStart = nil
  134.     savedVariables.raidStart = nil
  135. end
  136.    
  137. local RS = {
  138.     Menu = LibStub:GetLibrary("LibAddonMenu-2.0"),
  139.     hideScore = false,
  140.     lockScore = false,
  141.     }
  142.  
  143. ------Creating the menu with LibAddonMenu-2.0-------   
  144.     RS.panelData = {
  145.         type = "panel",
  146.         name = "RaidScore",
  147.         author = "Woeler",
  148.         version = "1.1.3",
  149.         registerForRefresh = false,
  150.         }
  151.        
  152.    
  153.     RS.optionsData = {
  154.         [1] = {
  155.         type = "description",
  156.         text = "Welcome to RaidScore by Woeler (EU: @Woeler)",
  157.         width = "full",
  158.         },
  159.         [2] = {
  160.             type = "checkbox",
  161.             name = "Show RaidScore",
  162.             tooltip = "Toggle RaidScore visible.",
  163.             getFunc = function() return not savedVariables.HideScores end,
  164.             setFunc = function(value)
  165.                 --RS.hideScore = not value
  166.                 savedVariables.HideScores = not value
  167.                 RAIDSCORE:SetHidden(savedVariables.HideScores)
  168.             end,
  169.         },
  170.         [3] = {
  171.             type = "checkbox",
  172.             name = "Lock window",
  173.             tooltip = "Lock the RaidScore window in place.",
  174.             getFunc = function() return not savedVariables.lockWindow end,
  175.             setFunc = function(value)
  176.                 savedVariables.lockWindow = not value
  177.                 RAIDSCORE:SetMovable(savedVariables.lockWindow)
  178.             end,
  179.         },
  180.         [4] = {
  181.             type = "checkbox",
  182.             name = "Post endscore in chat",
  183.             tooltip = "When a trial is completed post the endscore in the chat.",
  184.             getFunc = function() return savedVariables.postResults end,
  185.             setFunc = function(value)
  186.                 savedVariables.postResults = value
  187.             end,
  188.         },
  189.  
  190.  
  191.         width = "full",
  192.        
  193.     },
  194.    
  195.  
  196.     RS.Menu:RegisterAddonPanel("RaidScorePanel", RS.panelData)
  197.     RS.Menu:RegisterOptionControls("RaidScorePanel", RS.optionsData)
  198.    
  199.  
  200.  
  201.     EVENT_MANAGER:RegisterForEvent(MoveHandler, EVENT_ADD_ON_LOADED, OnAddonLoaded)
  202.     EVENT_MANAGER:RegisterForEvent("RAID_SCORE_POST", EVENT_RAID_TRIAL_COMPLETE, PostEndScore)
  203.     EVENT_MANAGER:RegisterForEvent("RAID_TIMER_UPDATE", EVENT_RAID_TRIAL_SCORE_UPDATE, RaidScoreUpdate)
  204.     EVENT_MANAGER:RegisterForEvent("RAID_TIMER_START", EVENT_RAID_TRIAL_STARTED, Timer.TimerStart)
  205.     EVENT_MANAGER:RegisterForEvent("RAID_TIMER_STOP", EVENT_RAID_TRIAL_COMPLETE, Timer.TimerStop)
  206.     EVENT_MANAGER:RegisterForEvent("RAID_TIMER_STOP", EVENT_RAID_TRIAL_FAILED, Timer.TimerStop)

Last edited by Woeler : 03/27/15 at 11:29 AM.
  Reply With Quote
03/27/15, 11:27 AM   #6
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 4,963
Where is the variable RT coming from?
See your function OnAddonLoaded...

if IsRaidInProgress() then
RT.TimerStart()
else
RT.savedVariables.raidStart = nil
end


It is not declared in this source code. It's a typo and you mean RS?

I guess you want it to be Timer.TimerStart()
  Reply With Quote
03/27/15, 11:38 AM   #7
Woeler
 
Woeler's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2014
Posts: 40
Originally Posted by Baertram View Post
Where is the variable RT coming from?
See your function OnAddonLoaded...

if IsRaidInProgress() then
RT.TimerStart()
else
RT.savedVariables.raidStart = nil
end


It is not declared in this source code. It's a typo and you mean RS?

I guess you want it to be Timer.TimerStart()
Copy paste is Copy waste... That is what I learned now... Thanks! I was also calling the update method from the XML, which wasn't a good idea :P

Now fixed, it works!
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » Simple start/stop timer


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