12.09.2010, 22:44
(
Последний раз редактировалось LarzI; 13.09.2010 в 12:45.
)
Converting STRCMP & STRTOK to ZCMD/DCMD & SScanF
Why am I making this?Because most "noobs" don't know how to convert and because there's no tutorial on this yet AFAIK.
NOTE: ZCMD is the fastest command processor, so I recommend using it!
How:
STRCMP to DCMD&ZCMD:
This is really easy. The only differences are that zcmd and dcmd got "params".
Let's convert this easy command:
pawn Код:
if( !strcmp( cmdtext, "/suicide", true ))
{
new
szMessage[ 54 ],
pName[ MAX_PLAYER_NAME ];
GetPlayerName( playerid, pName, sizeof( pName ));
format( szMessage, sizeof( szMessage ), "(%i)%s just commited suicide.", playerid, pName );
SendClientMessageToAll( 0xFFFFFFFF, szMessage );
SetPlayerHealth( playerid, 0.0 );
return true;
}
Then, if you want DCMD, you need to put a line inside OnPlayerCommandText (zcmd doesn't require this)
pawn Код:
dcmd(command_name_here, length_of_command_excluding_the_slash, cmdtext);
pawn Код:
dcmd(suicide, 7, cmdtext); //suicide is the command, it's length is 7.
Change:
pawn Код:
if( !strcmp( cmdtext, "/suicide", true ))
pawn Код:
//dcmd:
dcmd_suicide(playerid, params[])
//zcmd:
CMD:suicide(playerid, params[])
To get rid of this warning, put
pawn Код:
#pragma unused params
pawn Код:
dcmd_mycommand(playerid, params[])
{
#pragma unused params
//rest of code
DCMD
pawn Код:
//onplayercommandtext
dcmd(suicide, 7, cmdtext);
pawn Код:
dcmd_suicide(playerid, params[] )
{
#pragma unused params
new
szMessage[ 54 ],
pName[ MAX_PLAYER_NAME ];
GetPlayerName( playerid, pName, sizeof( pName ));
format( szMessage, sizeof( szMessage ), "(%i)%s just commited suicide.", playerid, pName );
SendClientMessageToAll( 0xFFFFFFFF, szMessage );
SetPlayerHealth( playerid, 0.0 );
return true;
}
pawn Код:
CMD:suicide(playerid, params[])
{
new
szMessage[ 54 ],
pName[ MAX_PLAYER_NAME ];
GetPlayerName( playerid, pName, sizeof( pName ));
format( szMessage, sizeof( szMessage ), "(%i)%s just commited suicide.", playerid, pName );
SendClientMessageToAll( 0xFFFFFFFF, szMessage );
SetPlayerHealth( playerid, 0.0 );
return true;
}
Now let's try converting a command using strtok into dcmd/zcmd & sscanf!
Let's use this vehicle command for an example
pawn Код:
if( !strcmp( cmd, "/v", true ))
{
new
tmp[ 128 ],
vID,
pID,
Float:x,
Float:y,
Float:z;
tmp = strtok(cmdtext, idx);
if( !strlen( tmp ))
return SendClientMessage( playerid, 0xFFFF00FF, ”USAGE: /v [playerid] [vehicleid]” );
pID = strval( tmp );
tmp = strtok(cmdtext, idx);
if( !strlen( tmp ))
return SendClientMessage( playerid, 0xFFFF00FF, ”USAGE: /v [playerid] [vehicleid]” );
vID = strval( tmp );
if( vID < 400 || vID > 611 )
return SendClientMessage( playerid, 0xFF0000FF, ”Invalid vehicle model!” );
else if( !IsPlayerConnected( pID ))
return SendClientMessage( playerid, 0xFF0000FF, ”Invalid ID!” );
GetPlayerPos( pID, x, y, z );
GetXYInFrontOfPlayer( pID, x, y );
CreateVehicle( vID, x, y, z+0.5, 0.0, -1, -1, -1 );
return true;
}
NOTE: I'm not making efficient code, but I'm not trying to either.
To convert this, we need to do the basics first.
If you use dcmd add
pawn Код:
dcmd(v, 1, cmdtext);
After we've done that, we start converting the strtok to sscanf.
The code so far looks like this:
pawn Код:
dcmd_v(playerid, params[]) //or CMD:v(playerid, params[]) if you use zcmd
{
new
tmp[ 128 ],
vID,
pID,
Float:x,
Float:y,
Float:z;
tmp = strtok(cmdtext, idx);
if( !strlen( tmp ))
return SendClientMessage( playerid, 0xFFFF00FF, ”USAGE: /v [playerid] [vehicleid]” );
pID = strval( tmp );
tmp = strtok(cmdtext, idx);
if( !strlen( tmp ))
return SendClientMessage( playerid, 0xFFFF00FF, ”USAGE: /v [playerid] [vehicleid]” );
vID = strval( tmp );
if( vID < 400 || vID > 611 )
return SendClientMessage( playerid, 0xFF0000FF, ”Invalid vehicle model!” );
else if( !IsPlayerConnected( pID ))
return SendClientMessage( playerid, 0xFF0000FF, ”Invalid ID!” );
GetPlayerPos( pID, x, y, z );
GetXYInFrontOfPlayer( pID, x, y );
CreateVehicle( vID, x, y, z+0.5, 0.0, -1, -1, -1 );
return true;
}
First of all, we don't need the variable called "tmp" anymore, so delete that.
Text, delete the part starting at "tmp = strtok(cmdtext, idx);" all the way down to the second "vID = strval( tmp );",
leaving us with this code:
pawn Код:
dcmd_v(playerid, params[]) //or CMD:v(playerid, params[]) if you use zcmd
{
new
vID,
pID,
Float:x,
Float:y,
Float:z;
if( vID < 400 || vID > 611 )
return SendClientMessage( playerid, 0xFF0000FF, ”Invalid vehicle model!” );
else if( !IsPlayerConnected( pID ))
return SendClientMessage( playerid, 0xFF0000FF, ”Invalid ID!” );
GetPlayerPos( pID, x, y, z );
GetXYInFrontOfPlayer( pID, x, y );
CreateVehicle( vID, x, y, z+0.5, 0.0, -1, -1, -1 );
return true;
}
Ok, first of all, we need to add the sscanf line, which is constructed fairly easy:
pawn Код:
if( sscanf( params, "ii", pID, vID )) //will return true if _NOT_ both parameters are typed in.
return SendClientMessage( playerid, 0xFFFF00FF, "USAGE: /v [playerid] [vehicleid]" );
And since those lines is all you need, you are already done!
Our code atm:
pawn Код:
dcmd_v(playerid, params[]) //or CMD:v(playerid, params[]) if you use zcmd
{
new
vID,
pID,
Float:x,
Float:y,
Float:z;
if( sscanf( params, "ii", pID, vID ))
return SendClientMessage( playerid, 0xFFFF00FF, "USAGE: /v [playerid] [vehicleid]" );
if( vID < 400 || vID > 611 )
return SendClientMessage( playerid, 0xFF0000FF, ”Invalid vehicle model!” );
else if( !IsPlayerConnected( pID ))
return SendClientMessage( playerid, 0xFF0000FF, ”Invalid ID!” );
GetPlayerPos( pID, x, y, z );
GetXYInFrontOfPlayer( pID, x, y );
CreateVehicle( vID, x, y, z+0.5, 0.0, -1, -1, -1 );
return true;
}
I told you that I won't explain the sscanf part, but I decided I did a quick explanation:
The sscanf param check always starts with
pawn Код:
if( sscanf( params
In my example, we use two integers as params.
Integers are represented by either "i" or "d", and that is why I put "ii" in the sscanf line.
What comes after "ii" is the variables you use to store those integer values.
In my example, that is pID and vID.
Other representants are
s - string
u - user (playerid or (part of) name)
f - float
x - hex
There are more, but those you have to check up yourself.
Go to: https://sampwiki.blast.hk/wiki/Fast_Commands for more info about dcmd and sscanf.
I hope this "tutorial" helped you converting commands.
Good luck & Have fun!