Thread Tools Display Modes
03-07-12, 05:35 PM   #1
zin99
A Deviate Faerie Dragon
Join Date: Nov 2010
Posts: 12
default unitframes healthtext script help

hi,

first of all: my lua knowledge isn't very advanced, i try to learn but eventually don'T know how to handle this problem, that's why i came here to ask for your help.

i picked up a script at arenajunkies (yeah i know, a lot of them are poorly coded) that reduces the health and power text on statusbars of the unitframes to only their current values (e.g. 150k instead of 150k/150k).

this script used an OnUpdate function which made it consume nearly 5 times as much cpu cycles as the entire rest of my ui, and i wanted to make more performance friendly.

i tried to achieve this in two ways ways but none of them really worked - first i read an article about how to throttle an OnUpdate function but i wasn't able to properly adapt it to this script and then i looked at code code of thek unitframes, where the healthtext is entirely updated based on certain events.

this is what i came up with but it's far from good - when mouseovering the frame or even every 2 or 3 tenth of second it randomly switches back to the default display of the text string. i hope some can take some time and look at this code, thanks in advance.

Code:
local CurrentValues = CreateFrame("Frame")

CurrentValues:RegisterEvent("UNIT_HEALTH");
CurrentValues:RegisterEvent("PLAYER_ENTERING_WORLD");
CurrentValues:RegisterEvent("PLAYER_TARGET_CHANGED");
CurrentValues:RegisterEvent("PLAYER_FOCUS_CHANGED");

local function HPMP(self, event, ...)

                local PlayerHealth = UnitHealth("player")
                local PlayerMana = UnitMana("player")
                
                local TargetHealth = UnitHealth("target")
                local TargetMana = UnitMana("target")
                
                local FocusHealth = UnitHealth("focus")
                local FocusMana = UnitMana("focus")
                
                
                PlayerFrameHealthBar.TextString:SetText(TextStatusBar_CapDisplayOfNumericValue(PlayerHealth))
                PlayerFrameManaBar.TextString:SetText(TextStatusBar_CapDisplayOfNumericValue(PlayerMana))
                
                TargetFrameHealthBar.TextString:SetText(TextStatusBar_CapDisplayOfNumericValue(TargetHealth))
                TargetFrameManaBar.TextString:SetText(TextStatusBar_CapDisplayOfNumericValue(TargetMana))
                
                FocusFrameHealthBar.TextString:SetText(TextStatusBar_CapDisplayOfNumericValue(FocusHealth))
                FocusFrameManaBar.TextString:SetText(TextStatusBar_CapDisplayOfNumericValue(FocusMana))
end
CurrentValues:SetScript("OnEvent", HPMP);
  Reply With Quote
03-07-12, 07:39 PM   #2
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Something like this would probably work better:
Lua Code:
  1. --  Make a list of all the text strings you want to affect:
  2. local textStrings = {
  3.     PlayerFrameHealthBar.TextString,
  4.     PlayerFrameManaBar.TextString,
  5.     TargetFrameHealthBar.TextString,
  6.     TargetFrameManaBar.TextString,
  7.     FocusFrameHealthBar.TextString,
  8.     FocusFrameManaBar.TextString,
  9. }
  10.  
  11. --  Post-hook the function that updates the text on the health and mana
  12. --  bars. This causes your function to be run every time the original
  13. --  function runs, after the original function, so you can overwrite
  14. --  changes made by the original function.
  15. hooksecurefunc("TextStatusBar_UpdateTextStringWithValues", function(statusFrame, textString, value, valueMin, valueMax)
  16.     -- Check to see if this text string is one you want to change:
  17.     if not textStrings[textString] then
  18.         --  You don't care about this text string, so quit:
  19.         return
  20.     end
  21.  
  22.     --  Get the new text to show on the bar:
  23.     local newText = TextStatusBar_CapDisplayOfNumericValue(value)
  24.  
  25.     --  Set the contents of the text string to that text:
  26.     textString:SetText(newText)
  27. end)
  Reply With Quote
03-08-12, 02:30 AM   #3
zin99
A Deviate Faerie Dragon
Join Date: Nov 2010
Posts: 12
thanks a lot phanx, i really appreciate your help. it seems to work perfectly right now
  Reply With Quote
03-08-12, 08:42 AM   #4
zin99
A Deviate Faerie Dragon
Join Date: Nov 2010
Posts: 12
actually i was wrong, and it wouldnt work (maybe i just implemented your code wrong, in generally expect the error to more likely be on my side concerning anything with lua ) but i found something.

obviously i found the default code for the textstring function on wowprogramming and tweaked it:

Code:
function TextStatusBar_UpdateTextStringWithValues(statusFrame, textString, value, valueMin, valueMax)
	if ( ( tonumber(valueMax) ~= valueMax or valueMax > 0 ) and not ( statusFrame.pauseUpdates ) ) then
		statusFrame:Show();
		if ( value and valueMax > 0 and ( GetCVarBool("statusTextPercentage") or statusFrame.showPercentage ) and not statusFrame.showNumeric) then
			if ( value == 0 and statusFrame.zeroText ) then
				textString:SetText(statusFrame.zeroText);
				statusFrame.isZero = 1;
				textString:Show();
				return;
			end
			value = tostring(math.ceil((value / valueMax) * 100)) .. "%";
			if ( statusFrame.prefix and (statusFrame.alwaysPrefix or not (statusFrame.cvar and GetCVar(statusFrame.cvar) == "1" and statusFrame.textLockable) ) ) then
				textString:SetText(statusFrame.prefix .. " " .. value);
			else
				textString:SetText(value);
			end
		elseif ( value == 0 and statusFrame.zeroText ) then
			textString:SetText(statusFrame.zeroText);
			statusFrame.isZero = 1;
			textString:Show();
			return;
		else
			statusFrame.isZero = nil;
			if ( statusFrame.capNumericDisplay ) then
				value = TextStatusBar_CapDisplayOfNumericValue(value);
				valueMax = TextStatusBar_CapDisplayOfNumericValue(valueMax);
			end
			if ( statusFrame.prefix and (statusFrame.alwaysPrefix or not (statusFrame.cvar and GetCVar(statusFrame.cvar) == "1" and statusFrame.textLockable) ) ) then
				textString:SetText(statusFrame.prefix.." "..value.." / "..valueMax);
			else
				textString:SetText(value);
-- ORIGINAL:		textString:SetText(value.." / "..valueMax);
			end
		end
		
		if ( (statusFrame.cvar and GetCVar(statusFrame.cvar) == "1" and statusFrame.textLockable) or statusFrame.forceShow ) then
			textString:Show();
		elseif ( statusFrame.lockShow > 0 and (not statusFrame.forceHideText) ) then
			textString:Show();
		else
			textString:Hide();
		end
	else
		textString:Hide();
		textString:SetText("");
		if ( not statusFrame.alwaysShow ) then
			statusFrame:Hide();
		else
			statusFrame:SetValue(0);
		end
	end
end
it works, but my question now is:
is there any downside of having this functions in a seperate custom lua file (obv. additionaly to the place where they are located normally). performance seems fine, like 0.05 cpu cycles/sec.
  Reply With Quote
03-09-12, 06:23 AM   #5
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Well, simply replacing a function is always more efficient, performance-wise, than post-hooking it, since only one function (yours) runs instead of two (the original plus yours). There are some situational disadvantages, though.

First, replacing functions breaks other addons that have already hooked the function, because their hooks were attached to the original function, and there's no way to transfer that attachment to your new function. If you're only using the code for yourself, it's not a big deal, but it's something to keep in mind.

Second, replacing functions, or insecurely hooking them, introduces taint. If the function, or any values it returns, or anything it touches while running, is used by a secure frame, you can break functionality this way.
  Reply With Quote
03-09-12, 06:34 AM   #6
zin99
A Deviate Faerie Dragon
Join Date: Nov 2010
Posts: 12
again thanx for being so patient and explaining stuff to a beginner phanx.

i think i'll leave it as it is for now, since i couldnt get the post hook function to work. so far i did not experience any lua errors und reduced the overall cpu load the script had in the beginning by roughly 2000%.

it's really interesting how such small pieces of code, when poorly written, can impact performance. in this case it even "outcycled" my extremely complex pve tell me when setup.

Last edited by zin99 : 03-09-12 at 06:37 AM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » default unitframes healthtext script help


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