Classic Wow - Play Sound based on built-in stopwatch timer value

Hello everyone!

I’m trying to write function which will play custom alert sound, based on Blizzard’s built-in stopwatch timer:

-- Init
local dgframe = CreateFrame("Frame")
dgframe:SetScript("OnEvent", function(self, event, a1)
  if event == "PLAYER_LOGIN" then print("Use /dg to toggle on/off");
  end
end)
dgframe:RegisterEvent("PLAYER_LOGIN")

-- Reset timer on key input
local function timeres(self, key)
   StopwatchTicker.timer = 0;
   if StopwatchTicker.timer == 5 then
   	PlaySoundFile("Interface\\AddOns\\DG\\horn.mp3");
   end
end
 
dgframe:SetScript("OnKeyDown", timeres)
dgframe:SetPropagateKeyboardInput(true)

--toggle on/off
SLASH_DGTOGGLE1 = "/dg";

function SlashCmdList.DGTOGGLE(msg, editBox)
	Stopwatch_Toggle();
	Stopwatch_Play();
end

if statement works, when both “StopwatchTicker.timer” values within the “timeres” function are the same, so I assume it needs some type of “OnUpdate” function to read timer in real time after it gets reset on key press. This is my first time coding an addon, so I’m absolutely lost. Could you point me to the right direction? Thanks :slight_smile:

I suggest you use a different approach, but we could still help you with your code if you prefer that

You can use /sw 5 to countdown and then you could replace the function with one that plays your custom sound
https://github.com/Gethe/wow-ui-source/blob/live/AddOns/Blizzard_TimeManager/Blizzard_TimeManager.lua#L630-L633

local function OnEvent(self, event, addon)
	if addon == "Blizzard_TimeManager" then
		function Stopwatch_FinishCountdown()
			Stopwatch_Clear()
			PlaySoundFile("Interface\\AddOns\\DG\\horn.mp3")
		end
		self:UnregisterEvent(event)
	end
end

local f = CreateFrame("Frame")
f:RegisterEvent("ADDON_LOADED")
f:SetScript("OnEvent", OnEvent)

Thanks for taking time to help me. From what I’ve checked /sw is not available for Classic WoW UI, since it was added in 2.4.3 as it seems:
https://wow.gamepedia.com/Patch_2.4.3#User_Interface

Nevertheless I was able to run countdown via:
Stopwatch_StartCountdown(0, 0, 30); Stopwatch_Play()

This alters the logic of the addon that I had in mind originally, but works so thank you again!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.