Thread Tools Display Modes
01-29-15, 11:13 AM   #1
odjur84
A Fallenroot Satyr
 
odjur84's Avatar
Join Date: Jan 2015
Posts: 24
XML problem: Twice function call

Hi!

I'm new to addon programming and currently coding a LootCouncil-Addon for my guild. But as I said, I'm new to Lua and XML (not to programming in general) and since I've desperately tried to figure out what is wrong with my code, I've decided to ask people, who are more familiar with that. As english isn't my mother tongue, please forgive me my probably bad spelling.

I have two addons, that consist of the same .lua-file with the exact identical code:

Code:
local frame = CreateFrame("FRAME", "FooAddonFrame");
frame:RegisterEvent("PLAYER_ENTERING_WORLD");
frame:RegisterEvent("LOOT_OPENED");

local function eventHandler(self, event, ...)
 if (event == "PLAYER_ENTERING_WORLD") then
	print("Hello!");
 end
 if (event == "LOOT_OPENED") then
	local texture, lootname, quantity, quality, locked = GetLootSlotInfo(1);
	print("You've found " .. lootname .. "!");
 end
end

frame:SetScript("OnEvent", eventHandler);

function Button1_OnClick()
	Frame1:Hide();
end
Then I have two XML-files, that are fairly different, because one of them is only a "test-XML-file" and the other my WIP-addon-file. What I want to do: Whenever a player opens a loot table (Event LOOT_OPENED is fired) the first item of the loot table should be printed in the chat window. This works just perfectly with my test-addon, consisting of the mentioned .lua-file and the following (basic) XML-file:

Code:
<Ui xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.blizzard.com/wow/ui/">
	<Script file="Frame.lua" />
	<Frame name="Frame1" parent="UIParent" toplevel="true" movable="true" enableMouse="true">
		<Size>
			<AbsDimension x="184" y="96" />
		</Size>
		<Anchors>
			<Anchor point="CENTER">
				<Offset x="-8" y="52" />
			</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>
		<Frames>
			<Button name="Button1" inherits="UIPanelButtonTemplate" text="Button1">
				<Size>
					<AbsDimension x="75" y="23" />
				</Size>
				<Anchors>
					<Anchor point="TOPLEFT">
						<Offset x="50" y="-57" />
					</Anchor>
				</Anchors>
				<Scripts>
					<OnClick>Button1_OnClick();</OnClick>
				</Scripts>
			</Button>
		</Frames>
		<Layers>
			<Layer level="OVERLAY">
				<FontString name="FontString1" inherits="GameFontNormal" text="Hello, World!">
					<Size>
						<AbsDimension x="80" y="20" />
					</Size>
					<Anchors>
						<Anchor point="TOPLEFT">
							<Offset x="50" y="-12" />
						</Anchor>
					</Anchors>
				</FontString>
			</Layer>
		</Layers>
	</Frame>
</Ui>
But it doesn't work with the other (bigger and more complicated) XML-file. And I've really desperately tried everything to solve the problem, but I don't get it. Everytime I use the WIP-addon-file, he prints the item twice in the chat! Here's it:

Code:
<Ui xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.blizzard.com/wow/ui/">
	<Script file="Frame.lua" />
	<Frame name="ORC_FrameUser" hidden="true" parent="UIParent" toplevel="true" movable="true" enableMouse="true">
		<Size>
			<AbsDimension x="514" y="514" />
		</Size>
		<Anchors>
			<Anchor point="CENTER">
				<Offset x="-6" y="-32" />
			</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>
		<Frames>
			<Button name="ORC_UserSettings" inherits="UIPanelButtonTemplate" id="2" text="Settings">
				<Size>
					<AbsDimension x="75" y="23" />
				</Size>
				<Anchors>
					<Anchor point="TOPLEFT">
						<Offset x="416" y="-17" />
					</Anchor>
				</Anchors>
				<Scripts>
					<OnClick>ORC_UserSettings_OnClick();</OnClick>
				</Scripts>
			</Button>
			<Button name="ORC_UserNeed1" inherits="UIPanelButtonTemplate" id="5" text="Need">
				<Size>
					<AbsDimension x="64" y="23" />
				</Size>
				<Anchors>
					<Anchor point="TOPLEFT">
						<Offset x="39" y="-330" />
					</Anchor>
				</Anchors>
			</Button>
			<Button name="ORC_UserUpdate1" inherits="UIPanelButtonTemplate" id="6" text="Kl. Update">
				<Size>
					<AbsDimension x="64" y="23" />
				</Size>
				<Anchors>
					<Anchor point="TOPLEFT">
						<Offset x="38" y="-359" />
					</Anchor>
				</Anchors>
				<Scripts>
					<OnClick>ORC_UserUpdate_OnClick();</OnClick>
				</Scripts>
			</Button>
			<Button name="ORC_Button2nd1" inherits="UIPanelButtonTemplate" id="7" text="2nd">
				<Size>
					<AbsDimension x="64" y="23" />
				</Size>
				<Anchors>
					<Anchor point="TOPLEFT">
						<Offset x="38" y="-389" />
					</Anchor>
				</Anchors>
			</Button>
			<Button name="ORC_UserTransmogg1" inherits="UIPanelButtonTemplate" id="8" text="Transm.">
				<Size>
					<AbsDimension x="64" y="23" />
				</Size>
				<Anchors>
					<Anchor point="TOPLEFT">
						<Offset x="38" y="-419" />
					</Anchor>
				</Anchors>
			</Button>
			<Button name="ORC_UserPass" inherits="UIPanelButtonTemplate" id="4" text="Passen">
				<Size>
					<AbsDimension x="64" y="23" />
				</Size>
				<Anchors>
					<Anchor point="TOPLEFT">
						<Offset x="39" y="-301" />
					</Anchor>
				</Anchors>
				<Scripts>
					<OnClick>ORC_UserPass_OnClick();</OnClick>
				</Scripts>
			</Button>
			<Button name="ORC_UserClose" inherits="UIPanelButtonTemplate" id="1" text="Schließen">
				<Size>
					<AbsDimension x="75" y="23" />
				</Size>
				<Anchors>
					<Anchor point="TOPLEFT">
						<Offset x="233" y="-477" />
					</Anchor>
				</Anchors>
				<Scripts>
					<OnClick>ORC_UserClose_OnClick();</OnClick>
				</Scripts>
			</Button>
		</Frames>
		<Layers>
			<Layer level="OVERLAY">
				<FontString name="FontString1" inherits="GameFontNormal" text="Name:" justifyH="LEFT">
					<Size>
						<AbsDimension x="80" y="20" />
					</Size>
					<Anchors>
						<Anchor point="TOPLEFT">
							<Offset x="12" y="-20" />
						</Anchor>
					</Anchors>
				</FontString>
				<FontString name="ORC_UserName" inherits="GameFontNormal" text="Name" justifyH="LEFT">
					<Size>
						<AbsDimension x="297" y="20" />
					</Size>
					<Anchors>
						<Anchor point="TOPLEFT">
							<Offset x="112" y="-20" />
						</Anchor>
					</Anchors>
				</FontString>
				<FontString name="FontString2" inherits="GameFontNormal" text="Klasse:" justifyH="LEFT">
					<Size>
						<AbsDimension x="80" y="20" />
					</Size>
					<Anchors>
						<Anchor point="TOPLEFT">
							<Offset x="12" y="-46" />
						</Anchor>
					</Anchors>
				</FontString>
				<FontString name="ORC_UserClass" inherits="GameFontNormal" text="Class" justifyH="LEFT">
					<Size>
						<AbsDimension x="297" y="20" />
					</Size>
					<Anchors>
						<Anchor point="TOPLEFT">
							<Offset x="112" y="-45" />
						</Anchor>
					</Anchors>
				</FontString>
				<FontString name="Specc:" inherits="GameFontNormal" text="Specc:" justifyH="LEFT">
					<Size>
						<AbsDimension x="80" y="20" />
					</Size>
					<Anchors>
						<Anchor point="TOPLEFT">
							<Offset x="12" y="-72" />
						</Anchor>
					</Anchors>
				</FontString>
				<FontString name="ORC_UserSpecc" inherits="GameFontNormal" text="Specc" justifyH="LEFT">
					<Size>
						<AbsDimension x="297" y="20" />
					</Size>
					<Anchors>
						<Anchor point="TOPLEFT">
							<Offset x="112" y="-72" />
						</Anchor>
					</Anchors>
				</FontString>
				<FontString name="FontString3" inherits="GameFontNormal" text="Stat-Prio:" justifyH="LEFT">
					<Size>
						<AbsDimension x="80" y="20" />
					</Size>
					<Anchors>
						<Anchor point="TOPLEFT">
							<Offset x="12" y="-112" />
						</Anchor>
					</Anchors>
				</FontString>
				<FontString name="ORC_UserStatprio" inherits="GameFontNormal" text="Duxensteinigkeit &gt; Bärtigkeit = STFU = kackamannistdoof" justifyV="TOP" justifyH="LEFT">
					<Size>
						<AbsDimension x="474" y="60" />
					</Size>
					<Anchors>
						<Anchor point="TOPLEFT">
							<Offset x="12" y="-139" />
						</Anchor>
					</Anchors>
				</FontString>
			</Layer>
		</Layers>
	</Frame>
