Attempt to fix: font not set error when cast bars are enabled#871
Attempt to fix: font not set error when cast bars are enabled#871monolitas wants to merge 1 commit intoElvUI-WotLK:masterfrom
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a "Font not set" error that occurs when cast bars are enabled in the nameplate module during initialization. The fix adds safeguards to ensure fonts are properly set before attempting to display text on cast bar elements.
- Adds conditional logic to only manipulate test cast bars when any cast bar is enabled in settings
- Implements font validation and fallback assignment before setting text
- Adds nil checks for cast bar elements before accessing their properties
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| local anyCastbarEnabled = unitsDB and ( | ||
| (unitsDB.ENEMY_PLAYER and unitsDB.ENEMY_PLAYER.castbar and unitsDB.ENEMY_PLAYER.castbar.enable) or | ||
| (unitsDB.ENEMY_NPC and unitsDB.ENEMY_NPC.castbar and unitsDB.ENEMY_NPC.castbar.enable) or | ||
| (unitsDB.FRIENDLY_PLAYER and unitsDB.FRIENDLY_PLAYER.castbar and unitsDB.FRIENDLY_PLAYER.castbar.enable) or | ||
| (unitsDB.FRIENDLY_NPC and unitsDB.FRIENDLY_NPC.castbar and unitsDB.FRIENDLY_NPC.castbar.enable) | ||
| ) or false |
There was a problem hiding this comment.
The repeated pattern for checking castbar.enable across different unit types creates duplicated logic. Consider extracting this into a helper function or using a loop over unit types to reduce code duplication.
| local anyCastbarEnabled = unitsDB and ( | |
| (unitsDB.ENEMY_PLAYER and unitsDB.ENEMY_PLAYER.castbar and unitsDB.ENEMY_PLAYER.castbar.enable) or | |
| (unitsDB.ENEMY_NPC and unitsDB.ENEMY_NPC.castbar and unitsDB.ENEMY_NPC.castbar.enable) or | |
| (unitsDB.FRIENDLY_PLAYER and unitsDB.FRIENDLY_PLAYER.castbar and unitsDB.FRIENDLY_PLAYER.castbar.enable) or | |
| (unitsDB.FRIENDLY_NPC and unitsDB.FRIENDLY_NPC.castbar and unitsDB.FRIENDLY_NPC.castbar.enable) | |
| ) or false | |
| local unitTypes = { "ENEMY_PLAYER", "ENEMY_NPC", "FRIENDLY_PLAYER", "FRIENDLY_NPC" } | |
| local anyCastbarEnabled = false | |
| if unitsDB then | |
| for _, unitType in ipairs(unitTypes) do | |
| local unit = unitsDB[unitType] | |
| if unit and unit.castbar and unit.castbar.enable then | |
| anyCastbarEnabled = true | |
| break | |
| end | |
| end | |
| end |
| if castbar.Name and not select(1, castbar.Name:GetFont()) then | ||
| castbar.Name:SetFontObject(GameFontNormal) | ||
| end | ||
| if castbar.Time and not select(1, castbar.Time:GetFont()) then | ||
| castbar.Time:SetFontObject(GameFontNormal) | ||
| end |
There was a problem hiding this comment.
The font checking and setting logic is duplicated for both Name and Time elements. This could be extracted into a helper function that takes the text element as a parameter to reduce code duplication.
| if castbar.Name and not select(1, castbar.Name:GetFont()) then | |
| castbar.Name:SetFontObject(GameFontNormal) | |
| end | |
| if castbar.Time and not select(1, castbar.Time:GetFont()) then | |
| castbar.Time:SetFontObject(GameFontNormal) | |
| end | |
| local function EnsureFontSet(textElement) | |
| if textElement and not select(1, textElement:GetFont()) then | |
| textElement:SetFontObject(GameFontNormal) | |
| end | |
| end | |
| EnsureFontSet(castbar.Name) | |
| EnsureFontSet(castbar.Time) |
**bug fix for: #482
When cast bars are enabled, the UI throws a "SetText" error