SA-MP Forums Archive
How can i make a cmd with optional options? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: How can i make a cmd with optional options? (/showthread.php?tid=662182)



How can i make a cmd with optional options? - aKnoxx - 25.12.2018

How can I make a command with optional options? Like...

/kill <targetid> (optional)

If you type "/kill" it sets your hp to 0

If you type "/kill 4" it sets id 4's hp to 0

Is this possible without making it super complicated?


Re: How can i make a cmd with optional options? - Mike861 - 25.12.2018

Use sscanf.
https://sampforum.blast.hk/showthread.php?tid=570927
Example:
Код:
if(sscanf(params,"i",id))



Re: How can i make a cmd with optional options? - ItsRobinson - 25.12.2018

PHP код:
new giveplayeridfloat:health;
if(
sscanf(params"uF"giveplayeridhealth))
{
    
SetPlayerHealth(giveplayeridhealth);

Using sscanf you format the input, your first exception is your input, then it's your format and then it's the variables you store to.

If we wanted to make it so that you HAD TO enter a health amount, the format would be "uf" for User and Float, however, because the amount is optional, it is "uF", capitalising the letter tells it that it's an optional check. (never made an optional float before, but I'm presuming it works the same as an integer :P)


Re: How can i make a cmd with optional options? - Logic_ - 25.12.2018

PHP код:
CMD:kill(playeridparams[]) {
    
    new
        
lookupid;
    if (
sscanf(params"U"lookupid)) {
        
SendClientMessage(playerid, -1"USAGE: You can use \"/kill [playerid]\"");
        
SetPlayerHealth(playerid0.0);
    }
    else {
        if (!
IsPlayerConnected(lookupid)) {
            return 
SendClientMessage(playerid, -1"ERROR: The specified player is not connected.");
        }
        
SetPlayerHealth(lookupid0.0);
    }
    return 
1;

You can find the list of supported specifiers for sscanf here: https://sampforum.blast.hk/showthread.php?tid=570927


Re: How can i make a cmd with optional options? - aKnoxx - 26.12.2018

I tried this...
It wont work. When I type /kill 0 it kills id 0, but when I type /kill nothing happens. Same with Logic_'s script too, it just works with /kill (id)

PHP код:
CMD:kill(playeridparams[])
{
    new 
targetid;
    if(
sscanf(params"U"targetid)) {
        return 
SetPlayerHealth(playerid0);
    }
    
    
SetPlayerHealth(targetid0);
    return 
1;




Re: How can i make a cmd with optional options? - Threshold - 26.12.2018

That's because the sscanf statement is using an optional parameter, and sscanf will never return true if you're only using an optional parameter in this instance.

There are multiple ways to do this but they're all essentially the same.
Examples:
PHP код:
CMD:kill(playeridparams[])
{    
    new 
lookupid INVALID_PLAYER_ID;
    if(
sscanf(params"u"lookupid)) {
        
SendClientMessage(playerid, -1"USAGE: You can use \"/kill [playerid]\"");
        
SetPlayerHealth(playerid0.0);
        return 
1;
    }
    if(!
IsPlayerConnected(lookupid)) {
        
SendClientMessage(playerid, -1"ERROR: The specified player is not connected.");
        return 
1;
    }
    
SetPlayerHealth(lookupid0.0);
    return 
1;

PHP код:
CMD:kill(playeridparams[])
{
    new 
lookupid = -1;
    
sscanf(params"U(-1)"lookupid);
    if(
lookupid == -1)
    {
        
SendClientMessage(playerid, -1"USAGE: You can use \"/kill [playerid]\"");
        
SetPlayerHealth(playerid0.0);
    }
    else if(!
IsPlayerConnected(lookupid))
        
SendClientMessage(playerid, -1"ERROR: The specified player is not connected.");
    else
        
SetPlayerHealth(lookupid0.0);
    return 
1;




Re: How can i make a cmd with optional options? - OKStyle - 26.12.2018

pawn Код:
CMD:kill(playerid, params[])
{
    if(sscanf(params, "d", params[0])) return SetPlayerHealth(playerid, 0.0);
    SetPlayerHealth(params[0], 0.0);
    return 1;
}



Re: How can i make a cmd with optional options? - aKnoxx - 26.12.2018

Quote:
Originally Posted by OKStyle
Посмотреть сообщение
pawn Код:
CMD:kill(playerid, params[])
{
    if(sscanf(params, "d", params[0])) return SetPlayerHealth(playerid, 0.0);
    SetPlayerHealth(params[0], 0.0);
    return 1;
}
TYVM!


Re: How can i make a cmd with optional options? - Threshold - 29.12.2018

You've got to be kidding me...