Skip to content
asaka-wa edited this page Feb 15, 2020 · 105 revisions

Useful Snippets

Table of Contents

Limit the # of clones in a TSU

This will also allow a parent dynamic group to sort them by order of creation.

function(allstates, ...)
  --do some stuff
  --maybe decision trees or whatever
  local newState = {
    --state info goes here
    show = true,
    index = 0, --this bit is important
  }
  tinsert(allstates, newState)
  --this functions as a queue; the oldest clones will be deleted first
  for _,v in pairs(allstates) do
    v.index = v.index + 1
    v.changed = true
    if v.index > cloneLimit then --where cloneLimit is some number > 0
      v.show = false
    end
  end
  return true
end

Easy iteration through group members

for unit in WA_IterateGroupMembers() do
   <block>
end

Get a spell's cost

GetSpellPowerCost(spellid) returns a table.

Cost is found at: GetSpellPowerCost(spellid)[1].cost An example of how this could be used would be a custom function that you can call by sending the spellID and get the cost returned:

aura_env.GetCost = function(spellID, powerType)
    if not spellID then return end
    powerType = powerType or UnitPowerType("player")
    local cost = 0
    local costTable = GetSpellPowerCost(spellID);
    for _, costInfo in pairs(costTable) do
        if costInfo.type == powerType then
            return costInfo.cost;
        end
    end
end
aura_env.GetCost(spellID [, powerType])

An example of this function's use in a Weak Aura: Link

Acquire the correct nameplate frame for a unit

If you're looking to attach an aura to a specific unit's nameplate, then use the following to get the correct frame.

C_NamePlate.GetNamePlateForUnit(unitID)

Range checking

UPDATE for added WeakAuras functionality

WeakAuras now has a default trigger status - Range Check that allows for range checks to specified units. If you do need to do some range checking in custom functions then you can also use the WeakAuras functions to simplify things:

WeakAuras.IsSpellInRange(spellId | spellName, unit)

This is very similar to the regular Blizzard function but allows the use of spellIDs or spell names. Returns a single value of nil, 0, or 1 for invalid target, out of range, in range respectively.

WeakAuras.GetRange(unit)

Since there's no way to get precise range info this is as close as it gets. Returns two number values for min and max possible distance from the given unit.

WeakAuras.CheckRange(unit, range, operator)

Allows to check if in or out of a given distance from the target. Give a unit, range number and either ">=" or "<=". Returns boolean.

General info on range checking without using WeakAuras-specific functions

With precise Unit positions being locked in meaningful content we're limited in the functions we can use to check ranges. The main options are IsItemInRange() and IsSpellInRange(). Although they seem very similar IsSpellInRange requires a spell name and a unit, while IsItemInRange will accept an item name, ID or Link, along with the unit. Also while IsItemInRange returns a simple boolean, IsSpellInRange returns a 1/0/nil for true/false/invalid. A simple spell range check would look like:

function()
    return IsSpellInRange((GetSpellInfo(203782)), "target") == 1
end

While a simple item range check would be:

function()
    return IsItemInRange(33278, "target")
end

Example

Say you want a trigger to check if there are 3 or more nameplates in range:

function()
    local count = 0
    for i = 1, 40 do
        local unit = "nameplate"..i
        if UnitCanAttack("player", unit)
        and WeakAuras.CheckRange(unit, 8, "<=")
        then
            count = count + 1
        end
    end
    return count >= 3 -- change as needed
end

https://wago.io/SimpleRangeChecker - An example Aura using the above function.

Honourable mentions for range checking go to: • UnitInRange("unit") which checks a generic distance for group members only (generally, are people close enough to be healed?) • CheckInteractDistance("unit", distIndex) which can be used to check some set distances like within range to trade/follow etc.

Class-coloured player names

This snippet is for use in a custom text function and will return a player name with class colouring.

function()
    if aura_env.state and aura_env.state.sourceName and UnitExists(aura_env.state.sourceName) then
        return WA_ClassColorName(aura_env.state.sourceName)
    end
end

This example uses aura_env.state.sourceName which is something that event - combat log - spell - cast success triggers might produce, and would work when the source of the event was someone in your group. The 4th, 5th and 6th lines are the important ones and they just need a valid UnitId.

Get Tooltip Data

tooltipText, debuffType, tooltipSize = WeakAuras.GetAuraTooltipInfo(unit, index, filter)
-- index is the actual aura index, not spellID etc.
-- tooltipSize is probably formatted with commas if a large number

Get WeakAuras to help you track CDs

WeakAuras.WatchSpellCooldown(id)
-- Once initialised, will fire "events" for the spell on:
SPELL_COOLDOWN_READY -- Used for the cooldown ready trigger
SPELL_COOLDOWN_CHANGED -- Used for the cooldown progress (spell) trigger

These events all carry the spellID of the CD that's changing as their first arg.

Acquiring the region of a WeakAuras clone

NOTE - aura_env.region now returns the correct region for clones as well as normal Auras. this makes the following much less frequently needed.

Especially useful for TSU auras.

WeakAuras.GetRegion(auraID, cloneID)

If this is being used within a code block that's specific to the clone (i.e. Custom Text, Custom Animations, On Show/Hide) then you can simply use WeakAuras.GetRegion(aura_env.id, aura_env.cloneId)

GCD Lookup

Specifically this is for checking against a spell's CD to ensure that it is on CD and not just showing the global.

local gcdSTART, gcdDUR = GetSpellCooldown(61304) -- GCD
local GCD_expirationTime = gcdSTART + gcdDUR

if spell_expirationTime == GCD_expirationTime then
    --we are NOT on CD
elseif spell_expirationTime > GCD_expirationTime then
    --we ARE on CD.
end

This can also be done with the assistance of a couple of WeakAuras functions. Putting WeakAuras.WatchGCD() in an Aura's "On Init" gives access to:

local start, duration = GetSpellCooldown(spellId)
if duration > 0 and duration ~= WeakAuras.gcdDuration() then
    --We ARE on CD
else
    --We ARE NOT on CD
end

Format PercentHealth output text

If using a status - health or status - power trigger, a variable will be available for percentages. However the placeholders %percenthealth and %percentpower are unformatted, and generally return many decimal places. To round it and add a percent sign you can use %c in the text box to open a custom code block, then paste:

function()
    if aura_env.state and aura_env.state.percenthealth then
        return Round(aura_env.state.percenthealth).."%"
    end
end

Change the colour of the CD "sweep"

Paste the following into Actions - On Init - Custom and adjust the 4 values in brackets for the required colour and opacity - (red, green, blue, alpha)

aura_env.region.cooldown:SetSwipeTexture([[Interface\AddOns\WeakAuras\Media\Textures\Square_White]])
aura_env.region.cooldown:SetSwipeColor(0.17,0,0,0.8)

Getting Boss Units (e.g on council fights)

Put this into Actions - On Init - custom

local aura_env = aura_env
local GetBossInfo = function()
    aura_env.bossUnit = {}
    for i = 1,5 do
        local unit = "boss"..i
        if UnitExists(unit) then
            local GUID = UnitGUID(unit)
            local NPCID = select(6, strsplit("-", GUID))
            aura_env.bossUnit[tonumber(NPCID)] = unit
        end
    end
end
local spawnChecker = CreateFrame("Frame")
spawnChecker:RegisterEvent("INSTANCE_ENCOUNTER_ENGAGE_UNIT")
spawnChecker:SetScript("OnEvent", GetBossInfo)

This will give you a table of Boss unit IDs accessible via their NPC ID throughout your Aura, e.g. UnitIsUnit("player", aura_env.bossUnit[123456].."target").

Getting dynamic info from non-active triggers

If you ever need to get dynamic info from a trigger and aura_env.states isn't an option:

local states = WeakAuras.GetTriggerStateForTrigger(AuraId, triggernum)

This will provide a table containing all the clone states carried by the specified trigger. For a regular non-cloning trigger the single state will have an empty string for its key.

Adjusting 3D Models

3D model type regions have a few unique functions and the region can be modified at aura_env.region.model.

-- Set the display positioning of the model (z, x, y)
aura_env.region.model:SetPosition(-5, 0, 10);

-- Set the rotation of the model in radians
aura_env.region.model:SetFacing(rad(180));
aura_env.region.model:SetFacing(3.14); -- these do the same thing

-- Change the model (note that you may need to adjust the z, x, y positioning)
aura_env.region.model:SetModel("Creature\\Arthaslichking\\arthaslichking.m2")

-- Change the model to match a unitID
aura_env.region.model:SetUnit("target")

-- Change animation speed
local elapsed=0
aura_env.region.model:SetScript("OnUpdate", function(self, elaps)
        elapsed = elapsed + (elaps * 500); -- the important bit is right here: 1000 = normal speed, 500 = half speed, etc
        self:SetSequenceTime(0, elapsed);
end);

Useful reference info on the Global table

Use /dump VAR_NAME in game to check their contents.

  • ICON_LIST: Raid target marker texture paths
  • RAID_CLASS_COLORS: Default colour info for classes
  • COMBATLOG_DEFAULT_COLORS.schoolColoring : Default colour info for damage schools