Thread Tools Display Modes
04-01-24, 12:08 AM   #1
An Aku'mai Servant
AddOn Author - Click to view addons
Join Date: Mar 2021
Posts: 30
XML to Lua?

So... I'm not very good with xml (old brain doesn't like it)
but I see a lot of UI examples in XML and I'm wondering if there is an easy way to convert them to lua code.

As an example I found this page

Gello posted some XML code and I'm trying to convert it into lua code.

    <Frame name="SimpleFrame" parent="UIParent" inherits="DefaultPanelTemplate">
        <Size x="150" y="150"/>
            <Anchor point="CENTER"/>
            <Button parentKey="CloseButton" inherits="UIPanelCloseButtonDefaultAnchors"/>
			<Frame parentKey="Inset" useParentLevel="true" inherits="InsetFrameTemplate">
					<Anchor point="TOPLEFT" x="10" y="-26" />
					<Anchor point="BOTTOMRIGHT" x="-6" y="26" />
            <Button parentKey="OkButton" inherits="UIPanelButtonTemplate" text="Okay">
                <Size x="90" y="22"/>
                    <Anchor point="BOTTOMRIGHT" x="-6" y="4"/>
                self.TitleContainer.TitleText:SetText("Simple Frame")
The frame part is simple enough but the button xml code has me baffled?
What would the lua code look like for the two buttons?
  Reply With Quote
04-01-24, 01:15 AM   #2
An Aku'mai Servant
AddOn Author - Click to view addons
Join Date: Mar 2021
Posts: 30
Lua code for xml (found)

I went to ChatGPT and asked them to convert the xml code to lua and the code below is the result.

Lua Code:
  1. local frame = CreateFrame("Frame", "SimpleFrame", UIParent, "BasicFrameTemplateWithInset")
  2. frame:SetSize(150, 150)
  3. frame:SetPoint("CENTER")
  5. local closeButton = CreateFrame("Button", nil, frame, "UIPanelCloseButton")
  6. closeButton:SetPoint("TOPRIGHT", -4, -4)
  8. local insetFrame = CreateFrame("Frame", nil, frame, "InsetFrameTemplate")
  9. insetFrame:SetPoint("TOPLEFT", 10, -26)
  10. insetFrame:SetPoint("BOTTOMRIGHT", -6, 26)
  12. local okButton = CreateFrame("Button", nil, frame, "UIPanelButtonTemplate")
  13. okButton:SetText("Okay")
  14. okButton:SetSize(90, 22)
  15. okButton:SetPoint("BOTTOMRIGHT", -6, 4)
  16. okButton:SetScript("OnClick", function()
  17.     frame:Hide()
  18. end)
  20. frame:SetScript("OnLoad", function()
  21.     frame.TitleText:SetText("Simple Frame")
  22. end)

Last edited by Codger : 04-01-24 at 07:53 AM.
  Reply With Quote
04-01-24, 11:04 AM   #3
I did that?
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,929
In XML, where you see parentKey, that is convert to a key on the parent frame (funny that)
<Button parentKey="CloseButton" inherits="UIPanelCloseButtonDefaultAnchors"/>
Would become:
Lua Code:
  1. frame.CloseButton = CreateFrame("Button", nil, frame, "UIPanelCloseButton")
  2. frame.CloseButton:SetPoint("TOPRIGHT", -4, -4)

Because the parent frame has a (not very unique) name you can access the child frame (CloseButton) via the parent's key

Lua Code:
  1. SimpleFrame.CloseButton:Click()
would be the same as the mouse clicking the close button
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.
  Reply With Quote
04-01-24, 06:15 PM   #4
A Pyroguard Emberseer
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,336
Originally Posted by Codger View Post
I went to ChatGPT and asked them to convert the xml code to lua and the code below is the result.


Lua Code:
  1. frame:SetScript("OnLoad", function()
  2.     frame.TitleText:SetText("Simple Frame")
  3. end)
The problem with this is OnLoad runs when CreateFrame() is called. Registering a script to it through Lua does nothing as it's never called again.

It's still a good idea to proofread any code GhatGPT spits out as it still makes mistakes. It sees parentKey and just shoves it in a local instead of assigning the object to that key in the parent's table, as Fizzlemizz pointed out. It also doesn't handle useParentLevel, which is to set the child's framelevel equal to its parent where by default, it's +1.
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 : 04-01-24 at 06:26 PM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » XML to Lua?

Thread Tools
Display Modes

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