1. #1

    [Guardian] Lets write a savage defense add-on!

    I've gotten a couple of request for this, so here goes. Please forgive any mistakes or oversights.

    The point here is to write a simple add-on that will alert the player when they are at sixty rage or more so that they know when they can use Savage Defense.

    To do this we will build a full screen panel, with a transparent texture that will pulse when the correct conditions are met and be hidden otherwise.

    This project will consist of three files, plus a custom texture if you want one.

    First, create a folder in your Interface\AddOns directory off of the World of Warcraft install directory. Name this folder "SavageAlert" or whatever you wish to name this add-on (we will take it as read that the name will be SavageAlert going forward, substitute your own name if you wish).

    Then, within the new folder, create a file named "SavageAlert.toc". This file defines the add-on. Open this file in the text editor of your choice, and enter the following.
    Code:
    ## Interface: 50400
    ## Title : Savage Alert |cff3BB9FFVersion|cff3BB9FF 1.0
    ## Notes: To alert when Savage Defense can be used.
    ## Author: My Name
    ## eMail: [email protected]
    ## URL: http://www.wowwiki.com/
    ## Version: 1.0
    ## DefaultState: enabled
    ## SavedVariablesPerCharacter: SA_ENABLED
    SavageAlert.xml
    The above code defines some basic information about the add-on, including its name and what other files it will use. In this case, only one other file is defined "SavageAlert.xml".

    Create a new text file in the SavageAlert directory named "SavageAlert.xml". This will define the interface for the add-on. Open the file in your text editor, and enter the following.
    Code:
    <Ui xmlns="http://www.blizzard.com/wow/ui/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.blizzard.com/wow/ui/">
        <Script file="SavageAlert.lua"/>
        <Frame name="SavageAlertCore">
    	<Scripts>
    	    <OnLoad function="SavageAlert_OnLoad"/>
    	    <OnEvent function="SavageAlert_OnEvent"/>
    	</Scripts>
        </Frame>
    </Ui>
    Since there really is no interface to this add-on, the XML is fairly simple. All it does is setup a LUA file, which will contain the actual code and setup two events that will trigger when the add-on loads and when any other defined event is triggered.

    Now create the SavageAlert.lua file and open it in your text editor. Enter the following at the start of the file.
    Code:
    -- Rage flash frame
    local f = CreateFrame("Frame","rageFrame",WorldFrame);
    f:SetFrameStrata("BACKGROUND");
    f:SetClampedToScreen(true)
    f:SetAllPoints(UIParent)
    local t = f:CreateTexture(nil,"BACKGROUND");
    t:SetTexture("Interface\\FullScreenTextures\\LowHealth");
    t:SetAlpha(0.5);
    t:SetBlendMode("ADD");
    t:SetVertexColor(1, 0.1, 0.1);
    t:SetAllPoints(frame);
    f.texture = t;
    f:SetPoint("CENTER",0,0);
    f:Hide();
    This sets up the full screen image that will flash when rage is at the desired level. This is contained in a frame referred to simply as "f". The image being used is one of the ones built into the game named "OutofControl". If you want to use a custom image, change the SetTexture call to something like the following (in this case to use a texture stored in the SavageAlert folder).

    Code:
    t:SetTexture("Interface\\AddOns\\SavageAlert\\rage.tga");
    Next enter the following, directly after the previously entered code.
    Code:
    SA_ENABLED = "on";
    
    function SavageAlert_OnLoad(self)
        self:RegisterEvent("ADDON_LOADED");
        self:RegisterEvent("UNIT_POWER");
        DEFAULT_CHAT_FRAME:AddMessage('Savage Alert Loaded.');
        
        SlashCmdList["SA"] = SA_SlashCommand;
        SLASH_SA1 = "/savagealert";
        SLASH_SA2 = "/sa";
    end
    This sets up a variable "SA_ENABLE" that will be used to allow the user to turn the add-on off from inside the game. This setting will be saved between game sessions. In addition it sets up the "on load" function that will be run when the add-on is loaded (when a character enters the game).

    The "on load" function registers two events, ADDON_LOADED and UNIT_POWER. It is the later, UNIT_POWER that we are the most interested in as it is what will be called when the player gains or loses rage, mana, runic power, etc.

    Lastly, the method sets up two slash commands that will be used to manipulate the add-on settings.

    Now enter the following.
    Code:
    function SavageAlert_OnEvent(self, event, ...)    
        if (event=="UNIT_POWER") then
            if(UnitPowerType("player")==1 and SA_ENABLED=="on") then
                if(UnitPower("player")>=60) then
                    UIFrameFlash(f, 0.5, 0.5, -1);
                else
                    if (UIFrameIsFlashing(f)) then
                        UIFrameFlashStop(f);
                    end
                end
            else
                if (UIFrameIsFlashing(f)) then
                    UIFrameFlashStop(f);
                end
            end
        end
    end
    This code sets up the SavageAlert_OnEvent function, that is called whenever a registered event happens. This code will make the frame we defined flash when the user is at 60 rage or more and stop flashing when they are at less then 60 rage or if they do not have a rage bar (such as when in caster or cat forms).

    Enter one last section of code.
    Code:
    function SA_SlashCommand(Msg)
        local msg, sub_msg = SA_GetCmd(Msg);
            if(msg=="off") then
            SA_ENABLED = "on";
            DEFAULT_CHAT_FRAME:AddMessage('|cff00ff00 Savage Defense alerts on |r');
        end
        if(msg=="on") then
            SA_ENABLED = "off";
            DEFAULT_CHAT_FRAME:AddMessage('|cff00ff00 Savage Defense alerts off |r');
        end
    end
    function SA_GetCmd(msg)
        if msg then
            local a,b,c=strfind(msg, "(%S+)"); --contiguous string of non-space characters
            if a then
                return c, strsub(msg, b+2);
            else	
                return "";
            end
        end
    end
    This will look for the slash commands we setup and let the user toggle the add-on on and off.

    --Please let me know if you find any problems with the above example, hard to be sure everything made it in as intended.
    Last edited by Kujako; 2013-01-17 at 08:32 PM. Reason: Had the texture name wrong.
    It is by caffeine alone I set my mind in motion. It is by the beans of Java that thoughts acquire speed, the hands acquire shakes, the shakes become a warning.

    -Kujako-

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •