Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- This is a universal utility script, which allows you to easily create multi-page menus and assign any functions to their buttons. Menus can be created both out of the 'menus' table and dynamically, to allow for changing variables. The documentation is quite extensive and exhaustive, so it should answer most of the how-to questions.
- [more=Documentation]
- :>: A menu table - be it in the menus table or dynamic - must have a title item that determines the title, an items table which determines the menu items and which has tables for each button of the following format in it:
- [code]
- {ButtonName, ButtonDescription, ButtonData, ButtonFunction}
- [/code]
- ButtonName and ButtonDescription are usually strings while ButtonData is usually a table. But they can also be functions that return specific values.
- [more=Documentation on Button Functions]
- This only applies if you make it a function and not a string
- [b]ButtonName[/b]
- Arguments: playerID [number], buttonData [table]
- Return: must return a string
- [b]ButtonDescription[/b]
- Arguments: playerID [number], buttonData [table]
- Return: must return a string
- [b]ButtonData[/b]
- Arguments: playerID [number]
- Return: must return a table containing the data
- [b]ButtonFunction[/b]
- Arguments: playerID [number], buttonData [table], menuTitle [string], menuButton [number]
- Return: does not matter
- [u]Note:[/u] menuTitle and menuButton are passed directly from cs2dhook:menu
- The ButtonName, -Description and -Data functions are called everytime when a menu is constructed.
- If you use addButton to initialize the functions above (initButtonName and other init* arguments), then playerID equals 0!
- Additionally, ButtonData is called only once and before ButtonName and ButtonDescription functions.
- [/more]
- UniMenu's internal menu table looks like this:
- [code]
- {
- title = "Test Title@b",
- items = {
- {"Test Button", "Test Description", {}, function (id) msg2(id,"Test successful!") end}
- }
- }
- [/code]
- :>: Menus are opened by calling the [b]unimenu[/b] function. It has the following arguments:
- :o: [i]id[/i] - player ID to open the menu to. [i]number[/i]
- :o: [i]construct[/i] - construct the menu anew or use the cached version? [i]boolean[/i]
- :o: [i]targetMenu[/i] - menu to show. [i]table/number/string[/i]
- :o: [i]page[/i] - page of the menu to show. [i]number[/i]
- The most versatile and important argument is [b]targetMenu[/b]:
- :o: [i]is a Table[/i] - This table must be a correctly constructed menu table, like other menus in [i]unimenu_menus[/i]
- :o: [i]is a Number or String[/i] - a menus table index to open a menu out of that (this menu must exist under unimenu_menus[ targetMenu ])
- :o: [i]equals "current"[/i] - Opens the currently opened menu
- If you have a menu that is extensive but does not change, specify false as construct, so the script wouldn't construct a menu anew, but open the cached page.
- :>: How to make a menu
- There are two methods. There're examples for both of them in unimenu_menus.lua
- 1) You can change the unimenu_menus table directly in the script. But this is slow and produces messy code. Not recommended
- 2) With the new method, you call the according functions from anywhere in the code
- [more=New method explained]
- You must first create a menu:
- [code]unimenu_newMenu("my_menu", "My Lovely Menu")[/code]
- With this, we created an empty menu without buttons. To open this menu you have to execute the unimenu functions with parameters unimenu(id, true, "my_menu", 1)
- Now, we'll add a button to it:
- [code]
- local showMessage = function (id, data)
- msg("©100100100This is a gray message from UniMenu!")
- end
- unimenu_addButton("my_menu", "Show message", "Color: Gray", {}, showMessage)
- [/code]
- If you now click on this menu button, a gray message will show up. That's all!
- [/more]
- [more=Documentation on newMenu and addButton]
- [b]unimenu_newMenu(umid, title, items)[/b]
- :o: [i]umid[/i] - Internal identifier, which you will need to open that menu. If you don't specify it, you have to save the returned value and use it instead
- :o: [i]title[/i] - Menu's title, may end with @b and @i
- :o: [i]items[/i] - (optional) put the ready UniMenu-compatible table with buttons here
- :o: [i]return value[/i] - If argument umid was specified then it equals umid, if umid is nil then the return is a number value
- [b]unimenu_addButton(umid, buttonName, buttonDesc, data, func, initButtonName, initButtonDesc, initData)[/b]
- :o: [i]umid[/i] - Internal identifier, which you need to open a menu.
- :o: [i]buttonName[/i] - (string or function) button text on the left side
- :o: [i]buttonDesc[/i] - (string or function) button description, grey text on the right side
- :o: [i]data[/i] - (table or nil) This table will be passed as an argument to the func function
- :o: [i]func[/i] - function which will be called when the button is pressed
- :o: [i]initButtonName[/i] - if TRUE, call the function buttonName with id=0 and data and save the returned string value to menu's buttonName
- :o: [i]initButtonDesc[/i] - same as initButtonName
- :o: [i]initButtonData[/i] - same as initButtonName. Note that this is called before buttonName and ButtonDesc are initialized, so both bName and bDesc are going to have the same data to operate with
- [/more]
- [more=Other variables include:]
- :o: unimenu_lastOpenedMenu [table] - a table containing the last opened menu of each player (1 layer - pmenu[id])
- :o: unimenu_menuPageStrings [table] - a table containing actual menu strings for each page of the menu opened by a player (2 layers - spages[id][page])
- :o: unimenu_tempData [table] - a table containing cached results for each button and player if buttonData of a menu is a function
- [/more]
- [/more]
- [more=Changelog:]
- [b]v2.1[/b]
- [ADDED] buttonData can also be a function now (called whenever the menu is constructed)
- ~ see [url=https://github.com/VADemon/cs2d-script/blob/cd129fa31c3e333ddae064273c0b0c9f80184ccb/unimenu_menus.lua#L155]this for an example[/url]
- [ADDED] unimenu_addButton can initialize functions instantly, saving returned string values to the menu table (to prevent tons of function calls during menu construction)
- [ADDED] Leave hook to delete temporary data of old players
- [b]v2.0[/b]
- [ADDED] You can now specify menu mode (nothing or "@b" or "@i")
- [ADDED] Button name and Button description can be both string or a function returning a string
- [ADDED] [i]data[/i] variable which contains additional custom data you can use in any of the menu functions
- [ADDED] Arguments [i]menu[/i] and [i]sel[/i] (from CS2D menu hook, menu = title, sel = pressed button) are passed to the onClick function
- [ADDED] [i]unimenu_newMenu[/i] and [i]unimenu_addButtion[/i] functions for easier menu creation
- [CHANGED] You can't use the old UniMenu menu tables anymore, the syntax of those changed slightly. If you want to update, add [b],{} [/b] after the button description string
- + Code refactoring[/more]
- Leave feedback and [url=https://github.com/VADemon/cs2d-script/issues/new]report bugs[/url], should any be found.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement