sscanf2 and zcmd
#1

Most (Or maybe all) people use sscanf like this method:
pawn Code:
CMD:a(playerid,params[])
{
   new string[128];
   if(!sscanf(params,"s ",string)
   .............
But I feel like something confusing my mind. "params[]". I know it's the string after the command (right?) (f.e /command (params)).
I have some question regarding this:
1- What happen if I put number/size in params? (f.e params[128])
2- What if I do this (if(!sscanf(params,"s ",params)? Is it possible(I guess yes)? What is the advantage and disadvantage?
3- Is this method (if(!strcmp(string,"qwe"))) is faster than sscanf? Why? And how to convert this to sscanf?
Reply
#2

lol idk what will happen but i recommand you to leave it as it is now....
Reply
#3

pawn Code:
if(sscanf(params, "s", params[2]))
That is already the error line. Meaning if you don't enter something for the params (The string or whatever) then it will not let you continue
Reply
#4

Okay, let me show you something...

pawn Code:
CMD:something(playerid, params[])
{
        new id, value1, value2; // defining the variables for sscanf
    if(!IsPlayerAdmin(playerid))
        return false;
    if(sscanf(params, "udd", id, value1, value2))
        return // Send syntax message.

    // More stuff here...
    return 1;
}
You will notice that you do not need to add the exclamation point (!) before calling sscanf. Above would be the proper way to use sscanf.


In your given code example, using sscanf for such a thing is worthless. You could easily use the in-built isnull function which is inside the ZCMD include. An example:

pawn Code:
if(isnull(params)) // do something, the params are null (empty)

Okay, let's answer your questions:

1) That would most likely give you an error when you attempt to compile- it is pointless to change the length of the params variable.

2) Again, doing such would be pointless simply because you don't need to use sscanf there.

3) I believe strcmp is actually faster than using sscanf because it's already an in-built function. Also keep in mind that you cannot use the sscanf syntax with strcmp- it doesn't pick up on the differences between u, i/d, s, etc... When using strcmp it is to compare two strings, sscanf is used to extract data from a string.


I hope this has answered your questions. I would recommend reading the sscanf release thread before attempting to use it inside your mode.
Reply
#5

@willsuckformoney that's not what I mean.

@RealCop228:

I already read the topic from ages.

Quote:

You will notice that you do not need to add the exclamation point (!) before calling sscanf. Above would be the proper way to use sscanf.

AFAIK "!" isn't useless.
pawn Code:
if(sscanf(params,"s[129] ",string)) return SendClientMessage(playerid,xxx,"This message will sent if player isn't type any params/wrong params.");
if(!sscanf(params,"s[129] ",string)) return SendClientMessage(playerid,xxx,"This message will sent if player type any correct params.");
Quote:

In your given code example, using sscanf for such a thing is worthless. You could easily use the in-built isnull function which is inside the ZCMD include. An example:

I'm just sorting about a simple method.

Quote:

1) That would most likely give you an error when you attempt to compile- it is pointless to change the length of the params variable.

It didn't give me any error. I just don't know why there's no size in params (Actually Idk why every funtion don't have size like OnPlayerCommandText(playerid,text[])
Reply
#6

The reason there isn't a size for params is that it can't be determined at compilation time how long the string passed to the function will be, as a player could type an any 'params' into that command.

1. If you mean adding it into the function that you create the command with, then any command you type more than that many characters with won't be parsed/read correctly. The string will be cut off at some point, the max size that you set it to. If you are referring to the sscanf function, then I would assume it would only parse data after that cell in the string.

2. I believe that would work. However, in the situation you provided, it would be useless as the string is already in the params variable (there is no reason to use sscanf regardless of what param will hold if you have only one argument). I can't really see a downside of using params variable to store data you extracted from it in the first place, unless sscanf extracts the characters one-by-one. So you should be fine using it. Test it and see for yourself! (Note: You will need to always put string parsing at the end of the sscanf line unless there is some type of delimiter or a specified length, or else it won't be able to tell the start and end of one string from the other)

3. That isn't a answerable question as strcmp( ) function compares two strings, nothing more. sscanf( ) function parses (extracts) data from a given string. If you mean which method is faster - using OnPlayerCommandText callback or ZCMD - then ZCMD is faster, especially if you have many commands in your gamemode. Reason being, ZCMD calls a function directly to call the function rather than compare x (number of commands) strings to see if it's a valid string (inside the OnPlayerCommandText callback).

As far as the explanation mark, there's no difference. It depends on your style of coding to which method you use. See as both are the same:
pawn Code:
if( sscanf( params, "iii", x, y, z ) return SendClientMessage( playerid, 0xFFFFFFFF, "Invalid params" );

if( !sscanf( params, "iii", x, y, z )
{
   // code
}
else return SendClientMessage( playerid, 0xFFFFFFFF, "Invalid params" );
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)