Thread Tools Display Modes
06-03-11, 01:12 AM   #1
Aprikot
A Frostmaul Preserver
 
Aprikot's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2010
Posts: 284
Exp bar color

Hello, I'm looking for a little help in understanding why this code does not successfully change the default exp status bar color:

lua Code:
  1. hooksecurefunc("ExhaustionTick_OnEvent", function(self, event, ...)
  2.     MainMenuExpBar:SetStatusBarColor(1, 1, 1, 1)
  3. end);

Also tried:

lua Code:
  1. hooksecurefunc("ExhaustionTick_OnEvent", function(self, event, ...)
  2.     MainMenuExpBar:SetStatusBarColor(1, 1, 1, 1)
  3.     if (exhaustionStateID == 1) or (exhaustionStateID == 2) then
  4.         MainMenuExpBar:SetStatusBarColor(1, 1, 1, 1)
  5.     end
  6. end);

Below is the full function from Blizzard's MainMenuBar.lua. For giggles I used the whole thing in my hooksecurefunc() to no avail...I'm not seeing the SetStatusBarColor() values being applied. I appreciate any thoughts.

lua Code:
  1. function ExhaustionTick_OnEvent(self, event, ...)
  2.     if ((event == "PLAYER_ENTERING_WORLD") or (event == "PLAYER_XP_UPDATE") or (event == "UPDATE_EXHAUSTION") or (event == "PLAYER_LEVEL_UP")) then
  3.         local playerCurrXP = UnitXP("player");
  4.         local playerMaxXP = UnitXPMax("player");
  5.         --local exhaustionCurrXP, exhaustionMaxXP;
  6.         --exhaustionCurrXP, exhaustionMaxXP = GetXPExhaustion();
  7.         local exhaustionThreshold = GetXPExhaustion();
  8.         local exhaustionStateID, exhaustionStateName, exhaustionStateMultiplier;
  9.         exhaustionStateID, exhaustionStateName, exhaustionStateMultiplier = GetRestState();
  10.         if (exhaustionStateID >= 3) then
  11.             ExhaustionTick:SetPoint("CENTER", "MainMenuExpBar", "RIGHT", 0, 0);
  12.         end
  13.  
  14.         if (not exhaustionThreshold) then
  15.             ExhaustionTick:Hide();
  16.             ExhaustionLevelFillBar:Hide();
  17.         else
  18.             local exhaustionTickSet = max(((playerCurrXP + exhaustionThreshold) / playerMaxXP) * MainMenuExpBar:GetWidth(), 0);
  19. --          local exhaustionTotalXP = playerCurrXP + (exhaustionMaxXP - exhaustionCurrXP);
  20. --          local exhaustionTickSet = (exhaustionTotalXP / playerMaxXP) * MainMenuExpBar:GetWidth();
  21.             ExhaustionTick:ClearAllPoints();
  22.             if (exhaustionTickSet > MainMenuExpBar:GetWidth() or MainMenuBarMaxLevelBar:IsShown()) then
  23.                 ExhaustionTick:Hide();
  24.                 ExhaustionLevelFillBar:Hide();
  25.                 -- Saving this code in case we want to always leave the exhaustion tick onscreen
  26. --              ExhaustionTick:SetPoint("CENTER", "MainMenuExpBar", "RIGHT", 0, 0);
  27. --              ExhaustionLevelFillBar:SetPoint("TOPRIGHT", "MainMenuExpBar", "TOPRIGHT", 0, 0);
  28.             else
  29.                 ExhaustionTick:Show();
  30.                 ExhaustionTick:SetPoint("CENTER", "MainMenuExpBar", "LEFT", exhaustionTickSet, 0);
  31.                 ExhaustionLevelFillBar:Show();
  32.                 ExhaustionLevelFillBar:SetPoint("TOPRIGHT", "MainMenuExpBar", "TOPLEFT", exhaustionTickSet, 0);
  33.             end
  34.         end
  35.  
  36.         -- Hide exhaustion tick if player is max level or XP is turned off
  37.         if ( UnitLevel("player") == MAX_PLAYER_LEVEL or IsXPUserDisabled() ) then
  38.             ExhaustionTick:Hide();
  39.         end
  40.     end
  41.     if ((event == "PLAYER_ENTERING_WORLD") or (event == "UPDATE_EXHAUSTION")) then
  42.         local exhaustionStateID = GetRestState();
  43.         if (exhaustionStateID == 1) then
  44.             MainMenuExpBar:SetStatusBarColor(0.0, 0.39, 0.88, 1.0);
  45.             ExhaustionLevelFillBar:SetVertexColor(0.0, 0.39, 0.88, 0.15);
  46.             ExhaustionTickHighlight:SetVertexColor(0.0, 0.39, 0.88);
  47.         elseif (exhaustionStateID == 2) then
  48.             MainMenuExpBar:SetStatusBarColor(0.58, 0.0, 0.55, 1.0);
  49.             ExhaustionLevelFillBar:SetVertexColor(0.58, 0.0, 0.55, 0.15);
  50.             ExhaustionTickHighlight:SetVertexColor(0.58, 0.0, 0.55);
  51.         end
  52.  
  53.     end
  54.     if ( not MainMenuExpBar:IsShown() ) then
  55.         ExhaustionTick:Hide();
  56.     end
  57. end
  Reply With Quote
06-03-11, 01:24 AM   #2
Nibelheim
local roygbi-
 
Nibelheim's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 1,600
Does it work if you simply enter this into your chat window?

Code:
/run MainMenuExpBar:SetStatusBarColor(1, 1, 1, 1)
  Reply With Quote
06-03-11, 07:13 PM   #3
Aprikot
A Frostmaul Preserver
 
Aprikot's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2010
Posts: 284
Originally Posted by Nibelheim View Post
Does it work if you simply enter this into your chat window?

Code:
/run MainMenuExpBar:SetStatusBarColor(1, 1, 1, 1)
Yep sure does...could I fail harder?! I know better too...

Thanks for the gentle delivery.
  Reply With Quote
06-03-11, 07:31 PM   #4
Nibelheim
local roygbi-
 
Nibelheim's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 1,600
Originally Posted by Aprikot View Post
Yep sure does...could I fail harder?! I know better too...

Thanks for the gentle delivery.
Hmm, so problem fixed?
  Reply With Quote
06-03-11, 08:10 PM   #5
Aprikot
A Frostmaul Preserver
 
Aprikot's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2010
Posts: 284
Originally Posted by Nibelheim View Post
Hmm, so problem fixed?
It's not working yet in my addon, but I think that might be due to the load order.

I'm going to try registering PLAYER_ENTERING_WORLD to see if that helps. I guess that's good practice anyway?
  Reply With Quote
06-03-11, 08:24 PM   #6
Nibelheim
local roygbi-
 
Nibelheim's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 1,600
Originally Posted by Aprikot View Post
It's not working yet in my addon, but I think that might be due to the load order.

I'm going to try registering PLAYER_ENTERING_WORLD to see if that helps. I guess that's good practice anyway?
It's not a bad idea. Just register PLAYER_LOGIN and do your hooks at that point.

Code:
f = CreateFrame("Frame")
f:RegisterEvent("PLAYER_LOGIN")
f:SetScript("OnEvent", function()
    MainMenuExpBar:SetStatusBarColor(1, 1, 1, 1)
    hooksecurefunc("ExhaustionTick_OnEvent", function()
        MainMenuExpBar:SetStatusBarColor(1, 1, 1, 1)
    end)
end)
  Reply With Quote
06-03-11, 10:57 PM   #7
Aprikot
A Frostmaul Preserver
 
Aprikot's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2010
Posts: 284
I can't seem to get the hooked function to work; the bar color defaults after update. I'm able get the desired result by registering the additional events to my frame:

Code:
local f = CreateFrame("Frame")
f:RegisterEvent("PLAYER_ENTERING_WORLD")
f:RegisterEvent("PLAYER_XP_UPDATE");
f:RegisterEvent("UPDATE_EXHAUSTION");
f:RegisterEvent("PLAYER_LEVEL_UP");
f:RegisterEvent("PLAYER_UPDATE_RESTING");
f:SetScript("OnEvent", function()
    MainMenuExpBar:SetStatusBarColor(1, 1, 1, 1) 
end)
Just not sure if this is a desirable method...
  Reply With Quote
07-27-11, 11:55 AM   #8
Aprikot
A Frostmaul Preserver
 
Aprikot's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2010
Posts: 284
I'm trying to do the same thing with the rep bar (recolor it), and having an issue where the bar will not recolor on load (but will recolor on exp gain):

lua Code:
  1. -- doesn't work:
  2. local ff = CreateFrame("Frame")
  3. ff:RegisterEvent("PLAYER_ENTERING_WORLD")
  4. ff:SetScript("OnEvent", function()
  5.     ReputationWatchStatusBar:SetStatusBarColor(1, 1, 1)
  6. end)
  7.  
  8. -- works:
  9. local ff = CreateFrame("Frame")
  10. ff:RegisterEvent("PLAYER_XP_UPDATE")
  11. ff:SetScript("OnEvent", function()
  12.     ReputationWatchStatusBar:SetStatusBarColor(1, 1, 1)
  13. end)

Any idea why this doesn't work with PLAYER_ENTERING_WORLD? It works for the exp bar (code in post above this).
  Reply With Quote
07-27-11, 12:23 PM   #9
Mischback
A Cobalt Mageweaver
 
Mischback's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2009
Posts: 221
Could you "noop" the original function?

Code:
local function noop()
    return
end

    MainMenuExpBar:SetStatusBarColor(1, 1, 1, 1)
    MainMenuExpBar.SetStatusBarColor = noop
__________________
  Reply With Quote
07-27-11, 12:45 PM   #10
Haleth
This Space For Rent
 
Haleth's Avatar
Featured
Join Date: Sep 2008
Posts: 1,173
@ OP; In your second attempt, you refer to exhaustionStateID without defining it. Local values are not inherited from hooked functions.

Try it when you define it as GetRestState(), as the original Blizzard function does.
  Reply With Quote
07-27-11, 01:00 PM   #11
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
Originally Posted by Aprikot View Post
... For giggles I used the whole thing in my hooksecurefunc() to no avail...I'm not seeing the SetStatusBarColor() values being applied. I appreciate any thoughts.
Here's a clip of MainMenuBar.xml...
xml Code:
  1. <Button name="ExhaustionTick" parent="MainMenuExpBar" hidden="false" frameStrata="DIALOG">
  2.     <Size>
  3.         <AbsDimension x="32" y="32"/>
  4.     </Size>
  5.     <Anchors>
  6.         <Anchor point="CENTER" relativeTo="MainMenuExpBar">
  7.             <Offset>
  8.                 <AbsDimension x="0" y="0"/>
  9.             </Offset>
  10.         </Anchor>
  11.     </Anchors>
  12.     <Scripts>
  13.         <OnLoad function="ExhaustionTick_OnLoad"/>
  14.         <OnEvent function="ExhaustionTick_OnEvent"/>
  15.         <OnEnter function="ExhaustionToolTipText"/>
  16.         <OnLeave function="GameTooltip_Hide"/>
  17.     </Scripts>
  18.     <NormalTexture name="ExhaustionTickNormal" file="Interface\MainMenuBar\UI-ExhaustionTickNormal"/>
  19.     <HighlightTexture name="ExhaustionTickHighlight" file="Interface\MainMenuBar\UI-ExhaustionTickHighlight" alphaMode="ADD"/>
  20. </Button>

Notice the way the script functions are registered. This grabs the function immediately and sets it to the associated script. hooksecurefunc() will not work on this. You'll have to hook the frame's script handler directly with something like this.
lua Code:
  1. ExhaustionTick:HookScript("OnEvent",function(self,event,...)
  2.     MainMenuExpBar:SetStatusBarColor(1,1,1,1);
  3. end);



Originally Posted by Aprikot View Post
I'm trying to do the same thing with the rep bar (recolor it), and having an issue where the bar will not recolor on load (but will recolor on exp gain):

...

Any idea why this doesn't work with PLAYER_ENTERING_WORLD? It works for the exp bar (code in post above this).
Your code is conflicting with Blizzards and the event API will randomly choose which it'll fire the event first for. This one seems to actually be called instead of set as a script handler, so hooksecurefunc() will work on this one.
lua Code:
  1. hooksecurefunc("ReputationWatchBar_Update",function()
  2.     ReputationWatchStatusBar:SetStatusBarColor(1,1,1,1);
  3. end);
__________________
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)
  Reply With Quote
07-27-11, 04:48 PM   #12
Aprikot
A Frostmaul Preserver
 
Aprikot's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2010
Posts: 284
I appreciate all the replies...

Originally Posted by Haleth View Post
@ OP; In your second attempt, you refer to exhaustionStateID without defining it. Local values are not

inherited from hooked functions.

Try it when you define it as GetRestState(), as the original Blizzard function does.

Thanks, this is good to know. When I "For giggles I used the whole thing in my hooksecurefunc()" the definition was there (but I didn't know it needed to be defined there ):

lua Code:
  1. hooksecurefunc("ExhaustionTick_OnEvent", function(self, event, ...)
  2.         local exhaustionStateID = GetRestState();
  3.         if (exhaustionStateID == 1) then
  4.             MainMenuExpBar:SetStatusBarColor(0, 1, 0);
  5.             ExhaustionLevelFillBar:SetVertexColor(0, 1, 0, .2);
  6.             ExhaustionTickHighlight:SetVertexColor(0, 1, 0);
  7.         elseif (exhaustionStateID == 2) then
  8.             MainMenuExpBar:SetStatusBarColor(0.58, 0.0, 0.55, 1.0);
  9.             ExhaustionLevelFillBar:SetVertexColor(0.58, 0.0, 0.55, 0.15);
  10.             ExhaustionTickHighlight:SetVertexColor(0.58, 0.0, 0.55);
  11.         end
  12.     end)

Originally Posted by SDPhantom View Post
Here's a clip of MainMenuBar.xml...
xml Code:
  1. <Button name="ExhaustionTick" parent="MainMenuExpBar" hidden="false" frameStrata="DIALOG">
  2.     <Size>
  3.         <AbsDimension x="32" y="32"/>
  4.     </Size>
  5.     <Anchors>
  6.         <Anchor point="CENTER" relativeTo="MainMenuExpBar">
  7.             <Offset>
  8.                 <AbsDimension x="0" y="0"/>
  9.             </Offset>
  10.         </Anchor>
  11.     </Anchors>
  12.     <Scripts>
  13.         <OnLoad function="ExhaustionTick_OnLoad"/>
  14.         <OnEvent function="ExhaustionTick_OnEvent"/>
  15.         <OnEnter function="ExhaustionToolTipText"/>
  16.         <OnLeave function="GameTooltip_Hide"/>
  17.     </Scripts>
  18.     <NormalTexture name="ExhaustionTickNormal" file="Interface\MainMenuBar\UI-ExhaustionTickNormal"/>
  19.     <HighlightTexture name="ExhaustionTickHighlight" file="Interface\MainMenuBar\UI-ExhaustionTickHighlight"
  20.  
  21. alphaMode="ADD"/>
  22. </Button>

Notice the way the script functions are registered. This grabs the function immediately and sets it to the associated

script. hooksecurefunc() will not work on this. You'll have to hook the frame's script handler directly with

something like this.
lua Code:
  1. ExhaustionTick:HookScript("OnEvent",function(self,event,...)
  2.     MainMenuExpBar:SetStatusBarColor(1,1,1,1);
  3. end);




Your code is conflicting with Blizzards and the event API will randomly choose which it'll fire the event first for. This

one seems to actually be called instead of set as a script handler, so hooksecurefunc() will work on this one.
lua Code:
  1. hooksecurefunc("ReputationWatchBar_Update",function()
  2.     ReputationWatchStatusBar:SetStatusBarColor(1,1,1,1);
  3. end);

This is good info, and both code examples work perfectly.


Originally Posted by Mischback View Post
Could you "noop" the original function?

Code:
local function noop()
    return
end

    MainMenuExpBar:SetStatusBarColor(1, 1, 1, 1)
    MainMenuExpBar.SetStatusBarColor = noop

This also works perfectly. So is return nothing just putting the kibosh on any subsequent SetStatusBarColor attempts from Blizzard?

Both methods acheive the desired result...

Click image for larger version

Name:	WoWScrnShot_072711_163000.jpg
Views:	407
Size:	1.41 MB
ID:	6393
  Reply With Quote
07-27-11, 05:13 PM   #13
Haleth
This Space For Rent
 
Haleth's Avatar
Featured
Join Date: Sep 2008
Posts: 1,173
If you want to play safe, use the hooking method. If you want the most efficient, use the one where you noop the Blizzard function (= overriding it with an empty function). This may cause taint though.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Exp bar color


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