[TFS 1.2] Modal Window Helper Lib

Discussion in 'Mods & Lua Functions' started by Non Sequitur, Dec 11, 2015.

  1. Non Sequitur

    Non Sequitur Veritas

    Joined:
    Dec 1, 2015
    Messages:
    83
    Reasoning
    The current way we can implement modal windows is quite cumbersome in my opinion, having to create it somewhere, register an event, add buttons in some weird order to make it work and having to take account of window ids, button ids, choice ids in a complete different file than the one you've created the modal window. That is not optimal so I've created this helper library to help with that.

    What you can do with it
    Code:
    -- The helper lib is used by passing a table value to the ModalWindow function
    local window = ModalWindow {
        title = 'Title',
        message = 'Please, choose the lowest number and press [Ok]'
    }
    
    local lowestChoice
    for i = 1, 5 do
        local value = math.random(1, 100)
        -- modalWindow:addChoice() returns the choice object that will be passed to the callbacks
        local choice = window:addChoice(value)
        -- This way we can pass extra information to the callback
        choice.value = value
        if not lowestChoice or lowestChoice.value > value then
            lowestChoice = choice
        end
    end
    
    lowestChoice.correct = true
    
    -- Add a button with a specific callback
    window:addButton('Ok',
        function(button, choice)
            if choice.correct then
                print('Player selected the correct option.')
            else
                print('Player selected the incorrect option.')
            end
        end
    )
    -- Set this button as the default enter button
    window:setDefaultEnterButton('Ok')
    
    -- Add a button without a specific callback
    window:addButton('Close')
    window:setDefaultEscapeButton('Close')
    
    -- If a button without a specific callback is pressed, this fucntion will be called
    window:setDefaultCallback(
        function(button, choice)
            print('Default callback, button pressed: ' .. button.text .. ' player choice: ' .. choice.text)
        end
    )
    
    window:sendToPlayer(player)
    
    You can use this example in a talkaction and test it for yourself (check the output on your server console). Other than what is shown in the above code, you can also use modalWindow:addButtons(...) and modalWindow:addChoices(...) to add multiple buttons/choices at once.

    Installing

    • In data/lib/lib.lua
      Code:
      dofile('data/lib/modalwindow.lua')
    The end

    That's it. Later today I will post a !rank talkaction I've made using this library.

    EDIT: Here it is.
     
    Last edited: Dec 11, 2015
  2. zbizu

    zbizu ‌‌Luakami

    Joined:
    Nov 22, 2010
    Messages:
    2,425
    so... what is the point of that verification?
     
  3. Non Sequitur

    Non Sequitur Veritas

    Joined:
    Dec 1, 2015
    Messages:
    83
    What verification?
     
  4. zbizu

    zbizu ‌‌Luakami

    Joined:
    Nov 22, 2010
    Messages:
    2,425
    print('Player selected the correct option.')
    else
    print('Player selected the incorrect option.')
     
  5. Non Sequitur

    Non Sequitur Veritas

    Joined:
    Dec 1, 2015
    Messages:
    83
    That is just a sample code, and it's on the button callback, so the player may choose the wrong option and choice.correct will be nil, not true.
     
  6. strutZ

    strutZ Australian OT Member {AKA Beastn} Premium User

    Joined:
    Nov 16, 2014
    Messages:
    1,176
    This is great now that i know how to use it lol question though how does one use
    Code:
    getChoiceCount()
    Is there another way to use it?

    EDIT:
    Ran into another issue.. Converting my addon doll script, I am hiding already unlocked outfits/addons from the choices my command
    Code:
    self:addOutfitAddon(outfits[choice.id].male, 3)
    is wrong because choice ID 1 will always being citizen... so when i unlock that and it hides the top addon changes to choice ID one so BAM! i get citizen again >.<
     
    Last edited: May 24, 2016
  7. BloodGauntlet

    BloodGauntlet Member

    Joined:
    Jul 27, 2016
    Messages:
    45
    Great contribution, going to help me along nicely.
     
  8. Colors

    Colors

    Joined:
    Mar 22, 2013
    Messages:
    904
    If I create a ModalWindow, but I don't use it for x reason, do I need to destroy the object to avoid memory leaks? Or it doesn't matter?
     

Share This Page