Thread Tools Display Modes
03-18-18, 09:28 AM   #1
Seethe
A Defias Bandit
Join Date: Mar 2018
Posts: 2
Character Stats not loading on PLAYER_LOGIN

Hello, I am new and noob to WoW addons development.
As an exercise I am trying to do a little panel with player stats in it, but actually I am failing to do so
With the following code, when I enter into the game my stat is 0.000.
Any help? Thanks in advance!

Lua Code:
  1. function round(number, decimals)
  2.         return (("%%.%df"):format(decimals)):format(number)
  3.     end
  4.  
  5.     local crit = round(GetCritChance(), 3)
  6.  
  7.     local c = CreateFrame("Frame", "myframe", UIParent)
  8.  
  9.     c:SetWidth(400)
  10.     c:SetHeight(200)
  11.     c:SetPoint("TOP", UIParent, "CENTER")
  12.     c:SetFrameStrata("MEDIUM")
  13.  
  14.     c.tex = c:CreateTexture()
  15.     c.tex:SetAllPoints(c)
  16.     c.tex:SetColorTexture( unpack(cfg.color))
  17.  
  18.     c.text = c:CreateFontString(nil,"ARTWORK","GameFontNormal")
  19.     c.text:SetPoint("TOPLEFT",8,-10)
  20.     c.text:SetTextColor(classColor.r, classColor.g, classColor.b)
  21.  
  22.     c:RegisterEvent("PLAYER_LOGIN")
  23.  
  24.     c:SetScript("OnEvent", function()
  25.        c.text:SetText( "Crit: " .. crit )
  26.     end)

Last edited by Seethe : 03-18-18 at 09:30 AM.
  Reply With Quote
03-18-18, 10:13 AM   #2
Kanegasi
A Molten Giant
 
Kanegasi's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2007
Posts: 666
Stats may not be available right at login. Plus, they could change with procs and other events during play. Use the event COMBAT_RATING_UPDATE instead. This could get spammy in a group, so you may have to check the unit return.

Lua Code:
  1. c:SetScript("OnEvent", function(self,event,unit)
  2.     if unit=="player" then
  3.         crit = round(GetCritChance(), 3)
  4.         c.text:SetText( "Crit: " .. crit )
  5.     end
  6. end)

I just noticed that you're getting crit only once, which explains the 0 since you're getting crit before you even login. The OnEvent is just setting the same number over and over again if you use the event I suggested. I put another crit= inside the OnEvent for you. You can even skip using a crit variable and just put the round() call in directly.

Lua Code:
  1. c:SetScript("OnEvent", function(self,event,unit)
  2.     if unit=="player" then
  3.         c.text:SetText( "Crit: " .. round(GetCritChance(), 3) )
  4.     end
  5. end)

Last edited by Kanegasi : 03-18-18 at 10:23 AM.
  Reply With Quote
03-18-18, 11:17 AM   #3
Seethe
A Defias Bandit
Join Date: Mar 2018
Posts: 2
Thanks! That's working exactly how I want!
Much appreciated
  Reply With Quote
03-19-18, 12:55 PM   #4
Kakjens
A Cliff Giant
Join Date: Apr 2017
Posts: 75
Where did you get information about COMBAT_RATING_UPDATE' s variables?
  Reply With Quote
03-19-18, 06:44 PM   #5
Tim
A Rage Talon Dragon Guard
 
Tim's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2008
Posts: 308
I really don't think there's any variables for it considering there's no payload table. Look at other events and there's payload tables including variables, so yeah.. Just do what the above code does and watch for the event and then poll the stat(s) you're desiring.
__________________
AddOns: Tim @ WoWInterface
Characters: Mage, Priest, Devoker, Pally
Battle Tag: Mysterio#11164
Current PC Setup: PCPartPicker List
  Reply With Quote
03-19-18, 09:38 PM   #6
Kanegasi
A Molten Giant
 
Kanegasi's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2007
Posts: 666
Originally Posted by Kakjens View Post
Where did you get information about COMBAT_RATING_UPDATE' s variables?
I assumed the event had a unit return looking at the function that updates your character stats.
  Reply With Quote
03-20-18, 07:05 AM   #7
10leej
A Molten Giant
 
10leej's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2011
Posts: 583
Originally Posted by Kanegasi View Post
Stats may not be available right at login. Plus, they could change with procs and other events during play. Use the event COMBAT_RATING_UPDATE instead. This could get spammy in a group, so you may have to check the unit return.

Lua Code:
  1. c:SetScript("OnEvent", function(self,event,unit)
  2.     if unit=="player" then
  3.         crit = round(GetCritChance(), 3)
  4.         c.text:SetText( "Crit: " .. crit )
  5.     end
  6. end)

I just noticed that you're getting crit only once, which explains the 0 since you're getting crit before you even login. The OnEvent is just setting the same number over and over again if you use the event I suggested. I put another crit= inside the OnEvent for you. You can even skip using a crit variable and just put the round() call in directly.

Lua Code:
  1. c:SetScript("OnEvent", function(self,event,unit)
  2.     if unit=="player" then
  3.         c.text:SetText( "Crit: " .. round(GetCritChance(), 3) )
  4.     end
  5. end)
I think OP would want to check PLAYER_ENTERING_WORLD as well if he wants the stats available right away. I'm probably wrong but I believe COMBAT_RATING_UPDATE only fires in combat.
__________________
Tweets YouTube Website
  Reply With Quote
03-20-18, 07:47 AM   #8
Ammako
A Frostmaul Preserver
AddOn Author - Click to view addons
Join Date: Jun 2016
Posts: 256
Originally Posted by 10leej View Post
I think OP would want to check PLAYER_ENTERING_WORLD as well if he wants the stats available right away. I'm probably wrong but I believe COMBAT_RATING_UPDATE only fires in combat.
What about when you take off or equip gear? It has to keep the stats on character sheet updated, even if you're not in combat.
I don't think the COMBAT here refers to being in combat, rather, it refers to combat stats (ratings, percentages.)

Otherwise if it's not that one, there has to be one, anyway.
  Reply With Quote

WoWInterface » AddOns, Compilations, Macros » AddOn Help/Support » Character Stats not loading on PLAYER_LOGIN

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