Sorry, we don't support your browser.  Install a modern browser

A Buttons click_function should know the index of the Button clicked#686

For my TTRPG-Party I script complex stuff like Character Sheets, but it is quite annoying that a click_function has no mean to know what button on a Object has been clicked, it just returns the object.
The Character Sheet has >100 Buttons and I have to make a separate method for every button, just so I know which Button has been clicked and it is really annoying.
If I had the Index, I could just use that Index to distinguish the buttons and immediatly know, which value I’d need to use to count up/down or display.

please change click_function signature from
click_function( obj, player_color, alt_click)
to
click_function( obj, player_color, alt_click, button_index)

8 days ago

This is possible by using Lua:

local function getButtonIndex(click_function_name)
    for _,v in pairs(self.getButtons()) do
        if v.click_function == click_function_name then
            return v.index
        end
    end
    return nil
end

function myClickFunction( obj, player_color, alt_click )
    local button_index = getButtonIndex("myClickFunction")
    print(button_index)
end
8 days ago

For that to work, I still would need to manually define one function per button and that’s exactly what I’m trying to work around.
Imagine, that every button has the same click_function. How would it work then?

8 days ago

Then it should use button title.
I just want to say that it’s not a big deal if there is no index parameter.

8 days ago

And how do I get the button title a generic click function? It would be the same mess as with the index because I can’t get a hand on the UI element that was clicked, without writing A LOT boilerplate code

8 days ago

My problem is not that I can’t do it. (I currently do it by named click functions). My problem is the boilerplate code I need for it.
I have >100 buttons my script is bloated to 1000+ lines. With my suggestion I should be able to do it in less than 300 lines.

8 days ago

Okay, you can make tiny code that does the trick. I did not test it, but it should work:

local function attachIndex(click_function, index)
    local fn_name = click_function .. index
    _G[fn_name] = function(obj, player_color, alt_click)
        _G[click_function](obj, player_color, alt_click, index)
    end
    return fn_name
end

function onLoad()
    self.createButton{
        click_function = attachIndex("click_func",1),
        function_owner = self,
        label          = "Test1",
    }
    self.createButton{
        click_function = attachIndex("click_func",2),
        function_owner = self,
        label          = "Test2",
    }
    self.createButton{
        click_function = attachIndex("click_func",3),
        function_owner = self,
        label          = "Test3",
    }
    -- ofc it's better to use for i=1,N do ....
end

function click_func(obj, player_color, alt_click, index)
    print(index)
end

Actually exact code depends on the mod.

7 days ago

Yup, that works for me!

I’m quite new to Lua, so I didn’t know about that _G stuff. Thank you very, very much for your time, patience and help! :-)

7 days ago
1