AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   Menu.AddItem Formatting (https://forums.alliedmods.net/showthread.php?t=310869)

ThatKidWhoGames 09-23-2018 12:49

Menu.AddItem Formatting
 
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.

hmmmmm 09-24-2018 03:42

Re: Menu.AddItem Formatting
 
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.

ddhoward 09-24-2018 16:44

Re: Menu.AddItem Formatting
 
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.

Psyk0tik 09-24-2018 17:26

Re: Menu.AddItem Formatting
 
That seems like a good alternative to this:

PHP Code:

char sItem[32];
Format(sItemsizeof(sItem), "%N"client);
menu.AddItem("test"sItem); 


ThatKidWhoGames 09-24-2018 17:30

Re: Menu.AddItem Formatting
 
Quote:

Originally Posted by ddhoward (Post 2616635)
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!

Spirit_12 09-24-2018 18:31

Re: Menu.AddItem Formatting
 
Quote:

Originally Posted by Crasher_3637 (Post 2616639)
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?

Neuro Toxin 09-24-2018 19:41

Re: Menu.AddItem Formatting
 
Adding format functionality to the existing .AddItem native seems reasonable as it would be backwards compatible with existing plugins

ddhoward 09-24-2018 19:44

Re: Menu.AddItem Formatting
 
Quote:

Originally Posted by Spirit_12 (Post 2616650)
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.

ddhoward 09-24-2018 19:46

Re: Menu.AddItem Formatting
 
Quote:

Originally Posted by Neuro Toxin (Post 2616658)
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.

Powerlord 09-24-2018 19:55

Re: Menu.AddItem Formatting
 
Quote:

Originally Posted by ddhoward (Post 2616635)
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.


All times are GMT -4. The time now is 19:23.

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