ESOUI

ESOUI (https://www.esoui.com/forums/index.php)
-   Lua/XML Help (https://www.esoui.com/forums/forumdisplay.php?f=175)
-   -   Dynamic buttons and function calls (https://www.esoui.com/forums/showthread.php?t=1296)

arkemiffo 05/01/14 01:41 AM

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

Harven 05/01/14 02:08 AM

Have you tried setting mouseEnabled in lua for each button?

arkemiffo 05/01/14 03:54 AM

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.

Harven 05/01/14 04:14 AM

Quote:

Originally Posted by arkemiffo (Post 6562)
... 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)


arkemiffo 05/01/14 05:55 AM

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?

Harven 05/01/14 06:09 AM

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)

Seerah 05/02/14 01:35 PM

Instead of using a counter variable to get up to 12, why not just do rows*columns ?

arkemiffo 05/07/14 01:41 AM

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.


All times are GMT -6. The time now is 06:11 AM.

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