I can't understand how "dcmd" works. - 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: I can't understand how "dcmd" works. (
/showthread.php?tid=281128)
I can't understand how "dcmd" works. -
Shetch - 04.09.2011
So here is my code. What i'm trying to do is, when player types /bomb plant, it shows "You typed 'plant'".
But when the player typed /bomb defuse, it shows "You typed 'defuse'".
Can anyone please help me?
Код:
dcmd_bomb(playerid,params[])
{
new tmp[256], Index;
new plant, defuse;
tmp = strtok(params,Index);
if(!strlen(params)) return SendClientMessage(playerid,GREY,"ERROR: /bomb [plant/defuse]");
if(strlen(tmp) == defuse)
{
SendClientMessage(playerid, GREY, "You typed 'defuse'");
return 1;
}
if(strlen(tmp) == plant)
{
SendClientMessage(playerid, GREY, "You typed 'plant'");
return 1;
}
return 1;
}
Re: I can't understand how "dcmd" works. -
JaTochNietDan - 04.09.2011
Quote:
Originally Posted by Shetch
So here is my code. What i'm trying to do is, when player types /bomb plant, it shows "You typed 'plant'".
But when the player typed /bomb defuse, it shows "You typed 'defuse'".
Can anyone please help me?
Код:
dcmd_bomb(playerid,params[])
{
new tmp[256], Index;
new plant, defuse;
tmp = strtok(params,Index);
if(!strlen(params)) return SendClientMessage(playerid,GREY,"ERROR: /bomb [plant/defuse]");
if(strlen(tmp) == defuse)
{
SendClientMessage(playerid, GREY, "You typed 'defuse'");
return 1;
}
if(strlen(tmp) == plant)
{
SendClientMessage(playerid, GREY, "You typed 'plant'");
return 1;
}
return 1;
}
|
Why do you try and split the string again using strtok? It's already split for you as "params", also you need to compare strings using strcmp (string compare), strlen gets the length of the string. Here is an example:
pawn Код:
dcmd_bomb(playerid,params[])
{
if(!strlen(params)) return SendClientMessage(playerid,GREY,"ERROR: /bomb [plant/defuse]");
if(!strcmp(params, "defuse", true))
{
SendClientMessage(playerid, GREY, "You typed 'defuse'");
}
else if(!strcmp(params, "plant", true))
{
SendClientMessage(playerid, GREY, "You typed 'plant'");
}
return 1;
}
You can read more about these functions on the SA-MP Wiki:
https://sampwiki.blast.hk/wiki/Strcmp
https://sampwiki.blast.hk/wiki/Strlen
I also suggest you start up from the beginning with the PAWN documentation:
http://www.compuphase.com/pawn/pawn.htm
Re: I can't understand how "dcmd" works. -
Shetch - 04.09.2011
Quote:
Originally Posted by JaTochNietDan
Why do you try and split the string again using strtok? It's already split for you as "params", also you need to compare strings using strcmp (string compare), strlen gets the length of the string. Here is an example:
pawn Код:
dcmd_bomb(playerid,params[]) { if(!strlen(params)) return SendClientMessage(playerid,GREY,"ERROR: /bomb [plant/defuse]"); if(!strcmp(params, "defuse", true)) { SendClientMessage(playerid, GREY, "You typed 'defuse'"); } else if(!strcmp(params, "plant", true)) { SendClientMessage(playerid, GREY, "You typed 'plant'"); } return 1; }
You can read more about these functions on the SA-MP Wiki:
https://sampwiki.blast.hk/wiki/Strcmp
https://sampwiki.blast.hk/wiki/Strlen
I also suggest you start up from the beginning with the PAWN documentation:
http://www.compuphase.com/pawn/pawn.htm
|
I've been scripting for almost a year now.
Just there are the things i've never run in to.
Anyway, thanks for the help.
reputation[playerid]++;