View Single Post
Author Message
ThatKidWhoGames
Veteran Member
Join Date: Jun 2013
Location: IsValidClient()
Old 09-23-2018 , 12:49   Menu.AddItem Formatting
Reply With Quote #1

Have you ever been annoyed like I have at the fact that when you are adding items to a menu, you can't format what is displayed?

What I mean by this is when you do something like:
PHP Code:
menu.AddItem("example""Name: Client's name"); 
You can't actually format the second string. This fixes that problem!

I present to you... Menu.AddItemFormat!
PHP Code:
// Appends a new item to the end of a menu.
//
// @param info          Item information string.
// @param display       Default item display string.
// @param style         Drawing style flags.  Anything other than DEFAULT or 
//                      DISABLED will be completely ignored when paginating.
// @param ...           Formatting parameters for the display string.
// @return              True on success, false on failure.
// @error               Item limit reached.
public bool AddItemFormat(const char[] info, const char[] displayint style=ITEMDRAW_DEFAULTany ...)
{
    
int length strlen(display) + 255;
    
char[] buffer = new char[length];
    
VFormat(bufferlengthdisplay5);
    return 
this.AddItem(infobufferstyle);

Simply just add this code into the methodmap portion for menus in the menus.inc include file!

Here is an example of this being used in a plugin:
PHP Code:
#include <sourcemod>

public void OnPluginStart()
{
    
RegConsoleCmd("sm_menutest"Command_Callback);
}

public 
Action Command_Callback(int clientint args)
{
    
Menu menu = new Menu(Menu_Handler);
    
menu.SetTitle("Menu Format Test");
    
menu.AddItemFormat("test""Hey guys, my name is %N!"_client);
    
menu.Display(clientMENU_TIME_FOREVER);
    return 
Plugin_Handled;
}

public 
int Menu_Handler(Menu menuMenuAction actionint param1int param2)
{
    if (
action == MenuAction_End)
        
delete menu;

    return 
0;

Enjoy!

EDIT: I just noticed that this function pretty much exists with this include https://forums.alliedmods.net/showthread.php?t=265325

Use this version if you prefer using methodmaps.

Last edited by ThatKidWhoGames; 01-27-2021 at 20:40.
ThatKidWhoGames is offline