Raised This Month: $261 Target: $400
 65% 

Notices

[INC] Menu Targeting


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
hmmmmm
Senior Member
Join Date: Mar 2017
Location: ...
Old 04-20-2018 , 04:24   [INC] Menu Targeting
Reply With Quote #1

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.
Attached Files
File Type: inc menu_targeting.inc (2.1 KB, 44 views)

Last edited by hmmmmm; 04-20-2018 at 04:28. Reason: Actually add the include :D
hmmmmm is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 04-21-2018 , 05:35   Re: [INC] Menu Targeting
Reply With Quote #2

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;

__________________
Am I back? Well, we'll see.

Last edited by Powerlord; 04-21-2018 at 05:57.
Powerlord is offline
hmmmmm
Senior Member
Join Date: Mar 2017
Location: ...
Old 04-21-2018 , 07:09   Re: [INC] Menu Targeting
Reply With Quote #3

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.

Last edited by hmmmmm; 04-21-2018 at 07:19.
hmmmmm is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 04-21-2018 , 14:07   Re: [INC] Menu Targeting
Reply With Quote #4

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.
__________________

Last edited by ddhoward; 04-21-2018 at 14:10.
ddhoward 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 23:09.


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