Skip to content

Instantly share code, notes, and snippets.

@Choonster
Created August 30, 2012 15:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Choonster/3531056 to your computer and use it in GitHub Desktop.
Save Choonster/3531056 to your computer and use it in GitHub Desktop.
A WoW AddOn that plays music when you're stealthed and moving.
-- Declare a local variable called something like IS_PLAYING (we use this a bit later) and set it to false.
-- Create a frame
-- Hide it
-- Register whatever event(s) fire when the player enters/leaves stealth
-- Set its OnEvent script to show the frame when the player enters stealth (allowing its OnUpdate script to run) and hide the frame, stop the music and set IS_PLAYING to false when the player leaves stealth (stopping its OnUpdate script from running).
-- Set its OnUpdate script to check the player's speed every 0.X seconds.
-- If the player is moving and IS_PLAYING is false, use PlayMusic to start your music and set IS_PLAYING to true.
-- If the player isn't moving and IS_PLAYING is true, use StopMusic to stop the music and set IS_PLAYING to false.
-- http://us.battle.net/wow/en/forum/topic/6412105149#4
local MUSIC_PATH = "Interface\\AddOns\\StealthMusic\\music.mp3"
local IS_PLAYING = false
local function PlayStealthMusic()
PlayMusic(MUSIC_PATH)
IS_PLAYING = true
end
local function StopStealthMusic()
StopMusic()
IS_PLAYING = false
end
local f = CreateFrame("Frame")
f:Hide()
f:RegisterEvent("UPDATE_STEALTH")
f:SetScript("OnEvent", function(self, event, ...)
if IsStealthed() then -- We're in stealth, start the OnUpdate script
self:Show()
else -- We're not in stealth, stop the OnUpdate script and the music.
self:Hide()
StopStealthMusic()
end
end)
local THROTTLE = 0.1 -- How many seconds to wait before running the OnUpdate
local timer = THROTTLE
f:SetScript("OnUpdate", function(self, elapsed)
timer = timer - elapsed
if timer > 0 then return end -- Don't run the code until at least THROTTLE seconds have passed
timer = THROTTLE -- Reset the timer
local speed = GetUnitSpeed("player")
if speed == 0 and IS_PLAYING then -- We're not moving but the music is still playing, so stop it.
StopStealthMusic()
elseif speed > 0 and not IS_PLAYING then -- We're moving but the music isn't playing, so start it.
PlayStealthMusic()
end
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment