Thread Tools Display Modes
01-09-10, 08:38 PM   #1
opismahname
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 23
First Addon - need help

Hey there, I'm making my first AddOn that will be a real simple one. The thing i want this AddOn to do is to print "Rigtheous Fury is now activated" in the chat window when you cast Righteous Fury.

I've been reading alot about how to do something like this but still can't get it...

The code I am doing is like this (i know, i suck at this, but it's like my first time):
Code:
local frame = CreateFrame("FRAME", "RFFrame");
frame:RegisterEvent("UNIT_AURA_PLAYER_RIGHTEOUS FURY");
local function eventHandler(self, event, ...)
 print("Rigtheous Fury is now activated " .. event);
end
frame:SetScript("OnEvent", eventHandler);
I know that this isn't right at all, but don't know what to do

Really need help...
  Reply With Quote
01-09-10, 09:00 PM   #2
Akryn
A Firelord
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 479
You are pretty close:

"UNIT_AURA_PLAYER_RIGHTEOUS FURY" is not an event. "UNIT_AURA" is, and that's all you should put there.

Therefore, in your OnEvent function, you need to check for...
1. Is this event firing for the player
2. If so, does the player currently have the aura.
3. If so, did the player not have it the last time I checked -- to prevent the addon from spamming every time you gain/loose an aura while that one is active.

The way to do #1 is to check the event's arg1, which is the unit the event fired for e.g.
Code:
if ... == "player" then
The way to do #2 is with UnitAura e.g.
Code:
UnitAura("player", "Righteous Fury")
The way to do #3 is to save the result of each check in a variable, and check it before announcing.
  Reply With Quote
01-09-10, 09:15 PM   #3
opismahname
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 23
Originally Posted by Akryn View Post
You are pretty close:

"UNIT_AURA_PLAYER_RIGHTEOUS FURY" is not an event. "UNIT_AURA" is, and that's all you should put there.

Therefore, in your OnEvent function, you need to check for...
1. Is this event firing for the player
2. If so, does the player currently have the aura.
3. If so, did the player not have it the last time I checked -- to prevent the addon from spamming every time you gain/loose an aura while that one is active.

The way to do #1 is to check the event's arg1, which is the unit the event fired for e.g.
Code:
if ... == "player" then
The way to do #2 is with UnitAura e.g.
Code:
UnitAura("player", "Righteous Fury")
The way to do #3 is to save the result of each check in a variable, and check it before announcing.
Thanks for answering!
Don't really know how to do this work but something like this?
Code:
local frame = CreateFrame("FRAME", "RFFrame");
frame:RegisterEvent("UNIT_AURA");
local function eventHandler(self, event, ...)
 print("Rigtheous Fury is now activated " .. event);
end
frame:SetScript("OnEvent *something here?*", eventHandler);
Or could you post how the entire code would be?
  Reply With Quote
01-09-10, 09:25 PM   #4
Akryn
A Firelord
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 479
Originally Posted by opismahname View Post
Or could you post how the entire code would be?
I would be happy to if you'd like; although wouldn't that invalidate the point of trying to make your own addon?

Otherwise:

What you want to do is put some checks in your OnEvent function itself, the function that you call eventHandler.
Code:
frame:SetScript("OnEvent", eventHandler)
and
Code:
frame:RegisterEvent("UNIT_AURA");
are both already correct as they are. You need to put some logic in eventHandler to detect the three things I mentioned above:
Code:
local function eventHandler(self, event, ...)
--some stuff goes here
--your existing code
end
  Reply With Quote
01-09-10, 09:56 PM   #5
opismahname
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 23
Hmm... Think I at least get a little bit of what you wrote.

Will it be like this?

Code:
local frame = CreateFrame("FRAME", "RFFrame");
frame:RegisterEvent("UNIT_AURA");
local function eventHandler(self, event, ...)
 print("Rigtheous Fury is now activated " .. event);
end
frame:SetScript("OnEvent", eventHandler);
if Rigtheous Fury (or is it supposed to be...?) == "player" then
**Didnt't get "--your existing code, or do you mean everything that i already have written?"**
end
Haven't coded like this or something before so I am really bad at this :P
  Reply With Quote
01-09-10, 10:17 PM   #6
Akryn
A Firelord
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 479
What I mean is, that what you need to add is inside the function e.g.
Code:
local frame = CreateFrame("FRAME", "RFFrame");
frame:RegisterEvent("UNIT_AURA");
local function eventHandler(self, event, ...)
 --ADD YOUR CODE HERE
 print("Rigtheous Fury is now activated " .. event);
end
frame:SetScript("OnEvent", eventHandler);
Given what you've posted so far, you might want to start with something like http://www.wowwiki.com/AddOn_program...l/Introduction
  Reply With Quote
01-10-10, 07:25 AM   #7
opismahname
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 23
Have been reading that tutorial before, and I created the HelloWorld addon :P

Just don't know what i should put in --ADD YOUR CODE HERE, but I know that the code i put in here will get the addon working and that.
  Reply With Quote
01-10-10, 07:52 AM   #8
ravagernl
Proceritate Corporis
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 1,176
First what you would need to do is check if the given unit by UNIT_AURA is 'player'. This event runs pretty often, and for each unit.

lua Code:
  1. local unit = ... -- select the first argument from arguments passed
  2. if unit ~= 'player' then return end -- run code only for player unit

Using return will skip out of the function stopping any processing of code. It's just a way of avoiding too many nested if then end's.

Then you know it's the player that gained or lost a buff. You want to see if the player has gained righteous fury. To see that, we use UnitBuff:

lua Code:
  1. local active = UnitBuff('player', 'Righteous Fury')
  2. if active then
  3.     print('Righteous Fury has been activated')
  4. end
or even
lua Code:
  1. if UnitBuff('player', 'Righteous Fury') then
  2.     print('Righteous Fury has been activated')
  3. end

But what will happen, is that will fire off every time you get or loose an aura, resulting in chat spam. So we solve that with a variable called 'hasrf':
lua Code:
  1. local active = UnitBuff('player', 'Righteous Fury')
  2. if not hasrf and active then -- we gained righteous fury as we did not have it before and it's active now
  3.     hasrf = true
  4.     print('Righteous Fury has been activated')
  5. elseif hasrf and not active then -- we lost righteous fury as we did have it before and it's no longer active now
  6.     hasrf = false
  7. end
Be sure to define 'hasrf' as a local variable outside your eventhandler function
  Reply With Quote
01-10-10, 11:51 AM   #9
opismahname
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 23
Thanks for all answers.

I tried this code, but isn't working:

Code:
local frame = CreateFrame("FRAME", "RFFrame");
frame:RegisterEvent("UNIT_AURA");
local function eventHandler(self, event, ...)
local active = UnitBuff('player', 'Righteous Fury')
if not hasrf and active then
    hasrf = true
    print('Rigtheous Fury has been activated')
elseif hasrf and not active then
    hasrf = false
end
frame:SetScript("OnEvent", eventHandler);
I'm pretty sure that I missed something that you wrote.

And some other questions too...
if i want to create a frame that shows "RF is not active", and when its active it shows "RF is active", in a frame like WinterTimer has, that is showing all the time, do i need to do this in the xml file, and is it hard?

Second question, what program are you guys coding in? Cause im just using the WordPad xD
  Reply With Quote
01-10-10, 12:14 PM   #10
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
You're missing an end in the code block above.
__________________
"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
01-10-10, 12:18 PM   #11
opismahname
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 23
Originally Posted by Seerah View Post
You're missing an end in the code block above.
Didn't work with that end, you did mean to set an end at the end of everything, right?
Can the reason why it doesnt work be because i use Prat instead of the default chat frame?
  Reply With Quote
01-10-10, 12:20 PM   #12
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
No, you need to close your function.
__________________
"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
01-10-10, 12:22 PM   #13
Akryn
A Firelord
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 479
Originally Posted by Seerah View Post
You're missing an end in the code block above.
This. Also, you should declare hasrf local above the function:

Code:
local hasrf
local function eventHandler(self, event, ...)
  Reply With Quote
01-10-10, 12:31 PM   #14
opismahname
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 23
Woow, this is much harder then i excpected.

Code:
local frame = CreateFrame("FRAME", "RFFrame");
frame:RegisterEvent("UNIT_AURA");
local function eventHandler(self, event, ...)
end
local active = UnitBuff('player', 'Righteous Fury')
local hasrf
local function eventHandler(self, event, ...)
if not hasrf and active then
    hasrf = true
    print('Rigtheous Fury has been activated')
elseif hasrf and not active then
    hasrf = false
end
frame:SetScript("OnEvent", eventHandler);
Is this right? (I guess not, but tried to do what you wrote)
  Reply With Quote
01-10-10, 12:35 PM   #15
Akryn
A Firelord
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 479
local function eventHandler(self, event, ...)
end
That closes the function as soon as you start it. You need to put the "end" at the *end* of the function

To be a bit more illustrative, look at what your current code looks like indented:

lua Code:
  1. local function eventHandler(self, event, ...)
  2.     local active = UnitBuff('player', 'Righteous Fury')
  3.     if not hasrf and active then
  4.         hasrf = true
  5.         print('Rigtheous Fury has been activated')
  6.     elseif hasrf and not active then
  7.         hasrf = false
  8.     end

does that make the problem more clear?
  Reply With Quote
01-10-10, 12:42 PM   #16
opismahname
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 23
Code:
   1.
      local function eventHandler(self, event, ...)
   2.
          local active = UnitBuff('player', 'Righteous Fury')
   3.
          if not hasrf and active then
   4.
              hasrf = true
   5.
              print('Rigtheous Fury has been activated')
   6.
          elseif hasrf and not active then
   7.
              hasrf = false
   8.
          end
   9.
      end
Don't think it's right yet, this is hard xD
  Reply With Quote
01-10-10, 12:50 PM   #17
Akryn
A Firelord
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 479
Right, now you've got that part correct. Now you just need to clean up your extra function (which was probably accidentally left there).

After that, there's only one more problem with this code:

lua Code:
  1. local frame = CreateFrame("FRAME", "RFFrame");
  2. frame:RegisterEvent("UNIT_AURA");
  3. --deleted duplicate function definition that was here
  4. local active = UnitBuff('player', 'Righteous Fury')
  5. local hasrf
  6. local function eventHandler(self, event, ...)
  7.     if not hasrf and active then
  8.         hasrf = true
  9.         print('Rigtheous Fury has been activated')
  10.     elseif hasrf and not active then
  11.         hasrf = false
  12.     end
  13. end
  14. frame:SetScript("OnEvent", eventHandler);

...and that is that "active" is declared in the wrong place. You need to check whether the player has the buff every time the function runs, not just when your addon loads. Is that enough information for you to figure out what to change?
  Reply With Quote
01-10-10, 01:34 PM   #18
Grimsin
A Molten Giant
 
Grimsin's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2006
Posts: 990
Originally Posted by opismahname View Post
Woow, this is much harder then i excpected
It gets easier the more you plug away at it i promise.
__________________
"Are we there yet?"

GrimUI
[SIGPIC][/SIGPIC]
  Reply With Quote
01-10-10, 01:37 PM   #19
Xrystal
nUI Maintainer
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 5,877
Originally Posted by Grimsin View Post
It gets easier the more you plug away at it i promise.
I can concur there. When I tried my first addon I was thinking there was no way I'd figure out the fancy stuff but with a site like wowwiki and people here to help you out you'll be doing fancy stuff sooner than you'd expect.
__________________
  Reply With Quote
01-10-10, 01:51 PM   #20
opismahname
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 23
Originally Posted by Akryn View Post
Right, now you've got that part correct. Now you just need to clean up your extra function (which was probably accidentally left there).

After that, there's only one more problem with this code:

lua Code:
  1. local frame = CreateFrame("FRAME", "RFFrame");
  2. frame:RegisterEvent("UNIT_AURA");
  3. --deleted duplicate function definition that was here
  4. local active = UnitBuff('player', 'Righteous Fury')
  5. local hasrf
  6. local function eventHandler(self, event, ...)
  7.     if not hasrf and active then
  8.         hasrf = true
  9.         print('Rigtheous Fury has been activated')
  10.     elseif hasrf and not active then
  11.         hasrf = false
  12.     end
  13. end
  14. frame:SetScript("OnEvent", eventHandler);

...and that is that "active" is declared in the wrong place. You need to check whether the player has the buff every time the function runs, not just when your addon loads. Is that enough information for you to figure out what to change?
I have no idea tbh... Can't find what's wrong more ;D
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » First Addon - need help

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