Thread Tools Display Modes
05-18-13, 06:19 PM   #1
10leej
A Molten Giant
 
10leej's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2011
Posts: 583
FontString for value and percent?

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
Attached Thumbnails
Click image for larger version

Name:	Capture.PNG
Views:	345
Size:	301.2 KB
ID:	7713  
__________________
Tweets YouTube Website

Last edited by 10leej : 05-18-13 at 06:36 PM.
  Reply With Quote
05-18-13, 09:43 PM   #2
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
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)
__________________
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.
  Reply With Quote
05-18-13, 11:04 PM   #3
10leej
A Molten Giant
 
10leej's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2011
Posts: 583
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
__________________
Tweets YouTube Website

Last edited by 10leej : 05-18-13 at 11:10 PM.
  Reply With Quote
05-19-13, 04:56 AM   #4
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
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.
  Reply With Quote
05-19-13, 05:05 AM   #5
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,060
Also UnitHealth and UnitHealthMax need to be inside of the update function or their values will never change.
  Reply With Quote
05-19-13, 06:15 AM   #6
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
Originally Posted by myrroddin View Post
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.
  Reply With Quote
05-19-13, 09:04 AM   #7
10leej
A Molten Giant
 
10leej's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2011
Posts: 583
Originally Posted by Resike View Post
Some men, just want to watch the taints burn.
But, I like tainting everything !

MWAHAHAHAHAHAHAHAHAHAHHAHAHAHA
__________________
Tweets YouTube Website
  Reply With Quote
05-19-13, 11:30 AM   #8
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,877
If i-taint worth tainting i-taint worth doing
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.
  Reply With Quote
05-19-13, 02:34 PM   #9
10leej
A Molten Giant
 
10leej's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2011
Posts: 583
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)
__________________
Tweets YouTube Website

Last edited by 10leej : 05-19-13 at 02:38 PM.
  Reply With Quote
05-19-13, 03:24 PM   #10
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
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)
__________________
"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

  Reply With Quote
05-19-13, 04:13 PM   #11
10leej
A Molten Giant
 
10leej's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2011
Posts: 583
Originally Posted by Seerah View Post
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!
Attached Thumbnails
Click image for larger version

Name:	Capture.PNG
Views:	258
Size:	262.5 KB
ID:	7714  
__________________
Tweets YouTube Website
  Reply With Quote
05-19-13, 05:56 PM   #12
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
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)
__________________
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.
  Reply With Quote
05-20-13, 04:07 PM   #13
10leej
A Molten Giant
 
10leej's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2011
Posts: 583
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
__________________
Tweets YouTube Website
  Reply With Quote
05-20-13, 06:41 PM   #14
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Well, there is nothing in that code that does anything to any statusbar, so that seems correct.
__________________
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.
  Reply With Quote
05-20-13, 07:16 PM   #15
10leej
A Molten Giant
 
10leej's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2011
Posts: 583
Originally Posted by Phanx View Post
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....
__________________
Tweets YouTube Website
  Reply With Quote
05-24-13, 11:59 AM   #16
Aanson
A Flamescale Wyrmkin
Join Date: Aug 2009
Posts: 124
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.
__________________
__________________
  Reply With Quote
05-24-13, 12:01 PM   #17
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Why hook your own code when you can just save the value to a variable?
__________________
"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

  Reply With Quote
05-24-13, 12:23 PM   #18
Aanson
A Flamescale Wyrmkin
Join Date: Aug 2009
Posts: 124
Originally Posted by Seerah View Post
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);
__________________
__________________
  Reply With Quote
05-24-13, 01:57 PM   #19
10leej
A Molten Giant
 
10leej's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2011
Posts: 583
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
__________________
Tweets YouTube Website
  Reply With Quote
05-24-13, 02:12 PM   #20
Aanson
A Flamescale Wyrmkin
Join Date: Aug 2009
Posts: 124
Haha!! I'm sure it's 6 and half a dozen
__________________
__________________
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » FontString for value and percent?

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