Using strcmp()

From SA-MP Wiki

Jump to: navigation, search

Image:50px-Ambox_outdated_serious.png This article is outdated. It may use methods or functions which no longer exist or which are deemed obsolete by the community. Caution is advised.


Using strcmp() in OnPlayerCommandText to make your own commands

Open Pawno, File > New.


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

public OnPlayerCommandText(playerid, cmdtext[])
{
    if(!strcmp(cmdtext, "/me", true, 3)) // 3 is the length of /me
    {
        if(!cmdtext[3])return SendClientMessage(playerid, 0xFF0000FF, "USAGE: /me [action]");
        new str[128];
        GetPlayerName(playerid, str, sizeof(str));
        format(str, sizeof(str), "* %s %s", str, cmdtext[4]);
        SendClientMessageToAll(0xFFFF00AA, str);
        return 1;
    }
    return 0;
}

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'.

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.

Note: There are faster, better and more reliable methods than strtok, such as sscanf and zcmd.

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!

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 with understanding strcmp and strtok. Also try adding more than one parameter to your script and see what sort of results you get.

Personal tools
In other languages