Thread Tools Display Modes
11-29-13, 04:32 PM   #1
P4artykilla
A Defias Bandit
Join Date: Nov 2013
Posts: 2
Userdefined Function

Hi, I'm trying to make a userdefined function...
Not sure if I got everything right,

a little help would be nice

I want this function to work like this:

if DamageCombatTime(3000, 30) then print("Go out of Defencive Stance!") end

(Want it to warn if you are in def stance after 30 seconds with less than 3k damage taken)

Code:
function DamageCombatTime(damagevalue, lastdamagetaken)
if event == "COMBAT_LOG_EVENT_UNFILTERED" then
        local unit, action, _, damage = ...
        if unit == "player" and action ~= "HEAL" and damage > damagevalue then
                lastdamagetaken = GetTime()
				return true
      		  end
        end
end
  Reply With Quote
11-29-13, 10:26 PM   #2
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
You're going to need more than "a function" for that. You'll need a variable outside the function to keep a running total of your damage taken. Also, unless you want to sit there manually pushing that button to run that command over and over and over, you're going to need a frame to run a timer, so it will automatically alert you when the time is up. You could go even further and have it automatically detect when you entered Defensive Stance, so you never had to run the command at all.

Code:
-- You need a frame to run a timer and listen for events.
local frame = CreateFrame("Frame")

-- You need variables to keep track of stuff.
local totalDamageTaken = 0
local playerGUID

-- You can use an animation object to run a timer.
local timer = frame:CreateAnimationGroup()
timer.anim = timer:CreateAnimation()
timer.anim:SetDuration(30)
timer.anim:SetOrder(1)

-- Tell the timer what to do when the time runs out.
timer:SetScript("OnFinished", function(self, requested)
     -- "requested" is true if you manually called :Stop() or :Finish() on the timer,
     -- or nil if the timer finished on its own.
     if not requested then
          -- Check to see if you've taken at least 3k damage since the timer started.
          if totalDamageTaken < 3000 then
               -- Nope. Show the alert.
               UIErrorsFrame:AddMessage("Get out of Defensive Stance!", 1, 0.8, 0, UIERRORS_HOLD_TIME)
          end
     end
end)

-- Tell the frame to listen for combat events.
frame:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")

-- Tell the frame to listen for your spell casts.
frame:RegisterUnitEvent("UNIT_SPELLCAST_SUCCEEDED", "player")

-- Tell the frame what to do when a registered event fires.
frame:SetScript("OnEvent", function(self, event, ...)
     -- Check which event fired.
     if event == "COMBAT_LOG_EVENT_UNFILTERED" then
          -- Assign the arguments to named variables.
          -- Use the name "_" for variables you don't care about, for tidiness.
          local _, subevent, _, _, _, _, _, destGUID, _, _, _, _, _, _, amount = ...

          -- Check if this combat event indicates damage being done.
          if subevent == "SPELL_DAMAGE" or subevent == "SWING_DAMAGE" then
              if not playerGUID then
                    -- Avoid unnecessary function calls by only defining this variable once per session.
                    playerGUID = UnitGUID("player")
               end

               -- Check if this damage was done to the player.
               if destGUID == playerGUID then
                    -- Add the amount to the total.
                    totalDamageTaken = totalDamageTaken + amount
               end
          end

     else
          -- Since you're only listening for 2 events, if it wasn't CLEU it must be a spellcast,
          -- so you can just use "else" and not waste time explicitly checking the event name.
          local _, _, _, _, spellID = ...

          -- Check if the spell was Defensive Stance.
          if spellID == 71 then
               -- Clear the total.
               totalDamageTaken = 0
               -- Start the timer.
               timer:Play()
          end
end)
There are a number of improvements that could be made there, but that should get you started. For example, you should probably only listen for CLEU events while in Defensive Stance. You may also want to listen for combat starting/stopping, and only run the timer in combat, since you won't be taking damage out of combat even when you're supposed to be in Defensive Stance. You should also stop the timer if you switch out of Defensive Stance on your own.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
11-30-13, 05:00 AM   #3
P4artykilla
A Defias Bandit
Join Date: Nov 2013
Posts: 2
Okay, thanks for replying but this got way too advanced, and I get error from using this too :P
Won't even load the lua...

Anyways thanks xD
  Reply With Quote
11-30-13, 05:18 AM   #4
Dridzt
A Pyroguard Emberseer
 
Dridzt's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 1,360
Originally Posted by P4artykilla View Post
Okay, thanks for replying but this got way too advanced, and I get error from using this too :P
Won't even load the lua...

Anyways thanks xD
You need to provide more context to your request.
Are you aiming for a standalone addon?

That looks more like something you'd like to put in a WeakAuras custom trigger to use for PvP.
If my guess is correct, you need to be much much more specific in what you want done.

Describe what you want to accomplish exactly.
  Reply With Quote
11-30-13, 11:50 AM   #5
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Saying "there is an error" is worse than useless. If there's an error, just copy and paste the error so I can fix it.

Also, yeah, if you're looking for something to shove in a macro, in a kgPanels script, or in a WeakAuras trigger, that's not even remotely the same thing as an addon, so you should specify that up front.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote

WoWInterface » AddOns, Compilations, Macros » AddOn Help/Support » Userdefined Function


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