I can't understand how "dcmd" works.
#1

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;
}
Reply
#2

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
Reply
#3

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]++;
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)