Thread Tools Display Modes
09-27-16, 09:23 PM   #1
ruadh
A Murloc Raider
Join Date: Aug 2007
Posts: 5
Accessing a frame created in XML from LUA

I've spent a week searching forums, and everything I've found leads me to believe that when a frame is created in XML, the reference is available in the global namespace with the same name as the frame. In other words, this should print "LUA Loaded", "ADDON_LOADED" and "XMLbeforeLUAForm" when the addon loads. The frame comes up visible, "LUA Loaded" prints, but nothing else. Can someone tell me where I'm going wrong? Thanks.

Code:
## Interface: 70000
## Title: XMLbeforeLUA
## Notes: How to address a frame created in XML from LUA run after the XML
## DefaultState: Enabled
## LoadOnDemand: 0
XMLbeforeLUAForm.xml
XMLbeforeLUA.lua
Code:
<Ui xmlns="http://www.blizzard.com/wow/ui" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Frame name="XMLbeforeLUAForm">
    <Size>
      <AbsDimension x="400" y="400" />
    </Size>
    <Anchors>
      <Anchor point="CENTER" relativeTo="UIParent">
      </Anchor>
    </Anchors>
    <Backdrop bgFile="Interface\DialogFrame\UI-DialogBox-Background" edgeFile="Interface\DialogFrame\UI-DialogBox-Border" tile="true">
      <BackgroundInsets>
        <AbsInset left="11" right="12" top="12" bottom="11" />
      </BackgroundInsets>
      <TileSize>
        <AbsValue val="32" />
      </TileSize>
      <EdgeSize>
        <AbsValue val="32" />
      </EdgeSize>
    </Backdrop>
  </Frame>
</Ui>
Lua Code:
  1. print("LUA loaded");
  2. local myFrame = XMLbeforeLUAForm;
  3. myFrame:SetScript("OnEvent", function(event) print(event); print(self.name); end)
  4. myFrame:RegisterEvent("ADDON_LOADED")

Last edited by ruadh : 09-27-16 at 09:26 PM.
  Reply With Quote
09-27-16, 09:25 PM   #2
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Change "self.name" to "self:GetName()".

If you want your frame to have a "name" key containing its name, you need to add that manually:
local myFrame = XMLbeforeLUAForm
myFrame.name = myFrame:GetName()
__________________
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
09-28-16, 09:58 AM   #3
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
Originally Posted by ruadh View Post
Code:
myFrame:SetScript("OnEvent", function(event) print(event); print(self.name); end)
This line should be printing something like table:1A2B3C4D and throwing an "attempt to index nil" error if it's being run. The function definition is missing self as the first parameter. event is the second followed by a list of arguments specific to the event fired.
__________________
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 : 09-28-16 at 10:02 AM.
  Reply With Quote
09-28-16, 06:52 PM   #4
ruadh
A Murloc Raider
Join Date: Aug 2007
Posts: 5
Thank you both very much. It looks like my entire problem was assuming that a frame object automatically has a name attribute; I'd also left out the SetScript(). This code works as I expected:
Code:
## Interface: 70000
## Title: XMLbeforeLUA
## Version: 70000
## Notes: How to address a frame created in XML from LUA run after the XML
## DefaultState: Enabled
## LoadOnDemand: 0
XMLbeforeLUAForm.xml
XMLbeforeLUA.lua
Code:
<Ui xmlns="http://www.blizzard.com/wow/ui" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <!--Autogenerated by wowuides, Version=1.0.300.0, Culture=neutral, PublicKeyToken=null-->
  <Frame name="XMLbeforeLUAForm">
    <!--<FrameSkin skinid="dcb143e1-a4ab-4e7c-b934-1efa40101d21" frameid="2d508883-59c2-4f83-ae10-27aaad48391b" />-->
    <Size>
      <AbsDimension x="400" y="400" />
    </Size>
    <Anchors>
      <Anchor point="CENTER" relativeTo="UIParent">
        <Offset>
          <AbsDimension x="0" y="0" />
        </Offset>
      </Anchor>
    </Anchors>
    <Backdrop bgFile="Interface\DialogFrame\UI-DialogBox-Background" edgeFile="Interface\DialogFrame\UI-DialogBox-Border" tile="true">
      <BackgroundInsets>
        <AbsInset left="11" right="12" top="12" bottom="11" />
      </BackgroundInsets>
      <TileSize>
        <AbsValue val="32" />
      </TileSize>
      <EdgeSize>
        <AbsValue val="32" />
      </EdgeSize>
    </Backdrop>
    <Layers>
      <Layer>
        <Texture name="$parentTitleBorder" hidden="true" file="Interface\DialogFrame\UI-DialogBox-Header">
          <Size>
            <AbsDimension x="160" y="32" />
          </Size>
          <Anchors>
            <Anchor point="TOP">
              <Offset>
                <AbsDimension x="0" y="5" />
              </Offset>
            </Anchor>
          </Anchors>
          <TexCoords left="0.2" right="0.8" top="0" bottom="0.6" />
        </Texture>
        <FontString name="$parentTitleString" hidden="true" font="Fonts\FRIZQT__.TTF">
          <Size>
            <AbsDimension x="140" y="0" />
          </Size>
          <Anchors>
            <Anchor point="TOP">
              <Offset>
                <AbsDimension x="0" y="-4" />
              </Offset>
            </Anchor>
          </Anchors>
          <FontHeight>
            <AbsValue val="12" />
          </FontHeight>
          <Color r="1" g="0.8196079" b="0" />
          <Shadow>
            <Color r="0" g="0" b="0" />
            <Offset>
              <AbsDimension x="1" y="-1" />
            </Offset>
          </Shadow>
        </FontString>
      </Layer>
    </Layers>
  </Frame>
</Ui>
Lua Code:
  1. local myFrame = XMLbeforeLUAForm;
  2. print(myFrame:GetName())
  3. myFrame:RegisterEvent("ADDON_LOADED")
  4. myFrame:SetScript("OnEvent", function(self, event, name) if name == "XMLbeforeLUA" then print(event); print(self:GetName()); self:Hide(); end end)
  5. print("LUA loaded");
  Reply With Quote
09-28-16, 07:48 PM   #5
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
And here is your XML file as Lua code instead. (with tables expanded vertically for ease of reading)

Lua Code:
  1. local luaFrame = CreateFrame("Frame", "UnneccessaryGlobalFrameName", "UIParent")
  2. luaFrame:SetWidth(400)
  3. luaFrame:SetHeight(400)
  4. luaFrame:SetPoint("CENTER")
  5. luaFrame:SetBackdrop({
  6.      bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background",
  7.      edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Border",
  8.      tile = true, tileSize = 32,
  9.      edgeSize = 32,
  10.      insets = {
  11.           left = 11,
  12.           right = 12,
  13.           top = 12,
  14.           bottom = 11
  15.      }
  16. })
  17.  
  18. local titleBorder = luaFrame:CreateTexture("UnneccessaryGlobalFrameNameTitleBorder")
  19. titleBorder:SetWidth(160)
  20. titleBorder:SetHeight(32)
  21. titleBorder:SetPoint("TOP", luaFrame, "TOP", 0, 5)
  22. titleBorder:SetTexture("Interface\\DialogFrame\\UI-DialogBox-Header")
  23. titleBorder:SetTexCoord(.2, .8, 0, .6)
  24. titleBorder:Hide()  --not sure why you're hiding it, but okay...
  25.  
  26. local titleString = luaFrame:CreateFontString("UnneccessaryGlobalFrameNameTitleString")
  27. titleString:SetFont("Fonts\\FRIZQT__.TTF", 12)
  28. titleString:SetWidth(140)
  29. titleString:SetPoint("TOP", luaFrame, "TOP", 0, -4)
  30. titleString:SetTextColor(1, 0.8196079, 0)
  31. titleString:SetShadowOffset(1, -1)
  32. titleString:SetShadowColor(0, 0, 0)
  33. titleString:Hide()  --again, not sure why you're hiding it...
  34. --note: you never gave your fontstring text...
  35. --titleString:SetText("This is my frame's title.")

And you can add this right below that.
Lua Code:
  1. luaFrame:RegisterEvent("ADDON_LOADED")
  2. luaFrame:SetScript("OnEvent", function(self, event, name)
  3.      if name == "XMLbeforeLUA" then
  4.           print(event)
  5.           print(self:GetName())
  6.           self:Hide() --don't hide your frame when seeing if things load - how do you know it works? This might be how your fontstring ended up with no text. ;)
  7.      end
  8. end)
  9. print("Lua loaded")

(BTW, it's Lua, not LUA. It's Portuguese for "moon", not an acronym.)
(Oh, and also semi-colons are totally unnecessary in 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
09-29-16, 01:20 AM   #6
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
Originally Posted by Seerah View Post
Code:
luaFrame:CreateTexture("UnneccessaryGlobalFrameNameTitleBorder")
luaFrame:CreateFontString("UnneccessaryGlobalFrameNameTitleString")
You can use the $parent tag to prefix a name with the parent's name in Lua as well.
Also, you should at the very least define what DrawLayer your regions reside in.
Code:
luaFrame:CreateTexture("$parentTitleBorder","BORDER")
luaFrame:CreateFontString("$parentTitleString","OVERLAY")
__________________
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
10-01-16, 08:08 AM   #7
ruadh
A Murloc Raider
Join Date: Aug 2007
Posts: 5
Originally Posted by Seerah View Post
--not sure why you're hiding it, but okay...
The example was focused on getting the Lua to work, but you raise another question. Shouldn't the visibility of the frame's children follow the parent? In other words, wouldn't hiding the frame also hide the border and the title string?

Thanks for all your comments.
  Reply With Quote
10-01-16, 12:39 PM   #8
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Yes. Children inherit visibility, alpha and scale from their parents.
__________________
"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

WoWInterface » Developer Discussions » General Authoring Discussion » Accessing a frame created in XML from LUA


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