Thread Tools Display Modes
01-27-17, 10:14 PM   #1
Minuen
A Defias Bandit
Join Date: Jan 2017
Posts: 2
Issues with FontString always being nil

Hey,

I'm new to WoW programming but not programming in general. I've been playing around with lua and generally having success, but I would prefer to design my UI's via xml instead of programmatically generating everything. Unfortunately, I seem to be running into some problems when it comes to creating a FontString.

Here is my xml:
Code:
<Ui xmlns="http://www.blizzard.com/wow/ui/"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="http://www.blizzard.com/wow/ui/
                   ..\..\FrameXML\UI.xsd">
  <Script File="Main.lua"/>
  <Frame name="DamageTracker_Frame" parent="UIParent" inherits="BasicFrameTemplate" hidden="false">
    <Size x="234" y="300"/>
    <Anchors>
      <Anchor point="CENTER" />
    </Anchors>
    <Scripts>
    <OnLoad>
      Main_OnLoad(self);
    </OnLoad>
   </Scripts>
    <Frames>
      <Layers>
        <Layer>
          <FontString name="$parent_Text" inherits="GameFontNormalSmall" text="testing">
            <Anchors>
              <Anchor point="TOPLEFT" x="0" y="13"/>
            </Anchors>
          </FontString>
        </Layer>
      </Layers>
      <Button name="$parent_Button" inherits="UIPanelButtonTemplate" text="Click me!">
          <Size x="234" y="21"/>
          <Anchors>
            <Anchor point="CENTER"/>
          </Anchors>
      </Button>
    </Frames>
  </Frame>
</Ui>
and here is my lua:
Code:
function Main_OnLoad(self)
  DamageTracker_Frame:Show()
  DamageTracker_Frame_Text:SetText("12345431")
end
However, whenever I try and access the FontString it is always nil. If I update it to instead be
Code:
DamageTracker_Frame_Button:SetText("123456789")
everything works perfectly. So, it seems to be related to my FontString, not all widgets in general. I was thinking that I may have invalid xml and it is not creating my FontString because of that, but I stole that code right from the blizzard ui kit.

I can successfully create and render a FontString via:
Code:
  local testFrame = CreateFrame("StatusBar",  nil, UIParent)
  testFrame:SetSize(235, 22)
  testFrame:SetPoint("CENTER", UIParent, 5, -3)
  
  local testFontString = testFrame:CreateFontString(nil, "OVERLAY")
  testFontString:SetPoint("CENTER", HonorBar)
  testFontString:SetFont("Fonts\\ARIALN.TTF", 12, "THINOUTLINE")
  testFontString:SetShadowOffset(1, -1)
  testFontString:SetShadowColor(0,0,0)
  testFontString:SetText("dwsfedwrgefwd")
  
  testFrame:Show()
it just isn't the way I'd prefer to do it.

Any help is appreciated

Thanks,
Minuen
  Reply With Quote
01-27-17, 10:41 PM   #2
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,877
If you had invalid XML you wouldn't see the frame at all.

Calling a global function "Main_OnLoad" is not a great idea, it's possibly a common enough name another addon may have overwritten it.. Try prefixing it with your addons name. Creating frames in XML means any functions called must be global.

You should also prefix your parent frame name ("DamageTracker_Frame") with the name of your addon as these also become global references (overwritten by the last frame created with that name) and it's also easier to see which addon created them with /fstack.

Frames are shown by default unless they have the hidden attribute set to true.

Also on the FontString, y="13" means its "TOPLEFT" will be 13 pixels ABOVE the "TOPLEFT" of the parent frame
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 01-28-17 at 12:34 AM.
  Reply With Quote
01-27-17, 11:07 PM   #3
Gethe
RealUI Developer
 
Gethe's Avatar
Premium Member
Featured
Join Date: Sep 2008
Posts: 942
You can see if there are any xml errors in World of Warcraft\Logs\FrameXML.log
__________________
Knowledge = Power; Be OP


Last edited by Gethe : 01-27-17 at 11:09 PM.
  Reply With Quote
01-28-17, 12:31 AM   #4
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,323
Usage of the <Script> tag is depreciated since you can add Lua files to the ToC. There are very niche cases that still make use of it, but for general addon development, you should avoid it. Just make sure the Lua files are listed before the XML ones that refer to functions defined in them.



