Thread Tools Display Modes
06-30-12, 10:20 AM   #1
sigEleven
A Murloc Raider
Join Date: Jun 2012
Posts: 7
Unhappy Trouble initializing UIDropDownMenu from XML

Hey All, hoping someone can lend me a hand here.

I've been trying to wrap my head around drop down menus for a while now, and it's surprisingly difficult. After following multiple examples and tutorials that don't work, I've hacked together some operable code to get my drop downs working.

However, I'd like to create the dropdowns in XML, and not clutter up my LUA code with it. The problem I'm noticing right now is that I cannot initialize the dropdowns correctly in XML.

This does not work correctly (I end up with a blank drop down box that says "Custom" with no options):

Code:
<Button name="BName" parent="Parent_Frame" inherits="UIDropDownMenuTemplate"> 
   <Scripts>
      <OnLoad>  UIDropDownMenu_Initialize(BName, BName_initialize)</OnLoad>
    </Scripts>
</Button>

However, this works fine in LUA, when placed in the drop down's parent frame onload function.
Lua Code:
  1. UIDropDownMenu_Initialize(BName, BName_initialize)

Hopefully I'm just missing something simple, anybody have any idea what's up?

Last edited by sigEleven : 06-30-12 at 10:46 AM.
  Reply With Quote
06-30-12, 10:40 AM   #2
Haleth
This Space For Rent
 
Haleth's Avatar
Featured
Join Date: Sep 2008
Posts: 1,173
Not sure if that's the problem, but the first argument of your function in your XML code is not the same as in your lua code. BName and BName_Filter.
  Reply With Quote
07-01-12, 02:00 AM   #3
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
(1) XML is hideous and ugly, and far more cluttered than any Lua.

(2) Is your "BName_initialize" function already loaded when your XML loads? If not (eg. if you are loading the XML file first, and then the Lua file where the function is defined) then that is your problem.
__________________
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
07-03-12, 03:52 PM   #4
sigEleven
A Murloc Raider
Join Date: Jun 2012
Posts: 7
Thanks for the replies,

Haleth: I caught that, and did an edit, but I guess you got your reply in first. I did confirm the function is correct.

Phanx: I agree; call it an OCD thing, but I'd like all my elements statically created in XML, in a separate file. Confirmed functions (lua file) loading first, then xml.

Has anybody been able to duplicate this, or have any other ideas?
__________________
There are 10 types of people who exist in this world, those who get it, and those who don't.
  Reply With Quote
07-03-12, 04:06 PM   #5
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
Originally Posted by sigEleven View Post
Phanx: I agree; call it an OCD thing, but I'd like all my elements statically created in XML, in a separate file.
...and needlessly inserting their names into the global namespace.
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.
  Reply With Quote
07-03-12, 04:10 PM   #6
sigEleven
A Murloc Raider
Join Date: Jun 2012
Posts: 7
Yeah, because that bit of memory allocation is going to kill people's machines. Thanks for the help Torhal.
__________________
There are 10 types of people who exist in this world, those who get it, and those who don't.
  Reply With Quote
07-03-12, 06:19 PM   #7
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
It seems like the "OCD" thing to do (quotes because I'm pretty sure 99.99999% of the people who say "I'm OCD" or "it's an OCD thing" do not actually have OCD) would be to have less clutter, not more...

Anyway, there's basically nothing more anyone can do to help you given the information you'd provided. If you want any meaningful help with your code, we need to see the entirety of your actual code, not just some random snippets that may or may not be relevant. You can either attach your addon files to your post using the forum attachment functions, or you can copy paste the entire contents of each file into a separate paste on any of the myriad other pastebin services that exist on the Internet. See my signature for links to a few popular ones.
__________________
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
07-03-12, 06:25 PM   #8
sigEleven
A Murloc Raider
Join Date: Jun 2012
Posts: 7
Please close the thread then.

Originally Posted by Phanx View Post
It seems like the "OCD" thing to do (quotes because I'm pretty sure 99.99999% of the people who say "I'm OCD" or "it's an OCD thing" do not actually have OCD) would be to have less clutter, not more...

Anyway, there's basically nothing more anyone can do to help you given the information you'd provided. If you want any meaningful help with your code, we need to see the entirety of your actual code, not just some random snippets that may or may not be relevant. You can either attach your addon files to your post using the forum attachment functions, or you can copy paste the entire contents of each file into a separate paste on any of the myriad other pastebin services that exist on the Internet. See my signature for links to a few popular ones.
__________________
There are 10 types of people who exist in this world, those who get it, and those who don't.
  Reply With Quote
07-03-12, 07:27 PM   #9
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
I'm not a moderator, but your request seems ridiculous. You asked for help, but gave us basically no details about what you need help with. We offered a few suggestions. You said they didn't work. I told you you'd need to give us more details if you want better suggestions. You'd rather close the thread than show us your code? Really? Are you working on a top-secret World of Warcraft addon for the NSA?
__________________
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
07-03-12, 08:26 PM   #10
sigEleven
A Murloc Raider
Join Date: Jun 2012
Posts: 7
Although I have done programming work for the DoD, this is obviously not one of those projects. I'm not sure why this seems ridiculous to you. I have supplied the code of calling the same function in two different ways, 1 via XML (code already provided), and 2 in lua (code already provided), which from my understanding, should have the same effect. Although, in my testing, they do not (the XML does not seem to call the function), so I was asking if anyone else could duplicate this, or provide an explanation.

It seems ridiculous to me, that you want me to post the entirety of the code for this add-on (which is thousands of lines, over multiple files), in order to replicate the creation of a simple drop down menu. I really don't want to deal with the hassle, especially with trolls like Torhal already on this thread, flaming about a few KB of memory, and global namespace usage, while providing no relevant information to the topic. Also, you have not clearly stated what information you're looking for. My best guess would be the function that is being called.

Bottom line, it's not worth the hassle. It's obviously not some silly mistake on my part, as none of you caught it, and it will be far easier just to deal with the creation of the objects in lua, than to try elaborate work arounds. I'll supply the information again, and include the function this time, in case someone finds this thread, and is actually interested.

The Function:
Lua Code:
  1. function UNIQUEGLOBALFORTORHAL_MYBUTTON12345_Initiallize(self, level)
  2.    local info = UIDropDownMenu_CreateInfo()
  3.    local items = {'Opt1', 'Opt2', 'Opt3'};
  4.    for k,v in pairs(items) do
  5.       info = UIDropDownMenu_CreateInfo()
  6.       info.text = v
  7.       info.value = v
  8.       info.func = UNIQUEGLOBALFORTORHAL_MYBUTTON12345_OnClick
  9.       UIDropDownMenu_AddButton(info, level)
  10.    end
  11.  
  12.    --Flame on about the fact I didn't unallocate the memory for these variables when I was done with them.
  13. end

XML initialization - results in no population of the drop down (function does not seem to be called)
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">
<Frame name = "UNIQUEGLOBALFORTORHAL_MYFRAME12345">
   <!-- positioning, inheritance, sizing, artwork ... blah, blah, blah -->
   <Scripts>
      <OnLoad>UNIQUEGLOBALFORTORHAL_MYFRAME12345_OnLoad</OnLoad>
   </Scripts>
   <Frames>
      <Button name="UNIQUEGLOBALFORTORHAL_MYBUTTON12345" inherits="UIDropDownMenuTemplate"> 
         <!-- positioning, inheritance, sizing, artwork ... blah, blah, blah -->
         <Scripts>
            <OnLoad>  UIDropDownMenu_Initialize(UNIQUEGLOBALFORTORHAL_MYBUTTON12345, UNIQUEGLOBALFORTORHAL_MYBUTTON12345_Initiallize)</OnLoad>
          </Scripts>
      </Button>
   </Frames>
</Frame>
</Ui>
lua initialization - results as expected
Lua Code:
  1. function UNIQUEGLOBALFORTORHAL_MYFRAME12345_OnLoad()
  2.    UIDropDownMenu_Initialize(UNIQUEGLOBALFORTORHAL_MYBUTTON12345, UNIQUEGLOBALFORTORHAL_MYBUTTON12345_Initiallize)
  3. end

Flame on that I have an extra 'l' in 'Initiallize'

Originally Posted by Phanx View Post
I'm not a moderator, but your request seems ridiculous. You asked for help, but gave us basically no details about what you need help with. We offered a few suggestions. You said they didn't work. I told you you'd need to give us more details if you want better suggestions. You'd rather close the thread than show us your code? Really? Are you working on a top-secret World of Warcraft addon for the NSA?
__________________
There are 10 types of people who exist in this world, those who get it, and those who don't.
  Reply With Quote
07-03-12, 11:21 PM   #11
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
I wasn't trolling, I was just adding that not only is XML completely unnecessary, but it clutters the global namespace. If you do professional programing, you should understand the value of namespaces and keeping them uncluttered. It isn't a matter of "a few KB of memory", it's about organization and not having things from other AddOns stomping all over your names (intentionally or otherwise).

The reason for wanting to see the code in its entirety is because you may have scoping issues. You may be calling things before they're actually defined. If you'd rather freak out on people who are honestly trying to help you, because they are doing so in a manner that you'd rather not deal with, then so be it.
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.
  Reply With Quote
07-03-12, 11:54 PM   #12
sigEleven
A Murloc Raider
Join Date: Jun 2012
Posts: 7
Torhol: I've read some of your posts. Obviously, you are a good programmer, and display a level of intelligence far higher than average; however your first post provided no helpful information regarding the issue at hand, not to mention it being derogatory; hence the comment about trolling and flaming. Yes, I know all about scoping, and namespace, and memory management etc. Frankly, this is a stupid little project running on an API from a poorly optimized, poorly multi-threaded application; so yes, I've cut a lot of corners. My goal was to introduce myself to XML, "in a fun way", as I have never utilized it in my programming before. I generally work with databases (and DB applications), file systems, and storage drivers. Hopefully this explains why I'm using XML at all, and started this post, asking if anyone can see an obvious problem, or is able to reproduce the issue.

I'm not sure how I can further elaborate on the issue in a meaningful way. I've included all the relevant pieces of code already. If anyone requires a full add-on, all that needs to be done is to copy and paste my snippets, wrap it up with a .toc file (including the lua file(s) first, as already mentioned), and add some more properties so the frame and button are visible and positioned, as they would like.
__________________
There are 10 types of people who exist in this world, those who get it, and those who don't.
  Reply With Quote
07-04-12, 02:56 AM   #13
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by sigEleven View Post
I have supplied the code of calling the same function in two different ways, 1 via XML (code already provided), and 2 in lua (code already provided), which from my understanding, should have the same effect. Although, in my testing, they do not (the XML does not seem to call the function), so I was asking if anyone else could duplicate this, or provide an explanation.
Since this is neither the expected behavior of the WoW scripting environment, nor behavior observed by anyone else, the problem is obviously in some other part of your code, or maybe in how your code as a whole is structured. Without seeing it, though, we can't tell you. We can only tell you that there is nothing wrong with the tiny snippets you posted.

Originally Posted by sigEleven View Post
It seems ridiculous to me, that you want me to post the entirety of the code for this add-on (which is thousands of lines, over multiple files), in order to replicate the creation of a simple drop down menu. .... Also, you have not clearly stated what information you're looking for.
Again, that's because we have no idea what your code looks like. If we knew what was in your code and what to ask for, we would already be able to tell you where the problem is.

Originally Posted by sigEleven View Post
Flame on about the fact I didn't unallocate the memory for these variables when I was done with them.
If you're talking about the local info and items variables, you actually did deallocate the memory for them. Since they are inside a function, new instances of them are created each time you run the function, and are flagged for garbage collection (deallocation) at the end of the run since they are no longer in scope and are -- in the case of items whose value is a table, and thus passed by reference instead of value -- (presumably) not referenced anywhere else.

(On a side note, tables are fairly expensive to create in Lua, so you should aim to reuse them as much as possible. If the options table is static, it would be better to define it outside of the dropdown's initialize function. Maybe you're already doing this in your real code, but since we have no idea what your real code looks like, there's no way for us to know.)

Code:
<OnLoad>UNIQUEGLOBALFORTORHAL_MYFRAME12345_OnLoad</OnLoad>
This is not a valid call to the UNIQUEGLOBALFORTORHAL_MYFRAME12345_OnLoad function. Elsewhere in the code you just posted, you reference functions that are not defined, such as UNIQUEGLOBALFORTORHAL_MYBUTTON12345_OnClick.

Honestly, in the time you took to write all of that vague "example" code which does not actually help anyone figure out what's wrong with your code, you could have attached 100+ files to your post. I know you don't think it matters, and you seem to think your code is flawless and precious, but it really does matter. It may sound harsh, but if you can't figure out what the problem is and solve it yourself, what makes you think you can tell which parts of the code are relevant to debugging the problem?

As someone with a background in programming as something other than a hobby, you shouldn't be surprised or take it personally that other programmers want to see your code when you ask them why it isn't working the way you expect.

As far as the length or size of your code, of course nobody wants to read through 5,000,000 lines of code. That's not why we want you to post it. We want you to post it because the easiest way for us to help you -- especially with a large or complex addon -- is to load and debug your addon in-game, but without having the full, actual code, we can't do that.

--------------------

Finally, I loaded your "example" code in-game, with no other addons enabled, and was not able to reproduce the issue you described.

!!!Sandbox.toc
Code:
## Interface: 40300
Sandbox.xml
Sandbox.lua
Code:
-- Debug prints to see when "UIDropDownMenu_Initialize" is called for your dropdown:
hooksecurefunc("UIDropDownMenu_Initialize", function(frame, func)
	local name = frame:GetName()
	if name == "MYBUTTON12345" then
		print("UIDropDownMenu_Initialize", name)
	end
end)

function MYBUTTON12345_Initialize(self, level)
	-- Debug print to see when this function is called:
	print("MYBUTTON12345_Initialize")
	local info = UIDropDownMenu_CreateInfo()
	local items = {'Opt1', 'Opt2', 'Opt3'}
	for k,v in pairs(items) do
		info = UIDropDownMenu_CreateInfo()
		info.text = v
		info.value = v
		info.func = MYBUTTON12345_OnClick
		UIDropDownMenu_AddButton(info, level)
	end
end

function MYFRAME12345_OnLoad()
	-- Debug print to see when this function is called:
	print("MYFRAME12345_OnLoad")
	-- Comment or uncomment this line for testing:
	-- UIDropDownMenu_Initialize(MYBUTTON12345, MYBUTTON12345_Initialize)
end
Sandbox.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="Sandbox.lua"/>
	<Frame name="MYFRAME12345">
		<Scripts>
			<OnLoad>
				-- Debug print to see when this script is executed:
				print("MYFRAME12345: OnLoad")
				MYFRAME12345_OnLoad(self)
			</OnLoad>
		</Scripts>
		<Frames>
			<Button name="MYBUTTON12345" inherits="UIDropDownMenuTemplate">
				<Scripts>
					<OnLoad>
						-- Debug print to see when this script is executed:
						print("MYBUTTON12345: OnLoad")
						-- Comment or uncomment this line for testing:
						UIDropDownMenu_Initialize(self, MYBUTTON12345_Initialize)
					</OnLoad>
				</Scripts>
			</Button>
		</Frames>
	</Frame>
</Ui>
The TOC calls only the XML file, and the XML file loads the Lua file before defining any frames, so all the functions defined in the Lua file are available to be called in scripts defined in the XML. Yes, I edited the global variables and comments to remove the unnecessary and inflammatory remarks. All other changes between the above and your posted code are either (a) syntax corrections so the code will load and function without errors, (b) additions of debug print statements, or (c) whitespace or indentation changes.

Results:

When UIDropDownMenu_Initialize is called from both MYFRAME12345_OnLoad and the button's XML OnLoad script:


When UIDropDownMenu_Initialize is called from only the button's XML OnLoad script:


When UIDropDownMenu_Initialize is called only from MYFRAME12345_OnLoad:


As you can see, UIDropDownMenu_Initialize is called and runs your MYBUTTON12345_Initialize function as expected in all three cases. (The order of debug messages for those two is actually the reverse of the order in which the functions are called, since the debug print for MYBUTTON12345_Initialize occurs at the beginning of the function, while the print for UIDropDownMenu_Initialize occurs after the entire function has run, since it's a secure post-hook.)

I feel I've already put in way more effort trying to help you than your posts and your attitude deserve. If you want more help, you really do need to post your actual code, not some "example" code that you obviously dry-coded because it's riddled with basic syntax errors and does not even illustrate the problem once those are fixed. You don't even have to copy and paste; you can simply scroll down on the posting page to the "Additional Options" section, click the "Manage Attachments" button, and attach your files to your post as-is.
__________________
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.

Last edited by Phanx : 07-04-12 at 02:58 AM.
  Reply With Quote
07-04-12, 08:25 AM   #14
Haleth
This Space For Rent
 
Haleth's Avatar
Featured
Join Date: Sep 2008
Posts: 1,173
Please remember to stay civilized, everyone. If someone doesn't post their code when you ask them to, there's no need to repeat yourself over and over.

sigEleven: Phanx and Torhal are both very experienced with WoW addons. They would be glad to help if you could show them your code (I don't even think Phanx minds sifting through a ton of code, as long as it includes the relevant parts). They're not trying to flame or troll you.
  Reply With Quote
07-04-12, 01:33 PM   #15
sigEleven
A Murloc Raider
Join Date: Jun 2012
Posts: 7
Since this is neither the expected behavior of the WoW scripting environment, nor behavior observed by anyone else...
Thanks, that's all I was looking for! Time for a re-install.
__________________
There are 10 types of people who exist in this world, those who get it, and those who don't.
  Reply With Quote
07-04-12, 02:11 PM   #16
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
I highly doubt you need to reinstall WoW. Again, there is most likely (99.9% sure) a problem that you have overlooked in a different part of your code.
__________________
"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
07-04-12, 02:46 PM   #17
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Okay, but don't be surprised when the same thing happens after you spend hours redownloading and reinstalling the game. Your total refusal to let anyone see your actual code is just mind-boggling. Why ask for help in the first place if you're just going to ignore everyone who tries to help you?
__________________
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
07-04-12, 04:04 PM   #18
Dridzt
A Pyroguard Emberseer
 
Dridzt's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 1,359
Is there any specific reason you're using a <Button> widjet as the base object to inherit from UIDropDownMenuTemplate?
  Reply With Quote
07-06-12, 10:07 AM   #19
Lombra
A Molten Giant
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 554
To be fair, frames inheriting UIDropDownMenuTemplate needs to be named regardless of how you create them. (as far as I remember) And you can use parentKey and relativeKey and stuff to avoid using global names. That being said, I avoid XML as much as the next guy.

Print BName and BName_initialize in both your self OnLoad and that of the parent's, see if they are the same, or even exist. But yeah, that's probably not all of the relevant code. Reinstalling WoW is not gonna help.
__________________
Grab your sword and fight the Horde!
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Trouble initializing UIDropDownMenu from XML

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