</Ui>
Can anyone help me please? I'm totally confused. I bet it's just a simple detail, but I don't get it.

Sincerly yours

odjur84
  Reply With Quote
01-29-15, 11:15 AM   #2
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 822
Check your toc file. Make sure your script is loaded in XML-File OR toc ... not in both.
__________________
The cataclysm broke the world ... and the pandas could not fix it!
  Reply With Quote
01-29-15, 11:23 AM   #3
odjur84
A Fallenroot Satyr
 
odjur84's Avatar
Join Date: Jan 2015
Posts: 24
Arg, I'm feeling so stupid.

Thank you very much! That did the trick. And I tought "no need to check the TOC-file.. what can go wrong with 3 lines?"
  Reply With Quote
01-29-15, 12:55 PM   #4
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 822
This is only the first trap when using xml I'd recommend to not use XML at all.
Lua can do most things that are needed without any XML.
__________________
The cataclysm broke the world ... and the pandas could not fix it!
  Reply With Quote
01-29-15, 02:18 PM   #5
odjur84
A Fallenroot Satyr
 
odjur84's Avatar
Join Date: Jan 2015
Posts: 24
Do you mean creating the XML-elements of the interface by using lua-code instead of using a separate XML-file? Or how do you create an interface without using XML at all?
  Reply With Quote
01-29-15, 02:26 PM   #6
Banknorris
A Chromatic Dragonspawn
 
Banknorris's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2014
Posts: 153
http://wow.gamepedia.com/Widget_API
  Reply With Quote
01-29-15, 07:21 PM   #7
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
For an example, here is how you would replicate your first (smaller) XML file in Lua alone:

Code:
local frame = CreateFrame("Frame", "MyAddonFrame", UIParent) -- [1]
frame:SetSize(184, 96) -- [2]
frame:SetPoint("CENTER", -8, 52)
frame:SetBackdrop({
	bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background", tile = true, tileSize = 32,
	edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Border", edgeSize = 32,
	insets = { left = 11, right = 12, top = 12, bottom = 11 }
})

local button = CreateFrame("Button", nil, frame, "UIPanelButtonTemplate") -- [3]
button:SetText("Button1")
button:SetSize(75, 23)
button:SetScript("OnClick", function(self, mouseButton)
	-- [4]
end)
frame.button = button -- [5]

local text = frame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
text:SetText("Hello, World!")
text:SetSize(80, 20)
text:SetPoint("TOPLEFT", 50, -12)
frame.text = text -- [6]
Notes:

[1] The name value you provide when creating a frame is set as a global variable, and all addons (plus the default UI code) share the same global namespace. A global name like "Frame1" is bad for several reasons. First, it's likely to conflict with globals (intentional or otherwise) from other addons or the default UI. Second, when users are trying to figure out which addon a frame belongs to (for example, if they want to move the frame, they need to know where to look for options) just seeing "Frame1" in a /fstack tooltip doesn't help them. A better name would be something like "MyAddonFrame" where "MyAddon" is the name of your addon -- this is both helpful to users, and unlikely to conflict with anyone else's code.

[2] object:SetSize(width, height) is shorthand for object:SetWidth(width) and object:SetHeight(height).

[3] Typically only your main frame should have a global name, and its child frames and regions should not have global names. The exception to this rule is when inheriting from a Blizzard template that expects a name. However, the "UIPanelButtonTemplate" template doesn't expect a name (none of its child regions are named, and none of its scripts identify it or its child regions with :GetName() calls) this button doesn't need one.

[4] Put the contents of the "Button1_OnClick" function here, and avoid such generic global function names in the future. The stuff in [1] applies equally to all globals, regardless of whether they're the names of frames, the names of functions, or anything else.

[5] This will let you refer to "frame.button" later instead of needing the "local button" to be in scope or needing to do a global lookup on the button's name if it had one. If you're never going to do anything with the button from other parts of the code, you can just leave this line out.

[6] Same as [5].
__________________
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
01-30-15, 04:47 PM   #8
odjur84
A Fallenroot Satyr
 
odjur84's Avatar
Join Date: Jan 2015
Posts: 24
@Banknorris

Thanks for the links! I'll look it up soon!

@Phanx

Thanks to you also. I think, I will transform my XML-file into lua-only-code like you suggest in the final stage of my project. But before then I'll stick to the XML-files, simply because I think, that they offer a better visual landmark for me as a developer. When I have all the X- and Y-positions of all frame-elements, I can converse them easier to lua-only-code. :-)

And also thank you for your additional notes, but note, that the first XML-file only was developed for testing, in the second (larger) file I mostly did use uncommon frame-names (like OCR_FrameUser). Nevertheless as from now I'm using 'nil' as names for most objects, thanks!
  Reply With Quote
01-31-15, 09:36 AM   #9
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
In XML if you write "nil" as the value for the name attribute, that'll actually result in a global name of "nil" (a string) which isn't quite the same; to achieve the desired result (object without a name) just omit the entire attribute.
__________________
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
02-04-15, 08:57 AM   #10
odjur84
A Fallenroot Satyr
 
odjur84's Avatar
Join Date: Jan 2015
Posts: 24
Hi!

Can you tell me how to omit the name if I want to have access to further arguments? Because

Code:
newButton = CreateFrame("Button", , ORC_UserFrame);
throws an error message.
  Reply With Quote
02-04-15, 09:34 AM   #11
Choonstertwo
A Chromatic Dragonspawn
 
Choonstertwo's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2011
Posts: 194
Originally Posted by odjur84 View Post
Hi!

Can you tell me how to omit the name if I want to have access to further arguments? Because

Code:
newButton = CreateFrame("Button", , ORC_UserFrame);
throws an error message.
Just use nil:
Code:
newButton = CreateFrame("Button", nil, ORC_UserFrame);
  Reply With Quote
02-04-15, 10:22 AM   #12
odjur84
A Fallenroot Satyr
 
odjur84's Avatar
Join Date: Jan 2015
Posts: 24
Well, as i understood Phanx, that it not equivalent to setting no name at all.
  Reply With Quote
02-04-15, 10:24 AM   #13
Duugu
Premium Member
 
Duugu's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 851
newButton = CreateFrame("Button", ORC_UserFrame);

CreateFrame can be called with one, two, tree, or four (five?) arguments and "decides" on the number of arguments how to handle them.
Two arguments are handled as string/frame type and object/parent frame.
Tree arguments are handled as string/frame type, string/frame Name, and object/parent frame
The concept is called "function overloading" if you would like to dig into it.

Last edited by Duugu : 02-04-15 at 10:33 AM.
  Reply With Quote
02-04-15, 10:27 AM   #14
Choonstertwo
A Chromatic Dragonspawn
 
Choonstertwo's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2011
Posts: 194
Originally Posted by odjur84 View Post
Well, as i understood Phanx, that it not equivalent to setting no name at all.
That's only in XML. Passing nil to CreateFrame in Lua actually passes nil.
  Reply With Quote
02-04-15, 11:01 AM   #15
odjur84
A Fallenroot Satyr
 
odjur84's Avatar
Join Date: Jan 2015
Posts: 24
Thanks to you two and especially to Duugu for the enlightenment!
  Reply With Quote
02-04-15, 12:57 PM   #16
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by odjur84 View Post
Thanks to you two and especially to Duugu for the enlightenment!
Yes. XML values are all strings. The equivalent of name="nil" in Lua would be "nil" with quotes (a string value) as opposed to nil without quotes (a keyword representing a non-value).
__________________
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

WoWInterface » Developer Discussions » Lua/XML Help » XML problem: Twice function call


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