{"Name":"CustomMenuTutorial","Description":"Custom menu tutorial","LoadPriority":1,"Scripts":[{"Path":"ui/custom_menu.nut","RunOn":"UI","UICallback":{"Before":"AddCustomMenu"}}]}
Then create custom_menu.nut in ./mod/scripts/vscripts/ui.
AddCustomMenu will get called when the UI vm is initializing and instantiate your menu. You can access your menu with GetMenu("CustomMenu") after it has been initialized.
Next, create the file that defines the layout of your menu. It’s already referenced in the above code at $"resource/ui/menus/custommenu.menu". Create the file ./mod/resource/ui/menus/custommenu.menu and paste this code in it.
.menu configuration
resource/ui/menus/custommenu.menu{menu{ControlNameFramexpos0ypos0zpos3widef0tallf0autoResize0visible1enabled1pinCorner0PaintBackgroundType0infocus_bgcolor_override"0 0 0 0"outoffocus_bgcolor_override"0 0 0 0"Vignette// Darkened frame edges{ControlNameImagePanelInheritPropertiesMenuVignette}Title// The title of this menu{ControlNameLabelInheritPropertiesMenuTitlelabelText"#CUSTOM_MENU_TITLE"}//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Content/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////SomeLabel// A label that is placed in the middle of the screen{ControlNameLabellabelText"Some Label"auto_wide_tocontents1// Set width automatically relative to the label contentxpos%50ypos%50}SomeButton// A button that is placed directly beneath the label{ControlNameRuiButtonInheritPropertiesRuiSmallButtontall50wide250labelText"Some Button"textAlignmentcenterpin_to_siblingSomeLabelpin_corner_to_siblingTOPpin_to_sibling_cornerBOTTOM}//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Footer/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////FooterButtons// Allow adding footers to this menu{ControlNameCNestedPanelInheritPropertiesFooterButtons}}}
Now you’ll need to define CustomMenu_Init. This is the function previously defined that contains all initializations needed for this menu.
First, create an instantiated struct for variables that should be available in the scope of your custom menu script.
struct{varmenu}file
At the moment, this struct can only contain your menu. To set it, edit AddCustomMenu like this:
We need a new callback that’s run after all menus are initialized to add any footers to them. Create the global function AddCustomMenuFooter in custom_menu.nut like this:
voidfunctionAddCustomMenuFooter(){AddMenuFooterOption(GetMenu("MainMenu"),// Get the main menu. We want to add a footer to this one. Change this if you want to add a footer to another menuBUTTON_X,// This sets the gamepad button that will trigger the click callback defined laterPrependControllerPrompts(BUTTON_X," Custom Menu"),// This is the text that will show as the label of the footer if a gamepad is used"Custom Menu",// This is the label text of the footer if no gamepad is usedvoidfunction(varbutton)// This is the click callback.{/* This is an anonymous function. It will be run every time the footer is pressed. */AdvanceMenu(file.menu)})}
We’ll use the button we defined earlier in the .menu file to increase a number of clicks and the label to show how often the user has clicked that button.
first, add someLabel and clicks to the file struct. Then define the label in the AddCustomMenu and add a callback to the button.
struct {
var menu
+ var someLabel+ int clicks} file
void function AddCustomMenu()
{
AddMenu( "CustomMenu", $"resource/ui/menus/custommenu.menu", CustomMenu_Init )
file.menu = GetMenu( "CustomMenu" )
+ file.someLabel = Hud_GetChild( file.menu, "SomeLabel" )+ var someButton = Hud_GetChild( file.menu, "SomeButton" )+ Hud_AddEventHandler( someButton, UIE_CLICK, OnSomeButtonClick )}
Now you need to define the OnSomeButtonClick callback that’s triggered when the button is activated.
voidfunctionOnSomeButtonClick(varbutton){file.clicks++Hud_SetText(file.someLabel,format("clicked the button %i times",file.clicks))}
Then add a UIE_CLICK callback for the button. It also makes sense to move the code that updates the label text to it’s own function so it can be reused by the reset button.
void function AddCustomMenu()
{
AddMenu( "CustomMenu", $"resource/ui/menus/custommenu.menu", CustomMenu_Init )
file.menu = GetMenu( "CustomMenu" )
file.someLabel = Hud_GetChild( file.menu, "SomeLabel" )
var someButton = Hud_GetChild( file.menu, "SomeButton" )
+ var resetButton = Hud_GetChild( file.menu, "ResetButton" ) Hud_AddEventHandler( someButton, UIE_CLICK, OnSomeButtonClick )
+ Hud_AddEventHandler( resetButton, UIE_CLICK, OnResetButtonClick )}
void function OnSomeButtonClick( var button )
{
file.clicks++
- Hud_SetText( file.someLabel, format( "clicked the button %i times", file.clicks ) )+ UpdateClickLabel()}
void function OnResetButtonClick( var button )
{
file.clicks = 0
+ UpdateClickLabel()}
+void function UpdateClickLabel()+{+ Hud_SetText( file.someLabel, format( "clicked the button %i times", file.clicks ) )+}