Comments on the XML code.
  1. The <Layers> tag is in the wrong place. It's a sibling to <Frames>, not a child of it.
  2. hidden="false" is redundant. As mentioned before, frames are already visible when created.
  3. You don't have a layer level defined for the FontString to be drawn at, see WoWPedia:Layer for more information.
  4. Using dynamic functions to call global functions in your Lua file is outdated. Script handler tags now have function and method attributes that directly link to those functions located in the global environment or the frame's own table respectively.



ToC Code:
  1. ## Interface: 70100
  2.  
  3. DamageTracker.lua
  4. DamageTracker.xml

XML Code:
  1. <Ui xmlns="http://www.blizzard.com/wow/ui/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.blizzard.com/wow/ui/
  2. ..\..\FrameXML\UI.xsd">
  3.     <Frame name="DamageTracker_Frame" parent="UIParent" inherits="BasicFrameTemplate">
  4.         <Size x="234" y="300"/>
  5.         <Anchors>
  6.             <Anchor point="CENTER" />
  7.         </Anchors>
  8.         <Layers>
  9.             <Layer level="OVERLAY">
  10.                 <FontString name="$parent_Text" inherits="GameFontNormalSmall" text="testing">
  11.                     <Anchors>
  12.                         <Anchor point="TOPLEFT" x="0" y="13"/>
  13.                     </Anchors>
  14.                 </FontString>
  15.             </Layer>
  16.         </Layers>
  17.         <Frames>
  18.             <Button name="$parent_Button" inherits="UIPanelButtonTemplate" text="Click me!">
  19.                 <Size x="234" y="21"/>
  20.                 <Anchors>
  21.                     <Anchor point="CENTER"/>
  22.                 </Anchors>
  23.             </Button>
  24.         </Frames>
  25.         <Scripts>
  26.             <OnLoad function="DamageTracker_OnLoad"/>
  27.         </Scripts>
  28.     </Frame>
  29. </Ui>

Lua Code:
  1. function DamageTracker_OnLoad(self)
  2.     DamageTracker_Frame_Text:SetText("12345431")
  3. end



PS: The Default UI was mostly written to show what you can do with the API. In doing so, it's terribly inefficient and many sections of it are using outdated programming methods that they have since improved. Also, the Blizzard Interface Toolkit is outdated software that I'm surprised you got working with the CASC storage since it was made for the old MPQs. See WoWPedia:Console for how to enable the game console and WoWPedia:Viewing Blizzard's Interface Code for the command to run.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 01-28-17 at 06:40 AM.
  Reply With Quote
01-28-17, 02:38 AM   #5
Gethe
RealUI Developer
 
Gethe's Avatar
Premium Member
Featured
Join Date: Sep 2008
Posts: 942
BasicFrameTemplate is actually a built-in template. It is defined in FrameXML/UIPanelTemplates.xml
__________________
Knowledge = Power; Be OP

  Reply With Quote
01-28-17, 06:45 AM   #6
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,323
So it is. Updated my last post to reflect this.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
01-28-17, 09:07 AM   #7
Minuen
A Defias Bandit
Join Date: Jan 2017
Posts: 2
! Thank you all so much for the feedback. I have been having a terrible time finding up to date resources with examples (most seem to be from 2012 or earlier).

You mentioned that the <Script> tag is now deprecated. How do you trigger onLoad without using it?
  Reply With Quote
01-28-17, 09:39 AM   #8
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,877
See the code SDPhantom posted for the current usage. The old way still works.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 01-28-17 at 09:41 AM.
  Reply With Quote
01-28-17, 09:52 AM   #9
Lombra
A Molten Giant
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 554
Originally Posted by Minuen View Post
! Thank you all so much for the feedback. I have been having a terrible time finding up to date resources with examples (most seem to be from 2012 or earlier).

You mentioned that the <Script> tag is now deprecated. How do you trigger onLoad without using it?
If you create a frame using a template that has an OnLoad handler, that will run regardless of whether you create the frame in Lua or XML. The script tag is irrelevant.
__________________
Grab your sword and fight the Horde!
  Reply With Quote
01-28-17, 01:16 PM   #10
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
I believe SDPhantom was mainly referring to using it with the file parameter.
Code:
<Script File="filename.lua">
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
01-28-17, 01:58 PM   #11
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,877
Originally Posted by Seerah View Post
I believe SDPhantom was mainly referring to using it with the file parameter.
Code:
<Script File="filename.lua">
Confusing <Script> with <Scripts> as did Minuen, my bad. Athough the <Script> tag is also still usable.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 01-28-17 at 02:05 PM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Issues with FontString always being nil


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