sscanf2 + '? :' (optional integer)
#1

Hey all!
I've just started using sscanf2, as it has been a long time since last I scripted my own GM/FS.
I used to use 'z' for an optional string and convert it to an integer (strval) and then use it in a function.

And after sscanf 2.0, z has been replaced with capital specifiers.
So in my example command, how could I use ? : to insert my parameter/argument or w/e ?


pawn Код:
CMD:color(playerid, params[])
{
    new
        iColor,
        iColor2[ 3 ];

    new
        vehicleid = GetPlayerVehicleID( playerid );
    if( !vehicleid )
        return SendClientMessage( playerid, 0xFF0000FF, "**[ERROR] You have to be inside a vehicle to change its color." );
    if( GetPlayerState( playerid ) != PLAYER_STATE_DRIVER )
        return SendClientMessage( playerid, 0xFF0000FF, "**[ERROR] You don't have the permission to do this, only the driver can." );

    if( sscanf( params, "iz", iColor, iColor2 ))
        return UsageError( playerid, "/color [carcolor 1] [carcolor 2 <optional]" );

    if( strlen( iColor2 ) < 1 )
        ChangeVehicleColor( GetPlayerVehicleID( playerid ), iColor, iColor );
    else
        ChangeVehicleColor( GetPlayerVehicleID( playerid ), iColor, strval( iColor2 ));
    SendClientMessage( playerid, 0x00FF00FF, "**[SUCCESS] Carcolor changed successfully!" );
    return true;
}
That's my old CMD, and this is how I tried to do it (which obviously returned errors)

pawn Код:
CMD:color(playerid, params[])
{
    new
        iColor,
        iColor2,
        szColor2[ 5 ];

    new
        vehicleid = GetPlayerVehicleID( playerid );
    if( !vehicleid )
        return SendClientMessage( playerid, 0xFF0000FF, "**[ERROR] You have to be inside a vehicle to change its color." );
    if( GetPlayerState( playerid ) != PLAYER_STATE_DRIVER )
        return SendClientMessage( playerid, 0xFF0000FF, "**[ERROR] You don't have the permission to do this, only the driver can." );

    if( sscanf( params, "iI", iColor, iColor2 ))
        return UsageError( playerid, "/color [carcolor 1] [carcolor 2 <optional]" );

    valstr( szColor2, iColor2 );
    ChangeVehicleColor( GetPlayerVehicleID( playerid ), iColor, (( isnull( szColor2 )) ? iColor : iColor2 ));
    SendClientMessage( playerid, 0x00FF00FF, "**[SUCCESS] Carcolor changed successfully!" );
    return true;
}
And I may be stupid, but I'm not that stupid that I don't know that this code is completely wrong, but I'm not so familiar with ? and : yet, so I want to learn more.

Ty in advance!

EDIT: The errors occurs obviously in the changevehiclecolor line..

EDIT2: I now 'fixed' the errors; going in-game to test.
Meanwhile, please do improve my code, if it's unefficient or if it can be done in an easier/faster way.

Again, ty for all help.

EDIT3: Went in-game and tested - the last color is not optional.
When typing i.e. "/color 0" I get unknown command, but when typing "/color 0 0" the command is performed..
Any help?
Reply
#2

Thank you for the help, ******.
I should've known though, if I'd read the server log (sscanf warning: No default value found)
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)