Go to Page... |
Thread Tools | Display Modes |
08-02-12, 10:09 AM | #1 |
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:
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 Lua Code:
|
|
08-02-12, 10:28 AM | #2 |
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 |
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:
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 |
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.
|
|
08-02-12, 09:37 PM | #5 |
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 | |
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 |
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 | ||
__________________
WoWInterface AddOns
Last edited by SDPhantom : 08-03-12 at 02:33 PM. |
|||
08-03-12, 09:17 PM | #9 | |
__________________
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? |
«
Previous Thread
|
Next Thread
»
|
Display Modes |
Linear Mode |
Switch to Hybrid Mode |
Switch to Threaded Mode |
|
|