WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   First Addon - need help (https://www.wowinterface.com/forums/showthread.php?t=29911)

opismahname 01-09-10 08:38 PM

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...

Akryn 01-09-10 09:00 PM

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.

opismahname 01-09-10 09:15 PM

Quote:

Originally Posted by Akryn (Post 173968)
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?

Akryn 01-09-10 09:25 PM

Quote:

Originally Posted by opismahname (Post 173969)
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


opismahname 01-09-10 09:56 PM

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

Akryn 01-09-10 10:17 PM

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

opismahname 01-10-10 07:25 AM

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.

ravagernl 01-10-10 07:52 AM

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 :)

opismahname 01-10-10 11:51 AM

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

Seerah 01-10-10 12:14 PM

You're missing an end in the code block above.

opismahname 01-10-10 12:18 PM

Quote:

Originally Posted by Seerah (Post 174035)
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?

Seerah 01-10-10 12:20 PM

No, you need to close your function.

Akryn 01-10-10 12:22 PM

Quote:

Originally Posted by Seerah (Post 174035)
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, ...)


opismahname 01-10-10 12:31 PM

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)

Akryn 01-10-10 12:35 PM

Quote:

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?

opismahname 01-10-10 12:42 PM

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

Akryn 01-10-10 12:50 PM

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?

Grimsin 01-10-10 01:34 PM

Quote:

Originally Posted by opismahname (Post 174040)
Woow, this is much harder then i excpected

It gets easier the more you plug away at it :) i promise.

Xrystal 01-10-10 01:37 PM

Quote:

Originally Posted by Grimsin (Post 174054)
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.

opismahname 01-10-10 01:51 PM

Quote:

Originally Posted by Akryn (Post 174050)
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


All times are GMT -6. The time now is 08:50 PM.

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