Thread Tools Display Modes
Prev Previous Post   Next Post Next
09-17-17, 05:46 PM   #1
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
Dynamically set the colour of a statusbar

I am writing a plugin for ElvUI, and yes, I could (and often do) ask Lua questions on their forums. However, this might be a universal question that is not unique to ElvUI.

In ElvUI there is a function that sets the text for the XP bar and I have securely hooked that function to alter the text and also colour the statusbar based on the average value of currentXP/maximumXP.

This so far is working splendidly. The text I set is working, and the new colour is working -- sort of. It does set the new colour, but I can't be 100% certain that the colour is dynamically updating.

Here is my code, which is called when Elv's function is hooked.
Lua Code:
  1. local function UpdateExperience(self, event)
  2.     local bar = self.expBar
  3.     local isMaxLevel = UnitLevel("player") == MAX_PLAYER_LEVEL_TABLE[GetExpansionLevel()]
  4.     local text = ""
  5.     local current, maximum = EDB:GetXP("player")
  6.     local avg = current / maximum
  7.     avg = PCB:Round(avg, 2)
  8.  
  9.     if isMaxLevel then
  10.         text = L["Capped"]
  11.         bar.text:SetText(text)
  12.         bar.statusBar:SetStatusBarColor(0, 0.4, 1, 0.8)
  13.         bar.rested:Hide()
  14.     else
  15.         bar.statusBar:SetStatusBarColor(0, 0.1, avg, 1)
  16.     end
  17. end

Line 15 in this chunk is the one that concerns me. UpdateExperience() is called each time Elv's similar function is called, so you would think the value of avg would be updated, and thus the SetStatusBarColor() would also change. But I'm not confident, hence I am here.

ElvUI aside, this is otherwise a basic Lua question I think. Anyone writing status bar code might be interested, so here I am.

Do I need to hide the status bar, colour it, then show it again? Also, when the character is at maximum level the status bar colour is not a nice blue and instead is black or empty. Why would that occur?
  Reply With Quote
 

WoWInterface » Developer Discussions » Lua/XML Help » Dynamically set the colour of a statusbar


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