WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   General Authoring Discussion (https://www.wowinterface.com/forums/forumdisplay.php?f=20)
-   -   Accessing a frame created in XML from LUA (https://www.wowinterface.com/forums/showthread.php?t=54552)

ruadh 09-27-16 09:23 PM

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")

Phanx 09-27-16 09:25 PM

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()

SDPhantom 09-28-16 09:58 AM

Quote:

Originally Posted by ruadh (Post 319428)
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.

ruadh 09-28-16 06:52 PM

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");

Seerah 09-28-16 07:48 PM

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. :) )

SDPhantom 09-29-16 01:20 AM

Quote:

Originally Posted by Seerah (Post 319454)
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")


ruadh 10-01-16 08:08 AM

Quote:

Originally Posted by Seerah (Post 319454)
--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.

Seerah 10-01-16 12:39 PM

Yes. Children inherit visibility, alpha and scale from their parents.


All times are GMT -6. The time now is 12:00 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI