Thread Tools Display Modes
08-02-12, 10:09 AM   #1
wolftech
A Deviate Faerie Dragon
Join Date: May 2008
Posts: 14
Help! What is this n00b doing wrong?

I am working on some unit frames and I have run into an error on Beta I can not seem to nail down. When I target some NPCs and Mobs (but not players), I get a Division by Zero error in what is line 37 in this snippet. It works fine on Live, but I can't find what changed in Beta.

Can you help a n00b out?

Lua Code:
  1. local firstload = true
  2. function TUF:TargetUpdate()
  3.     if firstload and not TextUnitFramesLiteDB.general.combat then
  4.         firstload = false
  5.         target:SetPoint(TextUnitFramesLiteDB.target.loc.p, UIParent, TextUnitFramesLiteDB.target.loc.rP, TextUnitFramesLiteDB.target.loc.x, TextUnitFramesLiteDB.target.loc.y)
  6.     end
  7.     if UnitExists("target") then
  8.         TUF:LockTargetFrame()
  9.         local fg, fn = UnitFactionGroup("target")
  10.         if fg ~= nil then target.faction:SetTexture("Interface\\GROUPFRAME\\UI-Group-PVP-"..fg) else target.faction:SetTexture("Interface\\PVPFrame\\PVP-ArenaPoints-Icon") end
  11.         target.faction:Show()
  12.         target.level:SetPoint("TOPLEFT", target, "TOPLEFT", 5, -5)
  13.         target.name:SetPoint("LEFT", target.level, "RIGHT", 5, 0)
  14.         if TextUnitFramesLiteDB.target.orientation then
  15.             target.health:ClearAllPoints()
  16.             target.mana:ClearAllPoints()
  17.             target.health:SetPoint("TOPLEFT", target.level, "BOTTOMLEFT", 0, 0)
  18.             target.mana:SetPoint("TOPLEFT", target.health, "BOTTOMLEFT", 0, 0)
  19.         else
  20.             target.health:ClearAllPoints()
  21.             target.mana:ClearAllPoints()
  22.             target.health:SetPoint("LEFT", target.name, "RIGHT", 5, 0)
  23.             target.mana:SetPoint("LEFT", target.health, "RIGHT", 0, 0)
  24.         end
  25.        
  26.         target.level:SetFontObject(GameFontNormalLarge)
  27.         target.name:SetFontObject(GameFontNormalHuge)
  28.         target.health:SetFontObject(NumberFontNormalLarge)
  29.         target.mana:SetFontObject(NumberFontNormalLarge)
  30.        
  31.         target.level:SetText(UnitLevel("target"))
  32.         target.name:SetText(UnitName("target"))
  33.         local _, cls = UnitClass("target")
  34.         if cls ~= nil then target.name:SetTextColor(RAID_CLASS_COLORS[cls].r, RAID_CLASS_COLORS[cls].g, RAID_CLASS_COLORS[cls].b) end
  35.         local htxtclr = TUF:SetTextColor(UnitHealth("target")/UnitHealthMax("target")*100)
  36.         target.health:SetTextColor(htxtclr.r, htxtclr.g, htxtclr.b, 1)
  37.         local mtxtclr = TUF:SetTextColor(UnitPower("target")/UnitPowerMax("target")*100)
  38.         target.mana:SetTextColor(mtxtclr.r, mtxtclr.g, mtxtclr.b, 1)
  39.         if TextUnitFramesLiteDB.target.percent then
  40.             target.health:SetText("|Cff00ff00H|r-"..string.format("%4.0f", UnitHealth("target")/UnitHealthMax("target")*100).."%") 
  41.             if UnitPowerType("target") == 0 then -- mana
  42.                 target.mana:SetText("|cff3300cc M|r-"..string.format("%4.0f", UnitPower("target")/UnitPowerMax("target")*100).."%")    
  43.             elseif UnitPowerType("target") == 3 then -- energy
  44.                 target.mana:SetText("|cffffff00E |r-"..UnitPower("target").." |cff990099 Combo|r: "..GetComboPoints("target"))
  45.             elseif UnitPowerType("target") == 1 then -- rage
  46.                 target.mana:SetText("|cffff3333Rage|r- "..UnitPower("target").."|cff666666 /|r"..UnitPowerMax("target"))
  47.             elseif UnitPowerType("target") == 5 then
  48.                 target.mana:SetText("|cff99ccffRP|r- "..UnitPower("target").."|cff666666 /|r"..UnitPowerMax("target"))
  49.             elseif UnitPowerType("target") == 6 then-- Runic
  50.                 target.mana:SetText("|cff99ccffRP|r- "..UnitPower("target").."|cff666666 /|r"..UnitPowerMax("target"))
  51.             else -- focus, happieness
  52.                 target.mana:SetText("|cffffcc00F|r- "..UnitPower("target").."|cff666666 /|r"..UnitPowerMax("target"))
  53.             end
  54.         else
  55.             target.health:SetText("|Cff00ff00H|r- "..UnitHealth("target").."|Cff666666 /|r"..UnitHealthMax("target"))
  56.             if UnitPowerType("target") == 0 then -- mana
  57.                 target.mana:SetText("|cff3300cc M|r-"..UnitPower("target").."|Cff666666 /|r"..UnitPowerMax("target"))      
  58.             elseif UnitPowerType("target") == 3 then -- energy
  59.                 target.mana:SetText("|cffffff00E |r-"..UnitPower("target").." |Cff990099 Combo|r: "..GetComboPoints("target"))
  60.             elseif UnitPowerType("target") == 1 then -- rage
  61.                 target.mana:SetText("|cffff3333Rage|r- "..UnitPower("target").."|Cff666666 /|r"..UnitPowerMax("target"))
  62.             elseif UnitPowerType("target") == 5 then
  63.                 target.mana:SetText("|cff99ccffRP|r- "..UnitPower("target").."|Cff666666 /|r"..UnitPowerMax("target"))
  64.             elseif UnitPowerType("target") == 6 then-- Runic
  65.                 target.mana:SetText("|cff99ccffRP|r- "..UnitPower("target").."|Cff666666 /|r"..UnitPowerMax("target"))
  66.             else -- focus, happieness
  67.                 target.mana:SetText("|cffffcc00F|r- "..UnitPower("target").."|Cff666666 /|r"..UnitPowerMax("target"))
  68.             end
  69.         end
  70.        
  71.         local ll = target.level:GetStringWidth()
  72.         local nl = target.name:GetStringWidth()
  73.         local hl = target.health:GetStringWidth()
  74.         local ml = target.mana:GetStringWidth()
  75.         if not TextUnitFramesLiteDB.target.orientation and not TextUnitFramesLiteDB.general.combat then
  76.             local len = ll + nl + hl + ml + 20
  77.             target:SetWidth(len)
  78.             target:SetHeight(target.level:GetHeight() + 20)
  79.         elseif TextUnitFramesLiteDB.target.orientation and not TextUnitFramesLiteDB.general.combat then
  80.             local len = math.max(ll + nl, ml, hl)
  81.             target:SetWidth(len + 20 )
  82.             local lh = target.level:GetHeight()
  83.             local hh = target.health:GetHeight()
  84.             local mh = target.mana:GetHeight()
  85.             target:SetHeight(lh + hh + mh + 20)
  86.         end
  87.        
  88.         if UnitExists("playertargettarget") then
  89.             if TextUnitFramesLiteDB.target.targettop then
  90.                 target.target:ClearAllPoints()
  91.                 target.target:SetPoint("TOPLEFT", target, "BOTTOMLEFT", 10, 10)
  92.             else
  93.                 target.target:ClearAllPoints()
  94.                 target.target:SetPoint("LEFT", target, "RIGHT", -10, 0)
  95.             end
  96.             target.target.level:SetFontObject(GameFontNormal)
  97.             target.target.name:SetFontObject(GameFontNormal)
  98.             target.target.level:SetText(UnitLevel("playertargettarget"))
  99.             target.target.name:SetText(UnitName("playertargettarget"))
  100.             target.target.level:SetPoint("TOPLEFT", target.target, "TOPLEFT", 5, -5)
  101.             target.target.name:SetPoint("LEFT", target.target.level, "RIGHT", 5, 0)
  102.            
  103.             local ttlw = target.target.level:GetStringWidth()
  104.             local ttnw = target.target.name:GetStringWidth()
  105.             target.target:SetHeight(target.target.name:GetHeight()+12)
  106.             target.target:SetWidth(ttlw + ttnw + 10)
  107.         end
  108.        
  109.         if not TextUnitFramesLiteDB.general.combat then
  110.             if TextUnitFramesLiteDB.target.enabled then target:Show() else target:Hide() end
  111.             if UnitExists("target") then target:Show() else target:Hide() end
  112.             if TextUnitFramesLiteDB.target.target and UnitExists("playertargettarget") then target.target:Show() else target.target:Hide() end
  113.         end
  114.     else
  115.         target:Hide()
  116.     end
  117. end

This is the error I get:
Code:
339x TextUnitFramesLite-5.0\target-Target.lua:131: Division by zero
TextUnitFramesLite-5.0\target-Target.lua:131: in function "TargetUpdate"
TextUnitFramesLite-5.0\main.lua:126: in function <TextUnitFramesLite\main.lua:119>

Locals:
self = TUF {
 0 = <userdata>
 lockPetF = <func> @TextUnitFramesLite\options.lua:404
 enablePetTF = <func> @TextUnitFramesLite\options.lua:430
 lockfocusF = <func> @TextUnitFramesLite\options.lua:452
 orientPetF = <func> @TextUnitFramesLite\options.lua:396
 HideBlizzFocus = <func> @TextUnitFramesLite\focus.lua:201
 ShowBlizzFocus = <func> @TextUnitFramesLite\focus.lua:205
 hideBlizzfocusF = <func> @TextUnitFramesLite\options.lua:439
 target = TUF_Target {}
 OnUpdate = <func> @TextUnitFramesLite\main.lua:119
 pet = TUF_Pet {}
 enablePF = <func> @TextUnitFramesLite\options.lua:353
 player = TUF_Player {}
 HideBlizzPlayer = <func> @TextUnitFramesLite\player.lua:172
 percfocusF = <func> @TextUnitFramesLite\options.lua:448
 generalO_Load = <func> @TextUnitFramesLite\options.lua:315
 enablePetF = <func> @TextUnitFramesLite\options.lua:425
 focus = TUF_Focus {}
 SetTextColor = <func> @TextUnitFramesLite\util.lua:8
 orientfocusTF = <func> @TextUnitFramesLite\options.lua:483
 percPF = <func> @TextUnitFramesLite\options.lua:381
 LockTargetFrame = <func> @TextUnitFramesLite\options.lua:487
 FocusUpdate = <func> @TextUnitFramesLite\focus.lua:67
 orientfocusF = <func> @TextUnitFramesLite\options.lua:444
 enablefocusTF = <func> @TextUnitFramesLite\options.lua:479
 enablefocusF = <func> @TextUnitFramesLite\options.lua:474
 PlayerUpdate = <func> @TextUnitFramesLite\player.lua:82
 ShowBlizzPlayer = <func> @TextUnitFramesLite\player.lua:176
 hideBlizzPF = <func> @TextUnitFramesLite\options.lua:391
 OnEvent = <func> @TextUnitFramesLite\main.lua:84
 percPetF = <func> @TextUnitFramesLite\options.lua:400
 playerO_Load = <func> @TextUnitFramesLite\options.lua:321
 orientPetTF = <func> @TextUnitFramesLite\options.lua:434
 orientPF = <func> @TextUnitFramesLite\options.lua:386
 PetUpdate = <func> @TextUnitFramesLite\pet.lua:68
 TargetUpdate = <func> @TextUnitFramesLite\target.lua:96
 targetO_Load = <func> @TextUnitFramesLite\options.lua:342
 lockPF = <func> @TextUnitFramesLite\options.lua:359
}
elapsed = 0.019000001251698
And here is the section in main.lua (beginning with 119)

