Thread Tools Display Modes
05/01/14, 01:41 AM   #1
arkemiffo
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 16
Dynamic buttons and function calls

Hello,

I'm in the process of building my first proper addon. the actual function of it is complete, all I need is to tie it in with the UI at the moment.
The addon is about mixture of ingredients for alchemy, so I have set up a loop to create virtual buttons, and they show up as they should. My problem however is that the handler of the buttons doesnt seem to work.
When I hardcoded the handler to a certain button I had no problems, but that kinda defeats the purpose of doing it in a loop.

The XML:
Code:
		<Button name="EffectButton" font="ZoFontGame" color="CFDCBD" mouseEnabled="true" 
			verticalAlignment="CENTER" horizontalAlignment="CENTER" virtual="true">
			<Dimensions x="200" y="30" />
			<Anchor point="TOPLEFT" />
			<Controls>
				<Backdrop name="$(parent)BG" inherits="ZO_ThinBackdrop" />
			</Controls>						
		</Button>
The lua:

Code:
local function NotebookInitialize(eventCode, addOnName)
	if(addOnName == "AlchemistsNotebook") then
		local counter = 0
		for columns = 1,2 do
			for rows = 1,6 do
				counter = counter + 1
				local EffectButtonControl = CreateControlFromVirtual("EffectButton", AlchemistsNotebook_ResultsBG, "EffectButton", counter)
				EffectButtonControl:SetText(antitrait_table[counter][1])
				EffectButtonControl:SetHandler("OnMouseDown", function(self) NotebookOnChange(counter) end)
				EffectButtonControl:SetSimpleAnchorParent(5+(200*(columns-1)),5+(30*(rows-1)))
			end
		end
	end
end
the buttons should be in 2 columns, with 6 buttons in each, that's why I have 2 for's.
The local var counter here is always 12 when actually pressing a button now. From all my years of programming other languages, it should hold the value it was during the iteration, but it seems it doesn't. Anyone that can help me?

(The counter binds into a table with the all the positive traits you can get from the ingredients, hence why it's so important to have it return the correct value).
  Reply With Quote
05/01/14, 02:08 AM   #2
Harven
 
Harven's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 135
Have you tried setting mouseEnabled in lua for each button?
  Reply With Quote
05/01/14, 03:54 AM   #3
arkemiffo
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 16
Thanks for the reply.

No, I haven't tried that. At work at the moment, so I'm not able to try it out right now, but I'll try when I get home.

However, shouldn't the template button hold that already, as it's there already?
And what exactly would that do? The buttons do interact with the mouse click (or mouse down more specifically), it's just that the counter in the handler doesn't seem to be saved by iteration in each instance of the button creation, but all of the buttons hold the number 12 (which is the last iteration of the var counter). When setting the text I do get the correct value from the counter in the antitrait_table[counter][1]. This is what's confusing me. Why the counter does give the correct value in the set text, but not in the sethandler on the very next line.

Last edited by arkemiffo : 05/01/14 at 04:01 AM. Reason: Clarifying where the counter is incorrect.
  Reply With Quote
05/01/14, 04:14 AM   #4
Harven
 
Harven's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 135
Originally Posted by arkemiffo View Post
... it's just that the counter in the handler doesn't seem to be saved by iteration in each instance of the button creation, but all of the buttons hold the number 12 (which is the last iteration of the var counter). ...
It's because function in OnMouseDown is called within a closure (read here). You must do it that way:
Code:
local c = counter
EffectButtonControl:SetHandler("OnMouseDown", function(self) NotebookOnChange(c) end)
  Reply With Quote
05/01/14, 05:55 AM   #5
arkemiffo
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 16
That makes sense.
Shouldn't the c-declaration be within the anonymous function though?
Otherwise the scope of counter and c will be exactly the same?
  Reply With Quote
05/01/14, 06:09 AM   #6
Harven
 
Harven's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 135
The c variable declaration is inside the inner most for loop. It's re-declared with every iteration, so every closure has it's own c.

EDIT:
If you declare c inside the anonymous function it will still be 12 in the moment of call (because it will evaluate in that moment)

Last edited by Harven : 05/01/14 at 06:19 AM.
  Reply With Quote
05/02/14, 01:35 PM   #7
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 648
Instead of using a counter variable to get up to 12, why not just do rows*columns ?
  Reply With Quote
05/07/14, 01:41 AM   #8
arkemiffo
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 16
Sorry, forgot to answer this thread.

Yes, it did work with the declaration of the c-variable within the loop.
The closed system is something new to me, so I think I need to sit down and look through it properly.
Thank you very much for your help.

The reason I used a counter is that rows*columns wouldn't work without adding more to the formula, and at the time I did this I couldn't figure it out properly, and then it was my curiosity that took over on why the variable wouldn't render as I though.

Each iteration must have a number which is one higher than the previous, and 1*2 and 2*1 has the same result et.c. for example.
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » Dynamic buttons and function calls

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