Thread Tools Display Modes
08-25-16, 01:34 PM   #1
Headhuntress
A Defias Bandit
 
Headhuntress's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2008
Posts: 3
FontString:SetFont() inconsistencies

Hi,

I wrote a small addon that shows how many Death Strikes the (DK, obviously) player can cast. It's basically just a small frame with an icon and a number. The issue I've been having is with the settings of the addon.

I'm supporting LibSharedMedia to change the font of the counter. I'm fetching the font and using SetFont() to set it. This usually goes fine, but sometimes even though the path passed to SetFont() is correct, the font will not be updated. So, up until SetFont() everything seems ok. If I re-enable config mode (which is basically a hide() and show()), the font is properly updated. What would be the proper way to update the font? The font is set in _frame:init() which initializes - but doesn't create - everything. So after every change in the options, I just call init().

What is interesting is that on my main DK with all my addons loaded, this works perfectly fine. The issue appears only on an alt DK with only bugsack, my addon, and sometimes SharedMedia enabled. The fact that it only happens sometimes confuses me quite a bit.

gist to the code
  Reply With Quote
08-26-16, 12:29 AM   #2
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
Your first problem is line 2. Assuming LeecherDB is in your ToC, then it is already a global, yet you define it again. Delete line 2.

Continuing, I didn't spend a lot of time looking at which part of your initialization this would belong, but it is usually in ADDON_LOADED.
Code:
LeecherDB = LeecherDB or {}
  Reply With Quote
08-26-16, 04:43 AM   #3
Kanegasi
A Molten Giant
 
Kanegasi's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2007
Posts: 666
Originally Posted by myrroddin View Post
Your first problem is line 2. Assuming LeecherDB is in your ToC, then it is already a global, yet you define it again. Delete line 2.

Continuing, I didn't spend a lot of time looking at which part of your initialization this would belong, but it is usually in ADDON_LOADED.
Code:
LeecherDB = LeecherDB or {}
ToC globals are not loaded when an addon's code is loaded, so if the globals are anywhere outside ADDON_LOADED, nil errors will occur. Declaring the ToC global at the top of the file is correct.
  Reply With Quote
08-27-16, 12:46 AM   #4
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
To be more specific, writing this in the main chunk defines the default value for a Saved Variable:

Code:
MyAddonDB = {
    foo = "bar",
    x = 0,
    y = 20
}
If it's the first time your addon loads, that value will stand, and will be saved in your addon's SavedVariables file for future sessions.

If it's not the first time your addon loads, then that value will be overwritten by the saved value when your addon's ADDON_LOADED event fires.

In either case, if your saved variable is a table, then declaring a default value for it in the main chunk is a good practice, and means you don't have to worry about checking for its existence anywhere else to avoid "attempt to index a nil value" errors.

Also in either case, this has nothing to do with the OP's problem.

========================================

On the topic of the actual problem, Headhuntress, your code is rather painfully convoluted, but the problem is most likely that you're applying the font in response to PLAYER_LOGIN, but the specific media you're using isn't available yet at that point, because the third-party addon that registers it with LibSharedMedia isn't done loading yet. What do you see if you un-comment line 32?

https://gist.github.com/anonymous/d6...eecher-lua-L32
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
08-27-16, 03:13 AM   #5
Headhuntress
A Defias Bandit
 
Headhuntress's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2008
Posts: 3
Originally Posted by Phanx View Post
On the topic of the actual problem, Headhuntress, your code is rather painfully convoluted, but the problem is most likely that you're applying the font in response to PLAYER_LOGIN, but the specific media you're using isn't available yet at that point, because the third-party addon that registers it with LibSharedMedia isn't done loading yet. What do you see if you un-comment line 32?

https://gist.github.com/anonymous/d6...eecher-lua-L32
To clarify:
On PLAYER_LOGIN everything seems to be going fine. The issue appears if I enable config mode and I start changing fonts every 2-3 seconds. After a point it starts acting weirdly. The dropdown menu does change value, and both the prints in lines 30 and 32 show the correct font/path. However, the font of the string does not change, and only changes if I uncheck and recheck the config mode checkbox (which basically only hides and shows it). That's why I assumed that the issue is not on the library's front, but with SetFont(). I also tried to use updated libraries with no improvement. As I mentioned on my first post, with my full addon set this doesn't happen at all. It only happens when I have the absolute minimum addons enabled. I also tried to use SetText() again right after changing the font, and it would sometimes complain that the font is not set.

I will toy with it later today to attempt to reproduce it reliably. For now it seems a bit unpredictable (sometimes it won't happen, or sometimes it might take longer), so perhaps it's specific fonts breaking it.

And yes, I'm rather new to LUA, and because it's nothing like any languages that I know, I've gone and made a mess So, even the irrelevant tips can prove useful in the long run.
  Reply With Quote
08-27-16, 04:01 AM   #6
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
It sounds like you may have some invalid fonts registered with LibSharedMedia. There's no way to check if a font file or path is valid. Passing an invalid one to :SetFont won't throw an error, but it won't change the font either. The "font not set" error can happen if you pass an invalid font path to :SetFont, and then try to use :SetText (and maybe other methods) on the font string.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
08-27-16, 10:08 AM   #7
kokomala
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 29
Originally Posted by Phanx View Post
It sounds like you may have some invalid fonts registered with LibSharedMedia. There's no way to check if a font file or path is valid. Passing an invalid one to :SetFont won't throw an error, but it won't change the font either. The "font not set" error can happen if you pass an invalid font path to :SetFont, and then try to use :SetText (and maybe other methods) on the font string.
I'm not sure if this is the case, as I've been experiencing a similar issue myself. This normally occurs with external fonts, but can also occur with wow internal fonts.

The fonts are registered correctly, as some of the errors are from SM fonts registered by other addons.

This has a higher chance of occurring when you first open the game and attempt to change a font string. After some game time, this usually fixes itself.
  Reply With Quote
08-28-16, 07:15 AM   #8
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
"font not set" errors can also happen if you create/SetFont/SetText a fontstring too early in the loading process (it seems like SetFont doesn't actually take effect until the fontstring is drawn at least once) but it's not consistent, and that doesn't sound like the problem the OP is having since their error isn't occuring immediately on login.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » FontString:SetFont() inconsistencies


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