Using strcmp()
Enviado: sábado mai 17, 2008 3:30 pm
Using strcmp() in OnPlayerCommandText to make your own commands
Open Pawno, File > New.
[edit]
Making a /me command
In this section we are going to make a /me command while NOT using a bunch of strtoks, which is inaccurate. More about strtok in the following section
1.
public OnPlayerCommandText(playerid, cmdtext[])
2.
{
3.
if(!strcmp(cmdtext, "/me", true, 3)) // 3 is the length of /me
4.
{
5.
if(cmdtext[3] == 0) {
6.
SendClientMessage(playerid, 0xFF0000FF, "USAGE: /me [action]");
7.
return 1;
8.
}
9.
new str[128];
10.
GetPlayerName(playerid, str, sizeof(str));
11.
format(str, sizeof(str), "* %s %s", str, cmdtext[4]);
12.
SendClientMessageToAll(0xFFFF00AA, str);
13.
return 1;
14.
}
15.
return 0;
16.
}
This will produce a /me command which will work perfectly . Let me explain why that line is highlighted.
If I use cmdtext[4] it will in fact cut the first 4 characters off the string. So in my code the string is not '/me blabla' but just 'blabla'.
[edit]
Using strtok()
strtok is the most used function for commands with extra parameters in it. Like /freeze PlayerID and /givecash PlayerID Money. The code below will say hello to the player you specify. For example /sayhello 4 will send "Hi, hello!" to player 4.
public OnPlayerCommandText(playerid, cmdtext[])
{
new cmd[30];
new idx;
cmd = strtok(cmdtext, idx);
if(strcmp(cmd, "/sayhello", true) == 0)
{
new tmp[30];
// assign the id (written by the user) to tmp
tmp = strtok(cmdtext, idx);
// convert the id to an integer using strval (this is essential)
// and assign to otherplayer
new otherplayer = strval(tmp);
if(IsPlayerConnected(otherplayer))
{
SendClientMessage(otherplayer, 0xFFFF00AA, "Hi, hello!");
}
return 1;
}
return 0;
}
Try understanding this code, don't forget to add the strtok definition in your script!
[edit]
Practicing
Try making simple commands with strtok for practicing. They don't have to be really good or anything else, as long as it helps you understanding strcmp and strtok. Also try adding more then one parameter to your script and see what sort of results you get
Open Pawno, File > New.
[edit]
Making a /me command
In this section we are going to make a /me command while NOT using a bunch of strtoks, which is inaccurate. More about strtok in the following section
1.
public OnPlayerCommandText(playerid, cmdtext[])
2.
{
3.
if(!strcmp(cmdtext, "/me", true, 3)) // 3 is the length of /me
4.
{
5.
if(cmdtext[3] == 0) {
6.
SendClientMessage(playerid, 0xFF0000FF, "USAGE: /me [action]");
7.
return 1;
8.
}
9.
new str[128];
10.
GetPlayerName(playerid, str, sizeof(str));
11.
format(str, sizeof(str), "* %s %s", str, cmdtext[4]);
12.
SendClientMessageToAll(0xFFFF00AA, str);
13.
return 1;
14.
}
15.
return 0;
16.
}
This will produce a /me command which will work perfectly . Let me explain why that line is highlighted.
If I use cmdtext[4] it will in fact cut the first 4 characters off the string. So in my code the string is not '/me blabla' but just 'blabla'.
[edit]
Using strtok()
strtok is the most used function for commands with extra parameters in it. Like /freeze PlayerID and /givecash PlayerID Money. The code below will say hello to the player you specify. For example /sayhello 4 will send "Hi, hello!" to player 4.
public OnPlayerCommandText(playerid, cmdtext[])
{
new cmd[30];
new idx;
cmd = strtok(cmdtext, idx);
if(strcmp(cmd, "/sayhello", true) == 0)
{
new tmp[30];
// assign the id (written by the user) to tmp
tmp = strtok(cmdtext, idx);
// convert the id to an integer using strval (this is essential)
// and assign to otherplayer
new otherplayer = strval(tmp);
if(IsPlayerConnected(otherplayer))
{
SendClientMessage(otherplayer, 0xFFFF00AA, "Hi, hello!");
}
return 1;
}
return 0;
}
Try understanding this code, don't forget to add the strtok definition in your script!
[edit]
Practicing
Try making simple commands with strtok for practicing. They don't have to be really good or anything else, as long as it helps you understanding strcmp and strtok. Also try adding more then one parameter to your script and see what sort of results you get