Thread Tools Display Modes
04-07-22, 09:44 AM   #1
PARRYHOTTEr
A Defias Bandit
Join Date: Apr 2022
Posts: 3
Need to refresh the UI before non-nil value is returned

Hello everyone!

I'm very new to coding addOns to wow, and i've encountered a problem that annoys me.

Whenever i launch the game, GetAverageItemlevel() returns a nil value. If i then reload the UI it will then start to return the expected values.

What i wish to figure out is a way to stop this behaviour of where i have to reload the UI once before the addOn displays the expected value.

The following code is giving me this problem:

Code:
local ilvlText = frame:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
ilvlText:SetPoint("TOPLEFT", 10, -60)
local avgItemLevel, avgItemLevelEquipped, avgItemLevelPVP = GetAverageItemLevel()
ilvlText:SetText("Average equipped ilvl is: " .. tostring(avgItemLevelEquipped or "Refresh UI"))
  Reply With Quote
04-07-22, 10:10 AM   #2
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
Originally Posted by PARRYHOTTEr View Post
Hello everyone!

I'm very new to coding addOns to wow, and i've encountered a problem that annoys me.

Whenever i launch the game, GetAverageItemlevel() returns a nil value. If i then reload the UI it will then start to return the expected values.

What i wish to figure out is a way to stop this behaviour of where i have to reload the UI once before the addOn displays the expected value.

The following code is giving me this problem:

Code:
local ilvlText = frame:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
ilvlText:SetPoint("TOPLEFT", 10, -60)
local avgItemLevel, avgItemLevelEquipped, avgItemLevelPVP = GetAverageItemLevel()
ilvlText:SetText("Average equipped ilvl is: " .. tostring(avgItemLevelEquipped or "Refresh UI"))
You need to wait until the game knows something about the characther you just logged in to, try (your code didn't mention where the frame is anchored so I made that bit up):
Lua Code:
  1. frame:SetSize(5, 5)
  2. frame:SetPoint("LEFT", 10, 0)
  3. frame.ilvlText = frame:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
  4. frame.ilvlText:SetPoint("TOPLEFT", 10, -60)
  5. frame:RegisterEvent("PLAYER_LOGIN")
  6. frame:RegisterUnitEvent("UNIT_INVENTORY_CHANGED", "player")
  7. frame:SetScript("OnEvent", function(self)
  8.     local avgItemLevel, avgItemLevelEquipped, avgItemLevelPVP = GetAverageItemLevel()
  9.     self.ilvlText:SetText("Average equipped ilvl is: " .. tostring(avgItemLevelEquipped or "Refresh UI"))
  10. end)
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.
  Reply With Quote
04-07-22, 10:35 AM   #3
PARRYHOTTEr
A Defias Bandit
Join Date: Apr 2022
Posts: 3
Originally Posted by Fizzlemizz View Post
You need to wait until the game knows something about the characther you just logged in to, try (your code didn't mention where the frame is anchored so I made that bit up):
Lua Code:
  1. frame:SetSize(5, 5)
  2. frame:SetPoint("LEFT", 10, 0)
  3. frame.ilvlText = frame:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
  4. frame.ilvlText:SetPoint("TOPLEFT", 10, -60)
  5. frame:RegisterEvent("PLAYER_LOGIN")
  6. frame:RegisterUnitEvent("UNIT_INVENTORY_CHANGED", "player")
  7. frame:SetScript("OnEvent", function(self)
  8.     local avgItemLevel, avgItemLevelEquipped, avgItemLevelPVP = GetAverageItemLevel()
  9.     self.ilvlText:SetText("Average equipped ilvl is: " .. tostring(avgItemLevelEquipped or "Refresh UI"))
  10. end)
First of all thanks for the provided code, it works now!

In hindsight, i probably should've explained that i'm creating a simple character panel as a first addOn project and the 'frame' is a simple CreateFrame("Frame", nil, UIParent, "BackdropTemplateMixin" and "BackdropTemplate").

You mention that the game has to 'know' something about the character before it can read values about the character, etc; could you perhaps elaborate a little bit on that? If you have an article or something explaining it, that is also fine
  Reply With Quote
04-07-22, 12:35 PM   #4
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
Most addons rely on updating when things happen in-game. They are notified of these "happenings" through an event system.

If you just put code in a .lua file withouot setting up a method to listen for the events you are interested in, then the code runs just once when the .lua file is loaded by the addon system (while the bar scrolls across the bottom of you screen) and never again.

The extra code I added, registers to listen for the PLAYER_LOGIN event that fires once after all initial addons have loaded and the "UNIT_INVENTORY_CHANGED" event for the player unit (you) see for information on this event

It also sets up the OnEvent (listener) script do do what needs to be done when those events are sent to the addon. Because it runs the same code for all events registerd (and ony the regiistered events will be sent), the script doesn't have to be specific about what to do for each event.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.
  Reply With Quote
04-07-22, 01:13 PM   #5
PARRYHOTTEr
A Defias Bandit
Join Date: Apr 2022
Posts: 3
Originally Posted by Fizzlemizz View Post
Most addons rely on updating when things happen in-game. They are notified of these "happenings" through an event system.

If you just put code in a .lua file withouot setting up a method to listen for the events you are interested in, then the code runs just once when the .lua file is loaded by the addon system (while the bar scrolls across the bottom of you screen) and never again.

The extra code I added, registers to listen for the PLAYER_LOGIN event that fires once after all initial addons have loaded and the "UNIT_INVENTORY_CHANGED" event for the player unit (you) see for information on this event

It also sets up the OnEvent (listener) script do do what needs to be done when those events are sent to the addon. Because it runs the same code for all events registerd (and ony the regiistered events will be sent), the script doesn't have to be specific about what to do for each event.
Ah i see, thanks a lot for the help and the explanation btw is this the appropriate section to ask these sorts of quesitons or is there a better section for scripting help and that stuff?
  Reply With Quote
04-07-22, 01:31 PM   #6
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
The Lua/XML support forum is probably best
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.
  Reply With Quote

WoWInterface » AddOns, Compilations, Macros » AddOn Help/Support » Need to refresh the UI before non-nil value is returned

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