WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   FontString for value and percent? (https://www.wowinterface.com/forums/showthread.php?t=46486)

10leej 05-18-13 06:19 PM

FontString for value and percent?
 
1 Attachment(s)
How would I go about this? Never dealt with FontStrings before (my UI uses whoa unitframes, which I have no idea as to what I'm looking at when i look there)

I know a basic fontsring can be done doing this
Lua Code:
  1. frame = f:CreateFontString(nil,"ARTWORK","GameFontHighlight")
  2. frame:SetPoint("BOTTOM",frame,0,0)
  3. frame:SetText("health text")
wouldn't mind having something like this, course that example is using the script I have above

Phanx 05-18-13 09:43 PM

I'm not really sure what you're looking for. The code you posted should work, though you probably want to give it a more useful name so you can access it later:

Code:

f.HealthText = f:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
f.HealthText:SetPoint("BOTTOM")

Then whenever you want to make it show some other text, just set its text:

Code:

f.HealthText:SetText("15%")
If your text involves any string.format or concatenation, use :SetFormattedText instead of :SetText to move the formatting C-side:

Code:

local currentHealth, maxHealth = UnitHealth("player"), UnitHealthMax("player")
f.HealthText:SetFormattedText("%d/%d", currentHealth, maxHealth)


10leej 05-18-13 11:04 PM

I'm making health text for a HUD addon I'm working on I got the bars updating I just gotta work on the text that I've honestly never dealth with before.

Then if I want the numbers to run and update with health changes I can just do this?
Lua Code:
  1. f.HealthText = f:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
  2. f.HealthText:SetPoint("BOTTOM",f.hp,0,0)
  3. local currentHealth, maxHealth = UnitHealth("player"), UnitHealthMax("player")
  4. local function update(self)
  5.   local unit = self.unit
  6.   f.PowerText:SetFormattedText("%d/%d", currentPower, maxPower)
  7. f:SetScript("OnEvent",update)
  8. f:RegisterUnitEvent("UNIT_HEALTH",f.unit)
  9.  
  10. update(f) --initial update

myrroddin 05-19-13 04:56 AM

Are you assigning currentHealth/maxHealth or currentPower/maxPower? Your code retrieves the former and (fails to display) the latter.

Also, whether you use "frame" or "f" please, for the love of the Light, make that a local variable.

semlar 05-19-13 05:05 AM

Also UnitHealth and UnitHealthMax need to be inside of the update function or their values will never change.

Resike 05-19-13 06:15 AM

Quote:

Originally Posted by myrroddin (Post 278286)
Are you assigning currentHealth/maxHealth or currentPower/maxPower? Your code retrieves the former and (fails to display) the latter.

Also, whether you use "frame" or "f" please, for the love of the Light, make that a local variable.

Some men, just want to watch the taints burn.

10leej 05-19-13 09:04 AM

Quote:

Originally Posted by Resike (Post 278288)
Some men, just want to watch the taints burn.

But, I like tainting everything !

MWAHAHAHAHAHAHAHAHAHAHHAHAHAHA

Fizzlemizz 05-19-13 11:30 AM

If i-taint worth tainting i-taint worth doing :p

10leej 05-19-13 02:34 PM

Anyways I got it working now for the value text, how do I get it to show percentage as well? (I honestly can't find information on it so I'm sorry to bother you guys)

I imagine it's simply just make another frame for the text but rather than using "%d/%d" just use something else (not sure what though)
Lua Code:
  1. local currentHealthPercent, maxHealthPercent = UnitHealth("player"), UnitHealthMax("player")
  2. f.HealthPercentText:SetFormattedText("%d/%d", currentHealthPercent, maxHealthPercent)
  3. --Updates the status bar to new health
  4. local function update(self)
  5.   local unit = self.unit
  6.   --update health percentage text
  7.   local currentHealthPercent, maxHealthPercent = UnitHealth("player"), UnitHealthMax("player")
  8.   f.HealthPercentText:SetFormattedText("%d/%d", currentHealthPercent, maxHealthPercent)
  9.   --update health value text
  10.   local currentHealth, maxHealth = UnitHealth("player"), UnitHealthMax("player")
  11.   f.HealthText:SetFormattedText("%d/%d", currentHealth, maxHealth)
  12. end
  13. f:SetScript("OnEvent",update)
  14. f:RegisterUnitEvent("UNIT_HEALTH",f.unit)
  15. f:RegisterUnitEvent("UNIT_POWER",f.unit)

Seerah 05-19-13 03:24 PM

Why are your variables named currentHealthPercent and maxHealthPercent. They're just your current and max health. Don't create misleading variable names.

To do the percent, you just calculate it. (ie, divide and x100)

10leej 05-19-13 04:13 PM

1 Attachment(s)
Quote:

Originally Posted by Seerah (Post 278304)
Why are your variables named currentHealthPercent and maxHealthPercent. They're just your current and max health. Don't create misleading variable names.

To do the percent, you just calculate it. (ie, divide and x100)

I guessed on it, but if I didn;t have to rename them for the second fontstring then i didn't

But, I did finally get the text working, thanks for the help everyone!

Phanx 05-19-13 05:56 PM

Code:

f:SetScript("OnEvent", function(self, event, unit)
        if event == "UNIT_HEALTH" then
                local currentHealth, maxHealth = UnitHealth(unit), UnitHealthMax(unit)
                local percentHealth = currentHealth / maxHealth * 100
                self.HealthText:SetFormattedText("%d/%d (%d%%)", currentHealth, maxHealth, percentHealth)
        elseif event == "UNIT_POWER" then
                local currentPower, maxPower = UnitPower(unit), UnitPowerMax(unit)
                local percentPower = currentPower / maxPower * 100
                self.PowerText:SetFormattedText("%d/%d (%d%%)", currentPower, maxPower, percentPower)
        end
end)
f:RegisterUnitEvent("UNIT_HEALTH", f.unit)
f:RegisterUnitEvent("UNIT_POWER", f.unit)

You should only update the portions relevant to the event that fired, and you shouldn't need to manually update on load unless your code is loading very late. If you notice wrong values on login, just add this at the end:

Code:

f:GetScript("OnEvent")(f, "UNIT_HEALTH", f.unit)
f:GetScript("OnEvent")(f, "UNIT_POWER", f.unit)


10leej 05-20-13 04:07 PM

Seems you code doesn't update the statusbars for 5.3 but the fontstrings do. Not sure why yet as I'm not getting any errors need to do some more testing with it

Phanx 05-20-13 06:41 PM

Well, there is nothing in that code that does anything to any statusbar, so that seems correct.

10leej 05-20-13 07:16 PM

Quote:

Originally Posted by Phanx (Post 278332)
Well, there is nothing in that code that does anything to any statusbar, so that seems correct.

Well it's monday I'm drunk so guess i missed that part....

Aanson 05-24-13 11:59 AM

I don't know what others would think about this, but if I was setting up a HUD, I would personally do:

1. Only the health bar and power/mana bar get their values from the UnitHealth("player"), UnitHealthMax("player"), and the power/mana equivalent funcs.

2. I would then hook everything else, including text to the OnValueChanged() handler of the bars.

That way, only 1 object is calling the health funcs and the same on the power/mana side.

I say this because you said you have the bars set up already, so presumably you're addOn is already being fed all the information that it needs.

Hope that makes sense.

Seerah 05-24-13 12:01 PM

Why hook your own code when you can just save the value to a variable? ;)

Aanson 05-24-13 12:23 PM

Quote:

Originally Posted by Seerah (Post 278578)
Why hook your own code when you can just save the value to a variable? ;)

Yeah true, you could always contain the data in local UpValues.

It makes little difference though because you'd still need to be constantly updating the value of your UpValues anyway.

Hooking to OnValueChanged just seems like the cleanest way to me. ie.

Lua Code:
  1. myHealthBar:SetScript("OnValueChanged", function(self, health)
  2.     local _, maxHealth = self:GetMinMaxValues();
  3.     healthText:SetFormattedText("%d/%d", health, maxHealth);
  4.     -- update other things here too
  5. end);

10leej 05-24-13 01:57 PM

NO DON'T DO THIS TO ME I already got the fontstrings working as they are, not gonna fix it if they ain't broke, lmao

Aanson 05-24-13 02:12 PM

Haha!! I'm sure it's 6 and half a dozen :)


All times are GMT -6. The time now is 04:07 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI