[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
    Likes Received:
    68
    Best Answers:
    0
    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 (Text):
    1.  
    2. -- The helper lib is used by passing a table value to the ModalWindow function
    3. local window = ModalWindow {
    4.     title = 'Title',
    5.     message = 'Please, choose the lowest number and press [Ok]'
    6. }
    7.  
    8. local lowestChoice
    9. for i = 1, 5 do
    10.     local value = math.random(1, 100)
    11.     -- modalWindow:addChoice() returns the choice object that will be passed to the callbacks
    12.     local choice = window:addChoice(value)
    13.     -- This way we can pass extra information to the callback
    14.     choice.value = value
    15.     if not lowestChoice or lowestChoice.value > value then
    16.         lowestChoice = choice
    17.     end
    18. end
    19.  
    20. lowestChoice.correct = true
    21.  
    22. -- Add a button with a specific callback
    23. window:addButton('Ok',
    24.     function(button, choice)
    25.         if choice.correct then
    26.             print('Player selected the correct option.')
    27.         else
    28.             print('Player selected the incorrect option.')
    29.         end
    30.     end
    31. )
    32. -- Set this button as the default enter button
    33. window:setDefaultEnterButton('Ok')
    34.  
    35. -- Add a button without a specific callback
    36. window:addButton('Close')
    37. window:setDefaultEscapeButton('Close')
    38.  
    39. -- If a button without a specific callback is pressed, this fucntion will be called
    40. window:setDefaultCallback(
    41.     function(button, choice)
    42.         print('Default callback, button pressed: ' .. button.text .. ' player choice: ' .. choice.text)
    43.     end
    44. )
    45.  
    46. window:sendToPlayer(player)
    47.  
    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 (Text):
      1. 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
    grilo13, HeberPcL, Kuzyn and 5 others like this.
  2. zbizu

    zbizu ‌‌Luakami

    Joined:
    Nov 22, 2010
    Messages:
    2,437
    Likes Received:
    1,328
    Best Answers:
    0
    so... what is the point of that verification?
     
  3. Non Sequitur

    Non Sequitur Veritas

    Joined:
    Dec 1, 2015
    Messages:
    83
    Likes Received:
    68
    Best Answers:
    0
    What verification?
     
  4. zbizu

    zbizu ‌‌Luakami

    Joined:
    Nov 22, 2010
    Messages:
    2,437
    Likes Received:
    1,328
    Best Answers:
    0
    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
    Likes Received:
    68
    Best Answers:
    0
    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,199
    Likes Received:
    419
    Best Answers:
    0
    This is great now that i know how to use it lol question though how does one use
    Code (Text):
    1. 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 (Text):
    1. 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
    Likes Received:
    16
    Best Answers:
    0
    Great contribution, going to help me along nicely.
     
  8. Colors

    Colors

    Joined:
    Mar 22, 2013
    Messages:
    905
    Likes Received:
    247
    Best Answers:
    0
    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?
     
  9. adahum

    adahum New Member

    Joined:
    Feb 28, 2015
    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0


    i did exactly what u say to do but for some reason, appears it when i start my server:

    [9:57:14.242] data/lib/modalwindow.lua:83: attempt to call field 'modalWindowConstructor' (a nil value)
    [9:57:14.242] [Warning - Event::loadScript] Cannot load script (data/talkactions/scripts/bests.lua)

    [9:57:14.285] [Error - CreatureEvent::configureEvent] No valid type for creature event.modalwindow
    [9:57:14.285] [Warning - BaseEvents::loadFromXml] Cannot configure an event




    i dont know how fix it. my server is 8.60
     
  10. Xeraphus

    Xeraphus Support Team Support Team Premium User

    Joined:
    Feb 14, 2015
    Messages:
    2,093
    Likes Received:
    791
    Best Answers:
    65
    you can't use modalwindows for 8.6, they were implemented in 9.6+
     
  11. adahum

    adahum New Member

    Joined:
    Feb 28, 2015
    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    there's no one version to 8.60?nothing? :( thats so bad.
     
  12. Xeraphus

    Xeraphus Support Team Support Team Premium User

    Joined:
    Feb 14, 2015
    Messages:
    2,093
    Likes Received:
    791
    Best Answers:
    65
    that's what i just said

    you could use otclient but i have no idea how to implement modalwindows for it
    would probably just be easier to move to a higher client version
     
    adahum likes this.

Share This Page

Loading...