SA-MP Forums Archive
optional parameters with sscanf - 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: optional parameters with sscanf (/showthread.php?tid=514598)



optional parameters with sscanf - jamjamnewbie - 22.05.2014

how to do optional parameters in sscanf?

like typing /kick will send "Usage: /kick [id] [reason]"

but typing /kick 1 will be successful even without reason


Re: optional parameters with sscanf - rappy93 - 22.05.2014

pawn Код:
if(sscanf(params,"d", playerid)) return SendClientMessage(playerid, "/kick [target id]");
Think this might do it.


Re: optional parameters with sscanf - AndySedeyn - 22.05.2014

Quote:
Originally Posted by rappy93
Посмотреть сообщение
pawn Код:
if(sscanf(params,"d", playerid)) return SendClientMessage(playerid, "/kick [target id]");
Think this might do it.
This can help.
If you want to add a reason to it use this:

pawn Код:
if(sscanf(params, "us[128]", tID, reason)) return SendClientMessage(playerid, /*COLOR HERE*/, "* [USAGE]: /kick [playerid/PartOfName] [Reason]");
I replaced the "d" with "u" so you can type a part of the players name or his ID.
"s" stands for string, that means that it's a message, in this case: your reason.
"[128]", behind the "s" is the maximum length your reason can have.

If you want to make your reason optional, use this:

pawn Код:
if(sscanf(params, "uS(No Reason)[128]", tID, reason)) return SendClientMessage(playerid, /*COLOR HERE*/, "* [USAGE]: /kick [playerid/PartOfName] [Reason]");
Make sure you have a local variable. Example:
pawn Код:
new reason[128];
Edit:
I forgot to mention that "tID" is the targetid, you have to make a local variable for that one too.
pawn Код:
new tID;



Re: optional parameters with sscanf - jamjamnewbie - 22.05.2014

does this work?
Код:
CMD:kick(playerid, params[]) {
     new targetid, reason[128];
     if(sscanf(params, "u", targetid)) return SendClientMessage(playerid, COLOR_HERE, "Usage: /kick [id] [reason]");
     else if(!sscanf(params, "u", targetid)) {
          //rest of code here
          SendClientMessageToAll(COLOR_HERE, "BLABLABLA has been kicked <> Reason: No reason specified");
     } else if(!sscanf(params, "us[128]", targetid, reason)) {
          //rest of code here
          SendClientMessageToAll(COLOR_HERE, "BLABLABLA has been kicked <> Reason: the reason here");
     }
     return 1;



Re: optional parameters with sscanf - AndySedeyn - 22.05.2014

This is an example of how it should be done:

pawn Код:
CMD:kick(playerid, params[])
{
    new targetid, reason[128], string[128], aname[MAX_PLAYER_NAME];
    if(sscanf(params, "uS(No Reason)[128]", targetid, reason)) return SendClientMessage(playerid, 0xAFAFAFAA, "Usage: /kick [id] [reason]");
    else
    {
        GetPlayerName(playerid, aname, sizeof(aname));
        //rest of code
        format(string, sizeof(string), "* Administrator %s has kicked %s, reason: %s", aname, targetid, reason);
        SendClientMessageToAll(COLOR_HERE, string)
    }
    return 1;
}



Re: optional parameters with sscanf - jamjamnewbie - 22.05.2014

does the "S(no reason)" in sscanf have to be capital? and also i have to include that part within the parenthesis?


Re: optional parameters with sscanf - Konstantinos - 22.05.2014

Use optional string as Bible showed you. You also don't need to check anything because "reason" array stores the text whether the input was given or not.
pawn Код:
CMD:kick(playerid, params[])
{
    new targetid, reason[64];
    if (sscanf(params, "rS(No reason specified)[64]", targetid, reason)) return SendClientMessage(playerid, COLOR_HERE, "Usage: /kick [id] [reason]");
    //rest of code here
    new msg[144];
    format(msg, sizeof (msg), "%s has been kicked <> Reason: %s", ..., reason);
    // ... is the argument for the name, modify it accoding to the code you have..
    SendClientMessageToAll(COLOR_HERE, msg);
    return 1;
}
EDIT: Didn't refresh the page.

--

Yes, the text should be inside parentheses and it doesn't matter about the letter case (lower/upper).

Documentation for optional strings: https://github.com/Y-Less/sscanf/wik...tional-strings


Re: optional parameters with sscanf - AndySedeyn - 22.05.2014

Quote:
Originally Posted by jamjamnewbie
Посмотреть сообщение
does the "S(no reason)" in sscanf have to be capital? and also i have to include that part within the parenthesis?
The capital "S" indicates that it's optional. "(No reason)" is the message that should be shown if there is no reason specified by the player.

EDIT:

Konstantinos gave you a great explanation why you should use the text between the parentheses.