Lua Code:
  1. function TUF:OnUpdate(elapsed)
  2.     if not TextUnitFramesLiteDB.general then TextUnitFramesLiteDB.general = {} end
  3.     if not TextUnitFramesLiteDB.general.elapsed then TextUnitFramesLiteDB.general.elapsed = 0 end
  4.     if not TextUnitFramesLiteDB.general.updaterate then TextUnitFramesLiteDB.general.updaterate = 0.066 end
  5.     TextUnitFramesLiteDB.general.elapsed = TextUnitFramesLiteDB.general.elapsed + elapsed
  6.     if TextUnitFramesLiteDB.general.elapsed > TextUnitFramesLiteDB.general.updaterate then
  7.         if UnitExists("target") then if TextUnitFramesLiteDB.target.enabled then TUF.target:Show() end if TextUnitFramesLiteDB.target.hideblizztargetframe then TargetFrame:Hide() end end
  8.         TUF:TargetUpdate()
  9.         TUF:PlayerUpdate()
  10.         TUF:PetUpdate()
  11.         TUF:FocusUpdate()
  12.         if TextUnitFramesLiteDB.general.combat then TUF.player.combat:Show() else TUF.player.combat:Hide() end
  13.         if UnitIsPVP("player") then TUF.player.pvp:Show() else TUF.player.pvp:Hide() end
  14.         if UnitIsPVP("target") then TUF.target.pvp:Show() else TUF.target.pvp:Hide() end
  15.         local lootmethod, mlpID, mlRID = GetLootMethod()
  16.         if lootmethod == "master" then
  17.             if mlpID == nil then
  18.                 TUF.player.masterloot:Hide()
  19.             elseif mlpID == 0 then
  20.                 TUF.player.masterloot:Show()
  21.             end
  22.         end
  23.         if IsResting() then TUF.player.rest:Show() else TUF.player.rest:Hide() end
  24.         TextUnitFramesLiteDB.general.elapsed = 0
  25.     end
 
08-02-12, 10:28 AM   #2
Talyrius
An Onyxian Warder
 
Talyrius's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 363
UnitPower(unit) and UnitPowerMax(unit) can both return 0 in same cases. You need to account for that.

Last edited by Talyrius : 08-02-12 at 10:33 AM.
 
08-02-12, 02:25 PM   #3
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
UnitPowerMax returns 0 for units that do not use mana/rage/etc, such as many "warrior" type NPCs.

On a side note, your code is really inefficient.

(1) Instead of calling, for example, UnitPowerType 5 times in a row to check each power type, you should call it just once, and assign its return value to a variable, and then check that variable instead of calling the function again. You do the same thing with UnitPower, UnitPowerMax, etc.

(2) Instead of doing both string concatention (eg. a .. b) and string formatting (eg. string.format), you should just use one or the other. If you need formatting (eg. %.0f) put everything into string.format.

(3) Instead of formatting text yourself with string.format and then passing the result to :SetText, you should pass all of the arguments directly to :SetFormattedText, which does the same thing but offloads the work into C code, which is (presumably) faster.

(4) Less an efficiency issue and more a logic issue, but there are many power types other than the 0 for mana, 3 for energy, 1 for rage, 5 for runes, and 6 for runic power that you check for. Your "else" check will catch not only focus, but also soul shards, eclipse, holy power, chi, shadow orbs, burning embers, demonic fury, and any "alternate power" used by vehicles or special encounter mechanics. Also, you probably don't want to support runes here, as death knights will constantly see their mana bar switching between runic power (which is what they expect a mana bar to show) and runes (which I'm actually not sure how a single bar displays anyway). You should explicitly check for the power types that make sense to show on the mana bar, and ignore the others. Oh, and happiness was removed in Cataclysm, so it no longer exists as a power type.

(5) Another logic error, you only color the name text if the unit has a class. This means that if you target a unit without a class the name will remain colored by the class of the previous target. Also, the classes returned for NPCs are usually not relevant, so it would make more sense to color their names by reaction.

(6) Finally, you wrongly assume that a unit with energy is a rogue (or cat druid) with combo points, and you are using the GetComboPoints function incorrectly. Monks use energy, but do not have combo points. As for GetComboPoints, it has taken two arguments for several years now, as you can build combo points on more than one target separately. The usual use is GetComboPoints("player", "target") to find out how many CP the player has on the target.

Compare this with the snippet you posted:
Lua Code:
  1. local firstload = true
  2.  
  3. -- upvalue frequently accessed globals for faster access:
  4. local db = TextUnitFramesLiteDB
  5. local format = string.format
  6.  
  7. -- store the player's class to check stuff later:
  8. local _, playerClass = UnitClass("player")
  9.  
  10. function TUF:TargetUpdate()
  11.     if firstload and not db.general.combat then
  12.         firstload = false
  13.         target:SetPoint(db.target.loc.p, UIParent, db.target.loc.rP, db.target.loc.x, db.target.loc.y)
  14.     end
  15.     if UnitExists("target") then
  16.         self:LockTargetFrame()
  17.  
  18.         local fg, fn = UnitFactionGroup("target")
  19.         if fg then -- explicitly checking ~= nil is unnecessary and wastes time
  20.             target.faction:SetTexture("Interface\\GROUPFRAME\\UI-Group-PVP-"..fg)
  21.         else
  22.             target.faction:SetTexture("Interface\\PVPFrame\\PVP-ArenaPoints-Icon")
  23.         end
  24.         target.faction:Show()
  25.  
  26.         target.level:SetPoint("TOPLEFT", target, "TOPLEFT", 5, -5)
  27.  
  28.         target.name:SetPoint("LEFT", target.level, "RIGHT", 5, 0)
  29.  
  30.         if db.target.orientation then
  31.             target.health:ClearAllPoints()
  32.             target.mana:ClearAllPoints()
  33.             target.health:SetPoint("TOPLEFT", target.level, "BOTTOMLEFT", 0, 0)
  34.             target.mana:SetPoint("TOPLEFT", target.health, "BOTTOMLEFT", 0, 0)
  35.         else
  36.             target.health:ClearAllPoints()
  37.             target.mana:ClearAllPoints()
  38.             target.health:SetPoint("LEFT", target.name, "RIGHT", 5, 0)
  39.             target.mana:SetPoint("LEFT", target.health, "RIGHT", 0, 0)
  40.         end
  41.  
  42.         target.level:SetFontObject(GameFontNormalLarge)
  43.         target.name:SetFontObject(GameFontNormalHuge)
  44.         target.health:SetFontObject(NumberFontNormalLarge)
  45.         target.mana:SetFontObject(NumberFontNormalLarge)
  46.  
  47.         target.level:SetText(UnitLevel("target"))
  48.  
  49.         target.name:SetText(UnitName("target"))
  50.         local _, cls = UnitClass("target")
  51.         if cls and UnitIsPlayer("target") then
  52.             -- support the CUSTOM_CLASS_COLORS standard used by !ClassColors and some others:
  53.             target.name:SetTextColor((CUSTOM_CLASS_COLORS or RAID_CLASS_COLORS)[cls].r, (CUSTOM_CLASS_COLORS or RAID_CLASS_COLORS)[cls].g, (CUSTOM_CLASS_COLORS or RAID_CLASS_COLORS)[cls].b)
  54.         elseif UnitIsFriend("target", "player") then
  55.             -- friendly, color it green
  56.             target.name:SetTextColor(0, 1, 0)
  57.         else
  58.             -- hostile, color it red
  59.             target.name:SetTextColor(1, 0, 0)
  60.         end
  61.  
  62.         -- get these values once, and reuse them:
  63.         local hp, hpmax = UnitHealth("target"), UnitHealthMax("target")
  64.         local hpperc = hp / hpmax * 100
  65.  
  66.         local htxtclr = TUF:SetTextColor(hpperc)
  67.         target.health:SetTextColor(htxtclr.r, htxtclr.g, htxtclr.b, 1)
  68.  
  69.         -- get these values once, and reuse them:
  70.         local pp, ppmax, pptype = UnitPower("target"), UnitPowerMax("target"), UnitPowerType("target")
  71.  
  72.         local mtxtclr = TUF:SetTextColor(pp / ppmax * 100)
  73.         target.mana:SetTextColor(mtxtclr.r, mtxtclr.g, mtxtclr.b, 1)
  74.  
  75.         -- concatenation + string.format is inefficient; do it all in string.format!
  76.         -- better yet, use :SetFormattedText instead of string.format + :SetText to offload the work into faster C code:
  77.         if db.target.percent then
  78.             target.health:SetFormattedText("|cff00ff00H|r-%.0f%%", hpperc)
  79.         else
  80.             target.health:SetFormattedText("|cff00ff00H|r- %d|Cff666666 /|r%d", hp, hpmax)
  81.         end
  82.        
  83.         -- also, since most of these are the same regardless of whether "db.target.percent" is true, no need to duplicate:
  84.         if pptype == 0 then -- mana
  85.             if db.target.percent then
  86.                 target.mana:SetFormattedText("|cff3300cc M|r-%.0f%%", pp / ppmax * 100)
  87.             else
  88.                 target.mana:SetFormattedText("|cff3300cc M|r-%d|Cff666666 /|r%d", pp, ppmax)
  89.             end
  90.  
  91.         elseif pptype == 1 then -- rage
  92.             target.mana:SetFormattedText("|cffff3333Rage|r- %d|cff666666 /|r%d", pp, ppmax)
  93.  
  94.         elseif pptype == 2 then -- focus
  95.             target.mana:SetFormattedText("|cffffcc00F|r- %d|cff666666 /|r%d", pp, ppmax)
  96.  
  97.         elseif pptype == 3 then -- energy
  98.             if playerClass == "ROGUE" or playerClass == "DRUID" then
  99.                 -- can have combo points
  100.                 target.mana:SetFormattedText("|cffffff00E |r-%d |cff990099 Combo|r: %d", mp, GetComboPoints("player", "target"))
  101.             else
  102.                 -- probably a monk, no combo points
  103.                 target.mana:SetFormattedText("|cffffff00E |r-%d|cff666666 /|r%d", pp, ppmax)
  104.             end
  105.  
  106.         elseif pptype == 6 then -- runic power
  107.             target.mana:SetFormattedText("|cff99ccffRP|r- %d|cff666666 /|r%d", pp, ppmax)
  108.         end
  109.         -- ignore other power types since they are additional resources, not mana equivalents.
  110.  
  111.         -- instead of getting a bunch of values and then not using them, check first:
  112.         if not db.general.combat then
  113.             local ll = target.level:GetStringWidth()
  114.             local nl = target.name:GetStringWidth()
  115.             local hl = target.health:GetStringWidth()
  116.             local ml = target.mana:GetStringWidth()
  117.             -- instead of checking "if not x then A, elseif x then B" check "if x then B, else A":
  118.             if db.target.orientation then
  119.                 local len = math.max(ll + nl, ml, hl)
  120.                 target:SetWidth(len + 20 )
  121.                 local lh = target.level:GetHeight()
  122.                 local hh = target.health:GetHeight()
  123.                 local mh = target.mana:GetHeight()
  124.                 target:SetHeight(lh + hh + mh + 20)
  125.             else
  126.                 local len = ll + nl + hl + ml + 20
  127.                 target:SetWidth(len)
  128.                 target:SetHeight(target.level:GetHeight() + 20)
  129.             end
  130.         end
  131.  
  132.         if UnitExists("playertargettarget") then
  133.             if db.target.targettop then
  134.                 target.target:ClearAllPoints()
  135.                 target.target:SetPoint("TOPLEFT", target, "BOTTOMLEFT", 10, 10)
  136.             else
  137.                 target.target:ClearAllPoints()
  138.                 target.target:SetPoint("LEFT", target, "RIGHT", -10, 0)
  139.             end
  140.             target.target.level:SetFontObject(GameFontNormal)
  141.             target.target.level:SetText(UnitLevel("playertargettarget"))
  142.             target.target.level:SetPoint("TOPLEFT", target.target, "TOPLEFT", 5, -5)
  143.  
  144.             target.target.name:SetFontObject(GameFontNormal)
  145.             target.target.name:SetText(UnitName("playertargettarget"))
  146.             target.target.name:SetPoint("LEFT", target.target.level, "RIGHT", 5, 0)
  147.  
  148.             local ttlw = target.target.level:GetStringWidth()
  149.             local ttnw = target.target.name:GetStringWidth()
  150.             target.target:SetHeight(target.target.name:GetHeight()+12)
  151.             target.target:SetWidth(ttlw + ttnw + 10)
  152.         end
  153.  
  154.         if not db.general.combat then
  155.             if db.target.enabled then target:Show() else target:Hide() end
  156.             if UnitExists("target") then target:Show() else target:Hide() end
  157.             if db.target.target and UnitExists("playertargettarget") then target.target:Show() else target.target:Hide() end
  158.         end
  159.     else
  160.         target:Hide()
  161.     end
  162. end

There are probably a lot of other improvements to be made, just in that section alone, but that's all I have time for right now. Hopefully you can look over that and get a feel for what to work on.
__________________
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.
 
08-02-12, 06:56 PM   #4
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
To explain why it doesn't work now, but seems like it did before, the client used to fail silently when trying to divide by 0. It still happened, though.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

 
08-02-12, 09:37 PM   #5
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
Originally Posted by Phanx View Post
<snip>
(3) Instead of formatting text yourself with string.format and then passing the result to :SetText, you should pass all of the arguments directly to :SetFormattedText, which does the same thing but offloads the work into C code, which is (presumably) faster.
Actually, the biggest reason to use this is that it goes straight to the C-side, thereby bypassing the creation of a Lua string. Since Lua strings are immutable, this can considerably reduce memory usage.
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.
 
08-03-12, 07:14 AM   #6
Farmbuyer
A Cyclonian
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 43
Originally Posted by Phanx View Post
(4) Less an efficiency issue and more a logic issue, but there are many power types other than the 0 for mana, 3 for energy, 1 for rage, 5 for runes, and 6 for runic power that you check for.
Also, they have useful names:
Code:
% grep SPELL_POWER FrameXML/Constants.lua
SPELL_POWER_MANA = 0;
SPELL_POWER_RAGE = 1;
SPELL_POWER_FOCUS = 2;
SPELL_POWER_ENERGY = 3;
SPELL_POWER_UNUSED = 4;
SPELL_POWER_RUNES = 5;
SPELL_POWER_RUNIC_POWER = 6;
SPELL_POWER_SOUL_SHARDS = 7;
SPELL_POWER_ECLIPSE = 8;
SPELL_POWER_HOLY_POWER = 9;
SPELL_POWER_ALTERNATE_POWER = 10;
%
 
08-03-12, 10:07 AM   #7
wolftech
A Deviate Faerie Dragon
Join Date: May 2008
Posts: 14
Thanks for your help. This is someone else's addon that I was updating (I had got his permission and was going to joint release it as both a lite version and a full version) but my personal coding skills are poor (about the best I can do is alter someone else's code. I am not a programmer by any stretch of my imagination) but it looks like I will have to try and rebuild this whole thing from scratch or abandon it. Well if I persevere I will be able to call myself an addon author.
 
08-03-12, 02:22 PM   #8
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
Originally Posted by Seerah View Post
To explain why it doesn't work now, but seems like it did before, the client used to fail silently when trying to divide by 0. It still happened, though.
Actually, it didn't fail at all, not even silently. Lua has always been able to convert divide-by-zero into a value that represents infinity. Any other operation that would result in an undefined value including 0/0 would result of a value representing NaN (Not-a-Number). The floating-point specification used is known as IEEE 754-1985. Starting with MoP beta, Blizzard changed this behavior to start throwing errors instead.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 08-03-12 at 02:33 PM.
 
08-03-12, 09:17 PM   #9
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by Farmbuyer View Post
Also, they have useful names:
Code:
% grep SPELL_POWER FrameXML/Constants.lua
SPELL_POWER_MANA = 0;
SPELL_POWER_RAGE = 1;
SPELL_POWER_FOCUS = 2;
SPELL_POWER_ENERGY = 3;
SPELL_POWER_UNUSED = 4;
SPELL_POWER_RUNES = 5;
SPELL_POWER_RUNIC_POWER = 6;
SPELL_POWER_SOUL_SHARDS = 7;
SPELL_POWER_ECLIPSE = 8;
SPELL_POWER_HOLY_POWER = 9;
SPELL_POWER_ALTERNATE_POWER = 10;
%
Yeah, but if you use those, make sure you upvalue them so you're not doing a bunch of global lookups.
__________________
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.
 
 

WoWInterface » Site Forums » Archived Beta Forums » MoP Beta archived threads » Help! What is this n00b doing wrong?

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