AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   [INC] Menu Targeting (https://forums.alliedmods.net/showthread.php?t=306954)

hmmmmm 04-20-2018 04:24

[INC] Menu Targeting
 
1 Attachment(s)
I didn't like the behaviour of the stock SM function FindTarget so I made this instead. Issue with FindTarget is that when you type a name and there are multiple matches it does pretty much nothing (other than an error).

This include file defines a stock function SelectTarget that works similar to FindTarget, but opens a menu to the client with all the possible matches if there are more than 1.

Best way to explain this is through an example:
PHP Code:

#include <sourcemod>
#include <menu_targeting>

public void OnPluginStart()
{
    
RegConsoleCmd"sm_selecttarget"Command_SelectTarget );
}

public 
Action Command_SelectTargetint clientint args )
{
    
char target[MAX_NAME_LENGTH];
    
GetCmdArgStringtargetsizeof(target) );
    
    
// stock bool SelectTarget( int client, const char[] targetname, MenuTargetingCB cb )
    
if( !SelectTargetclienttargetPrintName ) )
    {
        
ReplyToCommandclient"[SM] No targets found ..." );
    }

    return 
Plugin_Handled;
}

public 
void PrintNameint clientint target )
{
    
PrintToChattarget"[SM] I wish lambdas existed in SourcePawn :(" );


If there were 3 players on the server named "Ta", "Tac" and "Taco" and the user input was "Ta", then a menu shows up to the client with those 3 possible matches and the callback is called for whichever is chosen.
If the user input was instead "Taco", there is only 1 match and the callback is called immediately.

Powerlord 04-21-2018 05:35

Re: [INC] Menu Targeting
 
Or, you could just use ProcessTargetString, which is what FindTarget calls internally.

Edit: For an example of how to use it:

PHP Code:

// Somewhere earlier on, such as OnPluginStart
    
LoadTranslations("common.phrases");

public 
Action Command_SelectTargetint clientint args )
{
    
// Note: We changed this char length; this is defined in commandfilters.inc
    
char target[MAX_TARGET_LENGTH];
    
GetCmdArgStringtargetsizeof(target) );
    
    
int displayTo[MaxClients];

    
// Since this is intended to display a menu:
    // Filters say to ignore admin immunity levels and also not select bots
    
int count ProcessTargetString(targetclientdisplayTosizeof(displayTo), COMMAND_FILTER_NO_IMMUNITY|COMMAND_FILTER_NO_BOTS);
    if (
count 1)
    {
        
// This handles "target not found" and the like
        
ReplyToTargetError(clientcount);
        return 
Plugin_Handled;
    }

    
// displayTo[0] through displayTo[count-1] now contain client indexes of all matching clients that you can iterate through using a for loop
    
for (int i 0counti++)
    {
        
// Do something with displayTo[i]
        
PrintToChatdisplayTo[i], "[SM] This is boring and uses a for loop instead of a callback :(" );
    }
    return 
Plugin_Handled;



hmmmmm 04-21-2018 07:09

Re: [INC] Menu Targeting
 
The loop would apply to all clients that match the target name instead of just the 1. The whole point is you're selecting 1 player through a menu (if there are multiple matches).
You need a callback if the client is gonna be a selecting that player through a menu at a later point.

You could use ProcessTargetString in the stock function itself and wrap it with the menu/callback stuff but I had issues with it before in terms of bot/self selection so I do the StrContains with all clients instead.

ddhoward 04-21-2018 14:07

Re: [INC] Menu Targeting
 
ProcessTargetString doesn't even support multiple targets via display name. It only populates the array with multiple targets if a multi target filter was used, like @all.

If multiple targets match a partial name, then ProcessTargetString returns COMMAND_TARGET_AMBIGUOUS.

Psyk0tik 05-30-2018 04:16

Re: [INC] Menu Targeting
 
Thank you for making this. :bacon!::bacon!::bacon!:


All times are GMT -4. The time now is 18:22.

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