SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 16/09/2018, 02:53 PM   #1
NealPeteros
Gangsta
 
NealPeteros's Avatar
 
Join Date: Dec 2015
Location: GetPlayerCountry(playerid);
Posts: 638
Reputation: 51
Default Optional string (sscanf)

So I wanted to create a /mute command wherein the admin who uses the command may or may not input a reason as to why he'd mute the player. Here's where I got so far

PHP Code:
CMD:mute(playeridparams[])
{
    new 
idreason[64], time;
    if (
sscanf(params"dS[24]d"idreasontime))
    {
        
SendClientMessage(playerid, -1"USAGE: /mute [id] [reason (optional)] [minutes]");
        return 
1;
    }
    
Mute(idreasontime);
    return 
1;

Thing is that when I use it, say for example, like this

Code:
/mute 1 20
/mute - the command

1 - the ID

20 - the minutes

it sends me the

Code:
SendClientMessage(playerid, -1, "USAGE: /mute [id] [reason (optional)] [minutes]");
I wanted the reason to be optional. How u do that?


Oh and it sends me a
Code:
sscanf warning: No default value found.
__________________
Quote:
SA-MP is like a drug or alcohol or smoking.. you really get used to it and can not leave it.
NealPeteros is offline   Reply With Quote
Old 16/09/2018, 04:14 PM   #2
AlamoTR
Little Clucker
 
Join Date: Jul 2016
Posts: 16
Reputation: 14
Default Re: Optional string (sscanf)

Code:
CMD:mute(playerid, params[])
{
    new id, reason[64], time;
    if (sscanf(params, "dS(No Reason Given)[24]d", id, reason, time))
    {
        SendClientMessage(playerid, -1, "USAGE: /mute [id] [reason (optional)] [minutes]");
        return 1;
    }
    Mute(id, reason, time);
    return 1;
}
AlamoTR is offline   Reply With Quote
Old 16/09/2018, 04:34 PM   #3
Y_Less
Beta Tester
 
Y_Less's Avatar
 
Join Date: Jun 2008
Location: 629 - git.io/Y
Posts: 15,127
Reputation: 3170
Default Re: Optional string (sscanf)

Optional parameters must come last. "20" is a perfectly valid string (everything is a valid string), so sscanf sees you want a string, sees some text, and puts them together. Just do `id time [reason]` instead.
Y_Less is offline   Reply With Quote
Old 16/09/2018, 04:56 PM   #4
Undef1ned
Big Clucker
 
Undef1ned's Avatar
 
Join Date: Aug 2018
Location: Venezuela
Posts: 189
Reputation: 12
Default Re: Optional string (sscanf)

PHP Code:
CMD:mute(playeridparams[]) 

    new 
idtimereason[64]; 
    if(!
sscanf(params"uds[64]"idtimereason)) 
    { 
        if(!
IsPlayerConnected(id)) return SendClientMessage(playerid, -1"disconnected");
        
        
Mute(idreasontime);
        return 
1
    }
    
    if(
sscanf(params"ud"params[0], params[1])) return SendClientMessage(playerid, -1"USAGE: /mute [id] [minutes] [reason (optional)]");
    
Mute(params[0], ""params[1]); //I recommend using the code that is in "Mute" here.
    
return 1

Undef1ned is offline   Reply With Quote
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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Optional specifiers - SSCANF CrashCar Scripting Help 3 18/04/2015 12:29 AM
Issues with optional string on sscanf? Dokins Scripting Help 4 16/03/2015 11:21 PM
Optional sscanf parameters MP2 Scripting Help 5 20/10/2012 11:48 PM
Making SSCANF optional emokidx Help Archive 3 10/08/2011 06:56 AM
Optional string. CaHbKo Help Archive 2 20/08/2010 07:43 PM


All times are GMT. The time now is 06:10 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.