Menus
FivePD provides a server-side API for creating custom in-game menus. This allows you to build user interfaces for your addon's features directly from your server-side C# code.
Menus are created on a per-player basis when they join the server. You can inject any dependency into your menu class's constructor, just as you would with Extensions or Callouts.
1. Creating a Menu
To create a menu, you need to define a class that inherits from the Menu base class.
using System.Collections.Generic;
using System.Threading.Tasks;
using FivePD.Server.UIModels.MenuModels;
namespace MyAwesomeAddon;
public class MyMenu : Menu
{
public MyMenu()
: base("author::myAwesomeAddon::myMenu", "My Addon Menu", "B", false)
{
}
protected override List<MenuItem> CreateItems()
{
var items = new List<MenuItem>();
// A simple button
var doSomethingButton = new MenuButtonItem("Do Something", "This button performs an action.");
doSomethingButton.OnSelect += player =>
{
// Your logic here
return Task.CompletedTask;
};
items.Add(doSomethingButton);
// A list of selectable items
var selectSomething = new MenuListItem(
"Select Item",
"This item lets you select from a list.",
new List<string> { "Item 1", "Item 2" });
selectSomething.OnSelect += (player, selectedIndex) =>
{
// Your logic here, using the selectedIndex
return Task.CompletedTask;
};
items.Add(selectSomething);
// A toggleable checkbox
var toggleSomething = new MenuCheckboxItem(
"Toggle Something",
"This item can be checked or unchecked.",
false); // Default state is unchecked
toggleSomething.OnSelect += (player, isChecked) =>
{
// Your logic here, using the isChecked state
return Task.CompletedTask;
};
items.Add(toggleSomething);
return items;
}
}Menu Constructor
The Menu base class constructor takes the following parameters:
id(string): A unique identifier for your menu. We recommend the formatauthor::addon::menuName.title(string): The title displayed at the top of the menu.key(string): The keyboard key used to open the menu (e.g., "F5", "B").
2. Registering the Menu
To make your menu available in-game, you must register it using the IMenuService. The best place to do this is in your IExtension's constructor.
using System.Threading.Tasks;
using FivePD.Server.API;
using FivePD.Server.Interfaces;
namespace MyAwesomeAddon;
public class MyExtension : IExtension
{
public MyExtension(IMenuService menuService)
{
menuService.Register<MyMenu>();
}
public Task OnStarted()
{
return Task.CompletedTask;
}
public Task OnStopped()
{
return Task.CompletedTask;
}
}3. Accessing a Player's Menu Instance
You can get a specific player's instance of a menu to interact with it dynamically (e.g., update its items or properties). Use the GetMenu<T>() method on an FPlayer object.
// This example assumes you have an 'FPlayer' object named 'player'
var myMenuInstance = player.GetMenu<MyMenu>();