WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Play a sound file when you're mounted? (https://www.wowinterface.com/forums/showthread.php?t=14773)

Stonedpossum 01-31-08 10:43 PM

Play a sound file when you're mounted?
 
Hey,

I want to make an addon that plays "On the road again" when you mount. Then stops when you're unmounted, I want it to work it all kinds of mounts.

Now, how would I go about doing this?
Could someone help me?

I've never made an addon before, I just think that sounds like an awesome idea.

Thanks!

Sythalin 02-01-08 12:23 AM

Really simple. Just write a simple addon to check "IsMounted()" and "not IsMounted()".

www.wowwiki.com

ProfOak 02-01-08 08:58 AM

Hi!

You can make it like this:

-- the file that will call your files:
MyAddon.toc
-- add the next code line
MyAddon.lua

-- the file that will contain the code:
MyAddon.lua

-- add the following code:

-- create your addon's name space
MyAddon = {}
-- create a simple frame
local frame = CreateFrame("Frame");

-- register event, might be another better to make this but this one was the nicest I've found
frame:RegisterEvent("PLAYER_AURAS_CHANGED");

-- set the trigger to catch the event and call your function
frame:SetScript("OnEvent", function(this, event, ...)
MyAddon[event](MyAddon, ...) end; );

-- your function
function MyAddon:PLAYER_AURAS_CHANGED()
if(IsMounted()) then
-- since this event triggers so many times, we better declare anything inside the case we want
local soundFile = "path";
-- path is where your music will be
-- Ex:Interface\\AddOns\\YourAddon\\YourMusic.mp3
PlayMusic(soundFile);
else
StopMusic();
end;
end;

I didnt test this, so no clue if is working. One drawback is that else clause, it will trigger the stopmusic() everytime.
There might be a way to get from chat msgs the right place to put the ismount and the else clause.

Anyway, I hope it helps.

Cheers

Shirik 02-01-08 10:13 AM

Quote:

Originally Posted by ProfOak (Post 82521)
-- set the trigger to catch the event and call your function
frame:SetScript("OnEvent", function(this, event, ...)
MyAddon[event](MyAddon, ...) end; );

While there's nothing wrong with what you did, I strongly advise that you use "self" instead of "this" when making these types of functions. This fits the standard Lua syntax, and you'll get less confusion when others are reading your code (and may save yourself some confusion as well).

ProfOak 02-01-08 10:48 AM

Thanks for the tip Shirik :P

Stonedpossum, I forgot to add some important info that you'll need in your toc file:

MyAddon.toc
## Interface: 20300
## Title: My Addon
## Notes: This is my first AddOn.
MyAddon.lua


The explanation:
## Interface: 20300 -- is were u type current game version
## Title: My Addon -- the name that ppl using ur addon will see in the addon options
## Notes: This is my first AddOn. -- info
-- the next session is where u put the files to be loaded, .lua and/or .xml

Now the explanation is complete ^^

If u into the addon development, check foruns here and http://www.wowwiki.com/

Cheers :P

Stonedpossum 02-01-08 01:30 PM

Thanks guys.

Now, one more question, can I just put this all in notepad and save it as the type of files needed, or do I need to download a program?

Slakah 02-01-08 03:04 PM

Quote:

Originally Posted by Stonedpossum (Post 82529)
Thanks guys.

Now, one more question, can I just put this all in notepad and save it as the type of files needed, or do I need to download a program?

Notepad/wordpad will do what you want.

Stonedpossum 02-01-08 05:40 PM

Hey. Sorry for all the questions, I feel so dumb. Haha, but this is my very first time doing any sort of programing and making WoW addons, so anyway, I did this:

Code:

MyAddon = {OnTheRoad}
local frame = CreateFrame("Frame");
frame:RegisterEvent("PLAYER_AURAS_CHANGED");
frame:SetScript("OnEvent", function(this, event, ...)
OnTheRoad[event](OnTheRoad, ...) end; );
function MyAddon:PLAYER_AURAS_CHANGED()
if(IsMounted())
local soundFile = "Interface\\AddOns\\OnTheRoad\\music.mp3";
PlayMusic(music.mp3);
else
StopMusic();
end;
end;

It didn't work... haha... can someone tell me what I'm doing wrong. I bet it's something really easy :P

That's my Addon.lua file. The addon comes up on the list, but doesn't do anything.

Also, is it best to make an addon in Lua or XML?

Slakah 02-01-08 05:55 PM

Quote:

Originally Posted by Stonedpossum (Post 82545)
Hey. Sorry for all the questions, I feel so dumb. Haha, but this is my very first time doing any sort of programing and making WoW addons, so anyway, I did this:

Code:

MyAddon = {OnTheRoad}
local frame = CreateFrame("Frame");
frame:RegisterEvent("PLAYER_AURAS_CHANGED");
frame:SetScript("OnEvent", function(this, event, ...)
OnTheRoad[event](OnTheRoad, ...) end; );
function MyAddon:PLAYER_AURAS_CHANGED()
if(IsMounted())
local soundFile = "Interface\\AddOns\\OnTheRoad\\music.mp3";
PlayMusic(music.mp3);
else
StopMusic();
end;
end;

It didn't work... haha... can someone tell me what I'm doing wrong. I bet it's something really easy :P

I would personally do

Addon.lua
Code:

local f = CreateFrame("Frame")
f:RegisterEvent("PLAYER_AURAS_CHANGED");
f:SetScript("OnEvent", function()
  if IsMounted() then
      PlayMusic("Interface\\AddOns\\OnTheRoad\\music.mp3")
  end
end)

Addon.toc
Code:

## Interface: 20300
## Title: My Addon
## Notes: This is my first AddOn.
Addon.lua

Place both of these in a folder named Addon.


replace "Interface\\AddOns\\OnTheRoad\\music.mp3" with the location of the mp3 you want to use.

Stonedpossum 02-01-08 05:59 PM

Don't I want it to stop the music when you unmount?

And also, what's wrong with the where the music file is?

Because that's where it is...

erica647 02-01-08 06:24 PM

Stonedpossum... I love that nic! lol

Icecoldcoke 02-01-08 06:28 PM

Quote:

Originally Posted by Stonedpossum (Post 82550)
Don't I want it to stop the music when you unmount?

And also, what's wrong with the where the music file is?

Because that's where it is...


Code:

local f = CreateFrame("Frame")
f:RegisterEvent("PLAYER_AURAS_CHANGED");
f:SetScript("OnEvent", function()
  if IsMounted() then
      PlayMusic("Interface\\AddOns\\OnTheRoad\\music.mp3")
  else
      StopMusic()
  end
end)


Stonedpossum 02-01-08 11:20 PM

Hey, ok I think it worked. I logged on when i was mounted and it was playing, but when I unmounted and and remounted it didn't play. How can I make it play every time?

Addon.lua:
Code:

MyAddon = {OnTheRoad}
local frame= CreateFrame("Frame")
frame:RegisterEvent("PLAYER_AURAS_CHANGED");
frame:SetScript("OnEvent", function()
  if IsMounted() then
      PlayMusic("Interface\\AddOns\\OnTheRoad\\music.mp3")
  else
      StopMusic()
  end
end)

OnTheRoad.toc
Code:

# Interface: 20300
## Title: OnTheRoad
## Notes: Plays on the road when you're mounted. Pure awesome!
Addon.lua

UPDATE:
Ok, here's something weird. It doesn't work if you mount, but it plays the music when you run into a building and get unmounted... WTF?!? Help please...

Another UPDATE:
Now it's just playing when I log on mounted... I'm so confused!

Slakah 02-02-08 05:54 AM

Quote:

MyAddon = {OnTheRoad}
Remove that it's not doing anything.

I'm not certain but maybe "PLAYER_AURAS_CHANGED" is called before IsMounted() will return true. If so try

Code:


local frame= CreateFrame("Frame")
frame:RegisterEvent("PLAYER_AURAS_CHANGED")
frame:SetScript("OnEvent", function(frame)
    local t = 0
    frame:SetScript("OnUpdate", function(_, elapsed)
        t = t + elapsed
        if t >= 1 then
            frame:SetScript("OnUpdate", nil)
            if IsMounted() then
                PlayMusic("Interface\\AddOns\\OnTheRoad\\music.mp3")
            else 
                StopMusic()
            end
        end
end)


mulesh 02-02-08 06:30 AM

The value for IsMounted() is indeed delayed during PLAYER_AURAS_CHANGED. The event fires too fast. I came across this problem myself when creating the PlaySound addon. Using an OnUpdate handler is the best way to go.

Stonedpossum 02-02-08 12:24 PM

Gah! Ok, I have,

Code:

local frame= CreateFrame("Frame")
frame:RegisterEvent("PLAYER_AURAS_CHANGED")
frame:SetScript("OnEvent", function(frame)
    local t = 0
    frame:SetScript("OnUpdate", function(_, elapsed)
        t = t + elapsed
        if t >= 1 then
            frame:SetScript("OnUpdate", nil)
            if IsMounted() then
                PlayMusic("Interface\\AddOns\\OnTheRoad\\music.mp3")
            else 
                StopMusic()
            end
        end
end)

But it still doesn't even work. :(

Slakah 02-02-08 01:04 PM

Quote:

Originally Posted by Stonedpossum (Post 82578)
Gah! Ok, I have,

Code:

local frame= CreateFrame("Frame")
frame:RegisterEvent("PLAYER_AURAS_CHANGED")
frame:SetScript("OnEvent", function(frame)
    local t = 0
    frame:SetScript("OnUpdate", function(_, elapsed)
        t = t + elapsed
        if t >= 1 then
            frame:SetScript("OnUpdate", nil)
            if IsMounted() then
                PlayMusic("Interface\\AddOns\\OnTheRoad\\music.mp3")
            else 
                StopMusic()
            end
        end
end)

But it still doesn't even work. :(


put
Code:

DEFAULT_CHAT_FRAME:AddMessage("Stopping music")
after StopMusic() to find out if its IsMounted() and
Code:

DEFAULT_CHAT_FRAME:AddMessage("Playing music")
after PlayMusic() then try mounting up. If you see Playing music printed then it means PlayMusic() is failing else its IsMounted try playing around with the value in
Code:

t >= 1
.

Is Music enabled, in the sound options? And does <Your addon> appear in the addon list?

Stonedpossum 02-02-08 01:19 PM

Code:

local frame= CreateFrame("Frame")
frame:RegisterEvent("PLAYER_AURAS_CHANGED")
frame:SetScript("OnEvent", function(frame)
    local t = 0
    frame:SetScript("OnUpdate", function(_, elapsed)
        t = t + elapsed
        if t >= 1 then
            frame:SetScript("OnUpdate", nil)
            if IsMounted() then
                PlayMusic("Interface\\AddOns\\OnTheRoad\\music.mp3") DEFAULT_CHAT_FRAME:AddMessage("Playing music")
            else 
                StopMusic() DEFAULT_CHAT_FRAME:AddMessage("Stopping music")
            end
        end
end)

No message pops up, still no sound.

Yes, my sound is on.

Yes, it does show up on the list.

and you said something about changing the "t >= 1" what would I even change it to? I mean, what does that even mean...

Sorry for being so stupid when it comes to this programing stuff, never done it before.

mulesh 02-02-08 03:59 PM

Change it to this and see if it pops up any messages:
Code:

local frame= CreateFrame("Frame")
frame:RegisterEvent("PLAYER_AURAS_CHANGED")
frame:SetScript("OnEvent", function(frame)
    local t = 0
    frame:SetScript("OnUpdate", function(_, elapsed)
        t = t + elapsed
        if t >= 1 then
            frame:SetScript("OnUpdate", nil)
            if IsMounted() then
                PlayMusic("Interface\\AddOns\\OnTheRoad\\music.mp3")
                DEFAULT_CHAT_FRAME:AddMessage("Playing music")
            else 
                StopMusic()
                DEFAULT_CHAT_FRAME:AddMessage("Stopping music")
            end
        end
end)

To answer your question, the OnUpdate script is called by the game every time the screen refreshes (i.e. 30fps means 30 calls per second). The "elapsed" variable is a value for how much time has passed since the last OnUpdate call. Setting the timer to zero (local t = 0) and then we keep adding the value of "elapsed" to it (t = t + elapsed) until the timer equals 1 second or more (t >= 1). Once it equals 1 or more seconds we check if the player is mounted or not. By changing the t >= 1 line to something like t >= 2 would cause it to check for player mount every 2 seconds.

Not to put a stopper to your learning experience, but you could just use my PlaySound addon (shameless plug, whoohoo!!).

Good luck dood
:)

ProfOak 02-04-08 08:51 AM

Hi.

First on my original post, I put a line with:

MyAddon = {}

and your using MyAddon = {OnTheRoad}, which is wrong.

MyAddon = {} -- is used to create an empty table. You shouldn't put anything inside curly bracets.

So, pls copy and paste the code as is and then test it.

I also found this events that you can try to test and see if they fit best:

CHAT_MSG_SPELL_PERIODIC_FRIENDLYPLAYER_BUFFS
Check arg1 for "mount" and/or "mounted" strings.

COMBAT_TEXT_UPDATE
Check arg1 for "AURA_START", "AURA_END", "AURA_START_HARMFUL",
"AURA_END_HARMFUL" strings.

UNIT_AURA("player")
Use this instead of "PLAYER_AURAS_CHANGED"


MyAddon:UNIT_AURA(arg1, ...)
if( IsMounted() and arg1 = "player" ) then
....


U need to do some work here, I'm @ work and cant test it :D
sssshhhhh dont tell my boss

--------------------------------------------------------------------------
I like MyAddon = {} for 2 reasons, readability and u can add more functions if u like by using MyAddon:NEW_FUNCTION(args), avoinding the polution of the global enviroment and possible confiltcs between addons fucntions.
But this is just my point of view, u should follow whatever u feel it fits best ur porposes.
--------------------------------------------------------------------------

Hope I could give u a help

Cheers


All times are GMT -6. The time now is 11:25 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI