View Single Post
11-15-16, 02:20 PM   #23
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,322
Originally Posted by _Max_Cavalera_ View Post
Lua Code:
  1. MainFrame:SetShown(itemid and true or false);
Can you explain this line to me? I think that it's supposed to be, show frame if itemID exists?! but I don't get how the and true or false works.
This is basically a hack to implement inline conditions in Lua since there isn't a native method provided. frame:SetShown() either shows or hides a frame depending on the boolean value you pass to it. It shows the frame on true and hides it on false. The expression passed to it is the inline condition in question. The idea is if you have an expression A and B or C, it returns B if A is a true condition or C if not. There is an inherent bug in this expression though. If B evaluates to a false condition, C will always be returned no matter what A is. Keep this in mind if you decide to play around with it.


Originally Posted by _Max_Cavalera_ View Post
Backgrounds... do I really need them? I know I had one, I made it because I was learning from a book and it listed the background as something to use when making a frame but as you know mine wasn't even working and i didn't even notice it because it was already supposed to be almost invisible at 0.2 alpha.
It works without one so I guess there's no problem in not having one? Or can it create problems?
The background is just a visual element to make the text more readable, you can change or remove it if you wish.



Originally Posted by _Max_Cavalera_ View Post
Lua Code:
  1. (available>0 and " (+%d)" or "")
You have this on the formatted text, I guess it's supposed to be if available > 0 do the first one else do the other one?!?! So it's like an IF statement to use inside strings?
This is another example of the inline conditional explained earlier. If available>0 is true then it inserts the format specifier for it.


Originally Posted by _Max_Cavalera_ View Post
Can you explain why %5$d and %6$d is needed to grab the 5th and 6th parameter when the position of those in the string are the 5th and 6th? Shouldn't they already pick up the right ones with just %d? Or is it \n that breaks that?
By the way, I didn't know I could do that so, thanks
This is a modification to string.format() made by the WoW API and isn't normal for Lua itself. I needed to explain the inline condition used here before explaining this so you get the idea. The inline condition decided whether or not to insert the 4th arg into the string, making the identifiers for the 5th and 6th args prone to shifting. Manually selecting which arg the identifier references fixes this problem.


Originally Posted by _Max_Cavalera_ View Post
So, that nil because of the knowledge level 0. I totally get it why that is happening, it was trying to get multiplier[0] which doesn't exist since it starts at 1... BUT... on my original code I was doing the same mistake by accident, and it never gave me an error, not only that but it was actually working correctly, giving me on the output knowledge level 0 (+0%)... that's kinda strange, no?
An alternate fix would've been to define 0 at key 0 in your table. This is done by specifying key = value in a line of your table. If you use square brackets, it evaluates the key as an expression rather than trying to register it as a string. Using this, [0] = 0 writes 0 at index 0 in the table.
Lua Code:
  1. local akMulti = {
  2.     [0] = 0,
  3.     25, 50, 90, 140, 200,
  4.     275, 375, 500, 650, 850,
  5.     1100, 1400, 1775, 2250, 2850,
  6.     3600, 4550, 5700, 7200, 9000,
  7.     11300, 14200, 17800, 22300, 24900
  8. };

You can use another inline conditional to hide the AK display if you don't have any.
Lua Code:
  1. text:SetFormattedText("AP |cff00ff00%d/%d (%.1f%%)|r" .. (pointsFree > 0 and " (+%d)" or "") .. (akLevel > 0 and "\nAK |cff00ff00%5$d (+%d%%)|r" or ""), totalXP, xpToNextPoint, 100 * totalXP / xpToNextPoint, pointsFree, akLevel, akMulti[akLevel]);
I also trimmed %6$d since the previous identifier would make it select the 6th arg anyway. This is an undocumented feature, so many of the internal processes are unknown. I initially found Blizzard using it in the localization strings and poked around with it in my own testing.



Another alternative would be to implement a fallback by using akMulti[akLevel] or 0. The or operator will make sure if the table returns nil, it'll use 0 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 : 11-15-16 at 02:43 PM.
  Reply With Quote