Thread Tools Display Modes
11-21-13, 03:56 AM   #1
zork
A Pyroguard Emberseer
 
zork's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 1,740
Explaining UnitPower

I thought it might by helpful to bring some light into UnitPower.

The unit power function calls are:
Code:
local p = UnitPower(unit[, POWER_TYPE_INDEX][, FULL_POWER])
local pm = UnitPowerMax(unit[, POWER_TYPE_INDEX][, FULL_POWER])
By calling
Code:
local p = UnitPower("player")
you will get the current power value of the default unit power type of the unit "player".

At its current state WoW knows a ton of other power types.

Instead of getting the default power it is possible to access any other unit power by adding a second argument, the POWER_TYPE_INDEX.

Full unit power list as of state WoW patch 5.4.1
Code:
POWER_INDEX_VARIABLE  POWER_INDEX   Type
=================================================================================================
SPELL_POWER_MANA            0             Mana
SPELL_POWER_RAGE            1             Rage
SPELL_POWER_FOCUS           2             Focus
SPELL_POWER_ENERGY          3             Energy
???                         4             Combo Points
SPELL_POWER_RUNES           5             Deathknight Runes
SPELL_POWER_RUNIC_POWER     6             Deathknight Runic Power
SPELL_POWER_SOUL_SHARDS     7             Warlock Affliction Soul Shards
SPELL_POWER_ECLIPSE         8             Druid Balance Eclipse
SPELL_POWER_HOLY_POWER      9             Paladin Holy Power
SPELL_POWER_ALTERNATE_POWER 10            Alternate Power used in certain boss encounters or in mini-games
SPELL_POWER_DARK_FORCE      11            --
SPELL_POWER_CHI             12            Monk Chi
SPELL_POWER_SHADOW_ORBS     13            Priest Shadow Orbs
SPELL_POWER_BURNING_EMBERS  14            Warlock Destruction Burning Embers
SPELL_POWER_DEMONIC_FURY    15            Warlock Demonology Demonic Fury
Source: http://wowpedia.org/PowerType

So if you are a druid and you are currently in cat form by using
Code:
local SPELL_POWER_MANA = SPELL_POWER_MANA
local p = UnitPower("player", SPELL_POWER_MANA)
you are able to get the player unit mana value while the current default power type would return the energy value.

Before we get to a full example a quick note. The above is a list of global variables that will deliver the corresponding power type index. The index is needed to access the correct power value.
When working with powers you will register events to your frame. At the current state of the game that will probably be "UNIT_POWER" or "UNIT_POWER_FREQUENT". When those events fire they will deliver 4 arguments: SELF, EVENT, UNIT, POWER_TYPE_TOKEN.
The token itself does nothing, it is just a string. But you can use that string to access the corresponding power type index from the global scope by doing: _G["SPELL_POWER_"..POWER_TYPE_TOKEN]

lua Code:
  1. --local reference to _G
  2. local _G = _G
  3.  
  4. --PowerBarOnEvent func
  5. local function PowerBarOnEvent(...)
  6.   local self, event, unit, powerTypeToken = ...
  7.   local powerTypeIndex = _G["SPELL_POWER_"..powerTypeToken]
  8.   --check
  9.   if not self:IsShown() then return end
  10.   if not unit or (unit and (unit ~= "player" and unit ~= "vehicle")) then return end
  11.   if not powerTypeIndex then return end
  12.   --get the unit power per element (for example per burning ember)
  13.   local up = UnitPower(unit, powerTypeIndex)
  14.   local upm = UnitPowerMax(unit, powerTypeIndex)
  15.   --get the full unit power
  16.   local upf = UnitPower(unit, powerTypeIndex, true)
  17.   local upmf = UnitPowerMax(unit, powerTypeIndex, true)
  18.   --print
  19.   print(powerTypeToken)
  20.   print(powerTypeIndex)
  21.   print(up.."/"..upm)
  22.   print(upf.."/"..upmf)
  23.   --the default power type index and power type token
  24.   --the default power type index can be used to prevent wrong power events from getting through
  25.   local dpti, dptt = UnitPowerType(unit)
  26.   print("default: "..dpti.."/"..dptt)  
  27. end
  28.  
  29. --power frame
  30. local powerBar = CreateFrame("StatusBar", nil, UIParent)
  31. powerBar:RegisterUnitEvent("UNIT_POWER_FREQUENT", "player", "vehicle")
  32. powerBar:SetScript("OnEvent", PowerBarOnEvent)

When using the above example it will print you all unit power values from the WoW API that triggered an event for the "player" and the "vehicle" unit.
When you play a class that runs multiple power types you will recognize that multiple power types fire constantly. So using one bar to display all the powers is impossible.
One has to create multiple bars for multiple powers and register specific power types per bar. To check which is the default unit power one can use UnitPowerType(UnitId).
To prevent event spam the powerTypeToken of the event should be checked against a list of active power types available for that bar. (Example: https://github.com/haste/oUF/blob/ma...icons.lua#L164)

The last point I want to talk about is the difference between
Code:
local up = UnitPower(unit, powerTypeIndex)
and
Code:
local upf = UnitPower(unit, powerTypeIndex, true)
If the power you are looking at can be divided into sub-elements, like burning embers UnitPower(unit, powerTypeIndex) will only return the power per element. But UnitPower(unit, powerTypeIndex, true) will return the power in relation to the full bar. Thus you could display burning ember as a single big bar.

Example.
lua Code:
  1. local up    = UnitPower(unit, powerTypeIndex)
  2. local upm   = UnitPowerMax(unit, powerTypeIndex) --will be equivalent to the max number of elements on class bars
  3. local upf   = UnitPower(unit, powerTypeIndex, true)
  4. local upmf  = UnitPowerMax(unit, powerTypeIndex, true)
  5. --up   = 1
  6. --upm  = 4
  7. --upf  = 150
  8. --upmf = 400
  9. local maxPowerPerElement = upmf/upm
  10. --maxPowerPerElement = 100
  11. local currentElementValue = upf-up*maxPowerPerElement
  12. --currentElementValue = 50

Sources:
http://wowprogramming.com/docs/api/UnitPower
http://www.wowwiki.com/API_UnitPower
http://wowpedia.org/API_UnitPower
http://wowprogramming.com/docs/api/UnitPowerMax
http://www.wowwiki.com/API_UnitPowerMax
http://wowpedia.org/API_UnitPowerMax
__________________
| Simple is beautiful.
| WoWI AddOns | GitHub | Zork (WoW)

"I wonder what the non-pathetic people are doing tonight?" - Rajesh Koothrappali (The Big Bang Theory)

Last edited by zork : 11-05-14 at 03:52 PM.
  Reply With Quote
11-21-13, 10:07 AM   #2
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
Illuminating for people who aren't sure how to handle the UnitPower* APIs and related events, but, and don't take this as anything other than curiosity on my behalf, why post this here? Unless someone asked, and using the WowI forums is a means to an end?
  Reply With Quote
11-21-13, 10:32 AM   #3
Vlad
A Molten Giant
 
Vlad's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2005
Posts: 793
I think it's a good submission to the "Tutorials & Other Helpful Info" category!
__________________
Profile: Curse | Wowhead
  Reply With Quote
11-21-13, 10:42 AM   #4
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 822
why post this here?
I keep a little list of zorks tutorials and explanations to look into when I find the time or run in a situation where I need it Whenever I have an idea to create an addon you can be sure I put stuff in it I learned from those posts (animations etc) Other stuff is too difficult for me to understand eg his descriptions of round statusbars but still its in my mind that he has provided code to help me in case I need it
__________________
The cataclysm broke the world ... and the pandas could not fix it!
  Reply With Quote
11-05-14, 03:49 PM   #5
zork
A Pyroguard Emberseer
 
zork's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 1,740
WoD updated the powertype index 4 for WoD. That index now reflects player combo points.
http://wowpedia.org/PowerType
__________________
| Simple is beautiful.
| WoWI AddOns | GitHub | Zork (WoW)

"I wonder what the non-pathetic people are doing tonight?" - Rajesh Koothrappali (The Big Bang Theory)
  Reply With Quote

WoWInterface » Developer Discussions » Tutorials & Other Helpful Info. » Explaining UnitPower

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