How can i make a cmd with optional options?
#1

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?
Reply
#2

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

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)
Reply
#4

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
Reply
#5

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;

Reply
#6

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;

Reply
#7

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

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!
Reply
#9

You've got to be kidding me...
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)