Results 1 to 6 of 6

Thread: How to do function of 50 buttons?

  1. #1

    Question How to do function of 50 buttons?

    I created 50 buttons labled 1 to 50.
    I want a functon to add a checkmark in label of pressed button.

  2. #2
    Join Date
    May 2016
    Posts
    1,072
    There is no params field in button creation currently, although it is my personally most desired feature for scripting.

    So that means you need to make 50 different functions. Each like:

    Code:
    function b01 triggerOtherFunction(1) end
    function b02 triggerOtherFunction(2) end
    function b03 triggerOtherFunction(3) end
    etc etc
    Then have all your created buttons with the click_function of b01 for the first button, b02 for the second ,etc And write a function triggerOtherFunction to take the paramiter being passed to it and update the correct button with it. Fun!

  3. #3
    So I've also been frustrated by the lack of button function parameters, but I just did some fiddling around and I've found a workaround for until they are kind enough to give us them. Closures to the rescue!

    Code:
    function onLoad()
        for i = 1, 10 do
            local buttonNum = tostring(i)
            local funcName = "buttonFunc" .. tostring(i)
            local params = { click_function = funcName
                            , function_owner = self
                            , label = buttonNum
                            , position = {1.25 + 0.5 * i,0,0} 
                            , width = 200
                            , height = 200
                            , font_size = 200 }
            local func = function()  --function(obj) if you need a reference to the object the button is on
                print("Clicked Button #" .. buttonNum)
            end
            self.setVar(funcName, func)
            self.createButton(params)
        end
    end
    This is just a simple pointless script to demonstrate the concept. Put it on any object you want, of course you may have to adjust the button positions. I was using one of the tiles from the piecepack for testing.

    Basically, if you have a local variable and define an anonymous function that references that local variable, the value of the variable gets stored along with the function so that it can keep using it. Then because functions on objects are just variables that store a function, you can store that anonymous function on your object with setVar and it'll work just as if you'd written the function with that name normally.

    That's probably a ridiculously oversimplified explanation but there's a reason I don't make claims about being a good teacher.

    Anyway, hope this helps.

  4. #4
    Join Date
    May 2016
    Posts
    1,072
    Now THIS is just fascinating. I can't wait to play around with it. This would free up a couple of projects I had on hold involving dynamic button creation.

  5. #5
    Join Date
    Sep 2016
    Posts
    219
    I wanted to see if I could find a way to improve on what MalicWanderer accomplished, but I think it's basically the same. Here's an example of what I came up with:

    Code:
    anyObject_GUID = 'fde649'
    
    function onload()
        local obj = getObjectFromGUID(anyObject_GUID)
        for i=1, 8 do
            _G['on' .. i .. 'Pressed'] = function (object, sPlayer)
                print(i)
            end
            local params = {
                label = '' .. i, click_function = 'on' .. i .. 'Pressed', width = 1000, height=600, font_size = 200, function_owner = nil
            }
            params.index = i
            params.rotation = {0, i * 45, 0}
            local x, z
            x, z = rotateCoordinatesDegrees(0, 6, i * 45)
            params.position = {x, 1, z}
            obj.createButton(params)
        end
    end
    
    --rotates coordinates in degrees
    function rotateCoordinatesDegrees(x, z, rotation)
        local rad_rotation = math.rad(rotation)
        local new_x = x * math.cos(rad_rotation) - z * math.sin(rad_rotation)
        local new_z = z * math.cos(rad_rotation) + x * math.sin(rad_rotation)
        return new_x, new_z
    end
    Basically, create the function and store it as a global variable. I'm not sure if there's any advantage to doing it this way.
    Last edited by Sancho; 11-02-2016 at 10:28 AM.

  6. #6
    Join Date
    May 2016
    Posts
    1,072
    I am still working out some kinks, but this is a fantastic solution for faking params on buttons. Great thinking, and thank you

    I'm using it to make a tool to trigger looping/trigger effects on an item. Dynamically creating buttons in a circle (or circles) so it can handle any number of triggers.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •