Raised This Month: $12 Target: $400
 3% 

Menu.AddItem Formatting


Post New Thread Reply   
 
Thread Tools Display Modes
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
hmmmmm
Great Tester of Whatever
Join Date: Mar 2017
Location: ...
Old 09-24-2018 , 03:42   Re: Menu.AddItem Formatting
Reply With Quote #2

Modifying menus.inc, or any existing sourcemod include, sounds like a bad idea. Instead I'd consider making your own MethodMap that inherits from Menu and adds the function you want in your own custom include.
hmmmmm is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 09-24-2018 , 16:44   Re: Menu.AddItem Formatting
Reply With Quote #3

Maybe Menu's various natives could be made into format-class functions in stock Sourcemod? I'll poke some people on the IRC channel to see if this is feasible.
__________________
ddhoward is offline
Psyk0tik
Veteran Member
Join Date: May 2012
Location: Homeless
Old 09-24-2018 , 17:26   Re: Menu.AddItem Formatting
Reply With Quote #4

That seems like a good alternative to this:

PHP Code:
char sItem[32];
Format(sItemsizeof(sItem), "%N"client);
menu.AddItem("test"sItem); 
__________________
Psyk0tik is offline
ThatKidWhoGames
Veteran Member
Join Date: Jun 2013
Location: IsValidClient()
Old 09-24-2018 , 17:30   Re: Menu.AddItem Formatting
Reply With Quote #5

Quote:
Originally Posted by ddhoward View Post
Maybe Menu's various natives could be made into format-class functions in stock Sourcemod? I'll poke some people on the IRC channel to see if this is feasible.
That would be great, thanks!
ThatKidWhoGames is offline
Spirit_12
Veteran Member
Join Date: Dec 2012
Location: Toronto, CA
Old 09-24-2018 , 18:31   Re: Menu.AddItem Formatting
Reply With Quote #6

Quote:
Originally Posted by Crasher_3637 View Post
That seems like a good alternative to this:

PHP Code:
char sItem[32];
Format(sItemsizeof(sItem), "%N"client);
menu.AddItem("test"sItem); 
I'm with Crasher_3637 on this one. Why create a whole new function when Format can easily solve the issue?
__________________
Spirit_12 is offline
Neuro Toxin
Veteran Member
Join Date: Oct 2013
Location: { closing the void; }
Old 09-24-2018 , 19:41   Re: Menu.AddItem Formatting
Reply With Quote #7

Adding format functionality to the existing .AddItem native seems reasonable as it would be backwards compatible with existing plugins
__________________
Neuro Toxin is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 09-24-2018 , 19:44   Re: Menu.AddItem Formatting
Reply With Quote #8

Quote:
Originally Posted by Spirit_12 View Post
I'm with Crasher_3637 on this one. Why create a whole new function when Format can easily solve the issue?
You say you're with him, but then you immediately disagree with him? He claimed that the snippet in the OP was a good alternative to a Format call.

Format-class functions exist across Sourcemod. From the various PrintTo*() functions, KickClient, ServerCommand, ClientCommand, ShowActivity, BuildPath, the logging functions, etc are all examples of this. We combine the formatting into the function for convenience.

There is an ongoing discussion in the Sourcemod Discord/IRC server about this, and the potential issues.
__________________

Last edited by ddhoward; 09-24-2018 at 19:45.
ddhoward is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 09-24-2018 , 19:46   Re: Menu.AddItem Formatting
Reply With Quote #9

Quote:
Originally Posted by Neuro Toxin View Post
Adding format functionality to the existing .AddItem native seems reasonable as it would be backwards compatible with existing plugins
Not fully compatible. If 100% of the behavior from other format-class functions were to be adopted, then older plugins that include % symbols in the text would see that symbol wiped out, for example.
__________________

Last edited by ddhoward; 09-24-2018 at 19:47.
ddhoward is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 09-24-2018 , 19:55   Re: Menu.AddItem Formatting
Reply With Quote #10

Quote:
Originally Posted by ddhoward View Post
Maybe Menu's various natives could be made into format-class functions in stock Sourcemod? I'll poke some people on the IRC channel to see if this is feasible.
I always assumed they aren't there to encourage you to use the Menu callback with MenuAction_Display (with SetPanelTitle) and MenuAction_DisplayItem (with RedrawMenuItem) to translate the menu text on a per-user basis.
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 04:31.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode