Thread Tools Display Modes
05-09-13, 04:53 PM   #1
10leej
A Molten Giant
 
10leej's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2011
Posts: 583
DBM countdown via lua

I currently use this script I found to run a coundown timer, but I'm wondering if there's a way I can call DBM's countdown function with this script as well

Lua Code:
  1. local defaultcdtime = 6
  2. local channel = "RAID_WARNING"
  3.  
  4. local frame = CreateFrame("frame", nil)
  5. SlashCmdList['COUNTDOWN'] = function(newtime)
  6.     if newtime ~= "" then
  7.         cdtime = newtime+1
  8.     else
  9.         cdtime = defaultcdtime+1
  10.     end  
  11.     local ending = false
  12.     local start = floor(GetTime())
  13.     local throttle = cdtime
  14.     frame:SetScript("OnUpdate", function()
  15.         if ending == true then return end
  16.         local countdown = (start - floor(GetTime()) + cdtime)
  17.         if (countdown + 1) == throttle and countdown >= 0 then
  18.             if countdown == 0 then
  19.                 SendChatMessage('Pulling', channel)
  20.                 throttle = countdown
  21.                 ending = true
  22.             else
  23.                 SendChatMessage(countdown, channel)
  24.                 throttle = countdown
  25.             end
  26.         end
  27.     end)
  28. end
  29. SLASH_COUNTDOWN1 = "/inc"
__________________
Tweets YouTube Website
  Reply With Quote
05-09-13, 06:01 PM   #2
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Assuming you can start a DBM countdown timer via slash command, just look in DBM's files for where it defines that slash command -- search for "SlashCmdList" -- and then just call the function with the appropriate arguments, eg.

Code:
SlashCmdList["DBM_TIMER"]("custom 60")
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
05-12-13, 02:20 PM   #3
Aanson
A Flamescale Wyrmkin
Join Date: Aug 2009
Posts: 124
Originally Posted by 10leej View Post
I currently use this script I found to run a coundown timer, but I'm wondering if there's a way I can call DBM's countdown function with this script as well

Lua Code:
  1. local defaultcdtime = 6
  2. local channel = "RAID_WARNING"
  3.  
  4. local frame = CreateFrame("frame", nil)
  5. SlashCmdList['COUNTDOWN'] = function(newtime)
  6.     if newtime ~= "" then
  7.         cdtime = newtime+1
  8.     else
  9.         cdtime = defaultcdtime+1
  10.     end  
  11.     local ending = false
  12.     local start = floor(GetTime())
  13.     local throttle = cdtime
  14.     frame:SetScript("OnUpdate", function()
  15.         if ending == true then return end
  16.         local countdown = (start - floor(GetTime()) + cdtime)
  17.         if (countdown + 1) == throttle and countdown >= 0 then
  18.             if countdown == 0 then
  19.                 SendChatMessage('Pulling', channel)
  20.                 throttle = countdown
  21.                 ending = true
  22.             else
  23.                 SendChatMessage(countdown, channel)
  24.                 throttle = countdown
  25.             end
  26.         end
  27.     end)
  28. end
  29. SLASH_COUNTDOWN1 = "/inc"
If I were you, I'd maybe make a slight change to your existing code which you posted:

Lua Code:
  1. local defaultcdtime = 6
  2. local channel = "RAID_WARNING"
  3.  
  4. local frame = CreateFrame("frame", nil)
  5.  
  6. SlashCmdList['COUNTDOWN'] = function(newtime)
  7.     local cdtime = newtime and ( newtime + 1 ) or ( defaultcdtime + 1 );
  8.     local throttle, ending, start, msg = cdtime, false, floor(GetTime()), "";
  9.     frame:SetScript("OnUpdate", function()
  10.         if ending then
  11.             frame:SetScript("OnUpdate", nil);
  12.             return;
  13.         end
  14.         local countdown = ( start - floor(GetTime()) + cdtime );
  15.         if ( ( ( countdown + 1 ) == throttle ) and ( countdown >= 0 ) ) then
  16.             ending = ( countdown == 0 );
  17.             throttle = countdown;
  18.             msg = ( countdown == 0 ) and "Pulling" or countdown;
  19.             SendChatMessage(msg, channel);
  20.         end
  21.     end);
  22. end
  23. SLASH_COUNTDOWN1 = "/inc"

The only part changed is where the function returns out if var ending is true. The way it is, it'll check the value of ending every time your frames are drawn which can be up to 100 times per second.

I've changed it so that instead of it just returning out time and time again, it removes the function from the OnUpdate code altogether. When the function is called, it auto re-creates the function anyway.

I tidied it up a bit while I was at it.
__________________
__________________
  Reply With Quote
05-12-13, 05:34 PM   #4
10leej
A Molten Giant
 
10leej's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2011
Posts: 583
Ah why thank you didn't think a frame was drawn that much.
__________________
Tweets YouTube Website
  Reply With Quote
05-12-13, 06:42 PM   #5
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Frames are drawn based on your framerate. There is a setting in the options to cap it at 60fps. I've gotten close to 200fps before, without the cap. During a raid boss fight, most people see around 30 or so.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
05-14-13, 12:00 AM   #6
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Creating a new OnUpdate function every time you run the command is fairly wasteful. You should also use the elapsed value passed to your OnUpdate function, instead of calling GetTime() repeatedly.

Code:
local pull, seconds, onesec

local frame = CreateFrame("Frame")
frame:Hide()
frame:SetScript("OnUpdate", function(self, elapsed)
    onesec = onesec - elapsed
    pull = pull - elapsed
    if pull <= 0 then
        SendChatMessage("Pulling!", "RAID_WARNING")
        self:Hide()
    elseif onesec <= 0 then
        SendChatMessage(seconds, "RAID_WARNING")
        seconds = seconds - 1
        onesec = 1
    end
end)

SlashCmdList["COUNTDOWN"] = function(t)
    t = tonumber(t) or 6

    pull = t + 1
    seconds = t
    onesec = 1

    frame:Show()
end

SLASH_COUNTDOWN1 = "/inc"
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
05-15-13, 11:30 AM   #7
10leej
A Molten Giant
 
10leej's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2011
Posts: 583
Well gosh, glad you guys took to helping me optimize it

Also looks like
Code:
SlashCmdList["DEADLYBOSSMODS"]("pull ".. seconds)
Will simulate the /pull command for DBM

So the current script stands at
Lua Code:
  1. local pull, seconds, onesec
  2. local frame = CreateFrame("Frame")
  3.  
  4. frame:Hide()
  5. frame:SetScript("OnUpdate", function(self, elapsed)
  6.     --Start DBM pull timer
  7.     onesec = onesec - elapsed
  8.     pull = pull - elapsed
  9.     if pull <= 0 then
  10.         SendChatMessage("Pulling!", "SAY")
  11.         self:Hide()
  12.     elseif onesec <= 0 then
  13.         SendChatMessage(seconds, "SAY")
  14.         seconds = seconds - 1
  15.         onesec = 1
  16.     end
  17.     if IsAddOnLoaded("DBM-Core") then
  18.         SlashCmdList["DEADLYBOSSMODS"]("pull ".. seconds)
  19.     end
  20. end)
  21.  
  22. SlashCmdList["COUNTDOWN"] = function(t)
  23.     t = tonumber(t) or 6
  24.     pull = t + 1
  25.     seconds = t
  26.     onesec = 1
  27.     frame:Show()
  28. end
  29.  
  30. SLASH_COUNTDOWN1 = "/inc"

course running that will cause 50 million DBM timer bars
__________________
Tweets YouTube Website

Last edited by 10leej : 05-15-13 at 04:53 PM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » DBM countdown via lua

Thread Tools
Display Modes

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